From 09510be5174527085e3239326d252c42a977ca07 Mon Sep 17 00:00:00 2001 From: ARCJ137442 <61109168+ARCJ137442@users.noreply.github.com> Date: Sun, 31 Mar 2024 15:42:28 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20:recycle:=20=E5=9F=BA=E4=BA=8E?= =?UTF-8?q?=E3=80=8C=E7=89=B9=E6=80=A7=E3=80=8D=E7=9A=84=E9=A1=B9=E7=9B=AE?= =?UTF-8?q?=E7=BB=93=E6=9E=84=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. 全库定位重调整:+NAVM的应用程序实现 2. 将原先归属二进制子crate的「cin_launcher」归入主crate特性「命令行支持」下 --- Cargo.lock | 79 +++++++++++++++- Cargo.toml | 90 ++++++++++++------- README.md | 2 +- src/bin/cin_launcher/main.rs | 18 ++-- src/cin_implements/ona/translators.rs | 2 +- .../cin_search/anyhow_vm.rs | 0 .../cin_search}/impls_path_builder/mod.rs | 11 ++- .../impls_path_builder/path_builder_ona.rs | 13 ++- .../path_builder_opennars.rs | 13 ++- .../cin_search/mod.rs | 21 +++-- .../cin_search/name_match.rs | 0 .../cin_search/path_builder.rs | 0 .../cin_search/path_walker.rs | 4 +- .../io/mod.rs | 0 src/cmdline_support/mod.rs | 10 +++ src/lib.rs | 8 +- 16 files changed, 190 insertions(+), 81 deletions(-) rename src/{bin/cin_launcher => cmdline_support}/cin_search/anyhow_vm.rs (100%) rename src/{bin/cin_launcher => cmdline_support/cin_search}/impls_path_builder/mod.rs (88%) rename src/{bin/cin_launcher => cmdline_support/cin_search}/impls_path_builder/path_builder_ona.rs (87%) rename src/{bin/cin_launcher => cmdline_support/cin_search}/impls_path_builder/path_builder_opennars.rs (87%) rename src/{bin/cin_launcher => cmdline_support}/cin_search/mod.rs (72%) rename src/{bin/cin_launcher => cmdline_support}/cin_search/name_match.rs (100%) rename src/{bin/cin_launcher => cmdline_support}/cin_search/path_builder.rs (100%) rename src/{bin/cin_launcher => cmdline_support}/cin_search/path_walker.rs (99%) rename src/{bin/cin_launcher => cmdline_support}/io/mod.rs (100%) create mode 100644 src/cmdline_support/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 4130358..83d301f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -22,6 +22,7 @@ name = "babel_nar" version = "0.9.0" dependencies = [ "anyhow", + "colored", "lazy_static", "nar_dev_utils", "narsese", @@ -46,6 +47,16 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "colored" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbf2150cce219b664a8a70df7a1f933836724b503f8a413af9365b4dcc4d90b8" +dependencies = [ + "lazy_static", + "windows-sys", +] + [[package]] name = "cpufeatures" version = "0.2.12" @@ -105,7 +116,7 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149" [[package]] name = "nar_dev_utils" -version = "0.22.0" +version = "0.23.1" [[package]] name = "narsese" @@ -287,3 +298,69 @@ name = "version_check" version = "0.9.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f" + +[[package]] +name = "windows-sys" +version = "0.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9" +dependencies = [ + "windows-targets", +] + +[[package]] +name = "windows-targets" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c" +dependencies = [ + "windows_aarch64_gnullvm", + "windows_aarch64_msvc", + "windows_i686_gnu", + "windows_i686_msvc", + "windows_x86_64_gnu", + "windows_x86_64_gnullvm", + "windows_x86_64_msvc", +] + +[[package]] +name = "windows_aarch64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8" + +[[package]] +name = "windows_aarch64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc" + +[[package]] +name = "windows_i686_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e" + +[[package]] +name = "windows_i686_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406" + +[[package]] +name = "windows_x86_64_gnu" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e" + +[[package]] +name = "windows_x86_64_gnullvm" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc" + +[[package]] +name = "windows_x86_64_msvc" +version = "0.48.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538" diff --git a/Cargo.toml b/Cargo.toml index f5dcbbd..f90316b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -3,35 +3,16 @@ name = "babel_nar" version = "0.9.0" edition = "2021" +# Cargo文档参考: + +## 必要的依赖 ## + [dependencies] -pest_derive = "2.7.8" +# 用于错误处理 [dependencies.anyhow] version = "1.0.81" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -# Rust版本的正则表达式 -# * 🎯用于解析提取NARS输出 -# * 📄OpenNARS、ONA、PyNARS -[dependencies.regex] -version = "1.10.4" -optional = true - -# Rust版本的PEG解析器 -# * 🎯用于对接一些NARS方言的解析 -# * 📄OpenNARS(操作语法)、ONA(中缀语法) -[dependencies.pest] -version = "2.7.8" -optional = true - -# 用于实现「静态含闭包常量」 -# * 🎯初次引入:NARS-Python 方言格式 -# * 🔗:https://stackoverflow.com/questions/73260997/rust-boxed-closure-in-global-variable -[dependencies.lazy_static] -version = "1.4.0" -optional = true - [dependencies.nar_dev_utils] # 【2024-03-13 21:17:55】实用库现在独立为`nar_dev_utils` # version = "0.1.0" # ! 本地依赖可以不添加版本 @@ -39,7 +20,7 @@ optional = true path = "../NAR-dev-util" # git = "https://github.com/ARCJ137442/NAR-dev-util" # ! 【2024-03-23 19:19:01】似乎Rust-Analyzer无法获取私有仓库数据 -features = [] +features = [ "bundled" ] # 启用所有特性 [dependencies.narsese] # ! 本地依赖可以不添加版本 @@ -62,17 +43,53 @@ path = "../NAVM.rs" # ! 【2024-03-23 19:19:01】似乎Rust-Analyzer无法获取私有仓库数据 features = [] # ! 【2024-03-21 09:24:51】暂时没有特性 -# 定义库的特性 +## 依赖特性的可选依赖 ## + +# Rust版本的正则表达式 +# * 🎯用于解析提取NARS输出 +# * 📄OpenNARS、ONA、PyNARS +[dependencies.regex] +version = "1.10.4" +optional = true + +# 用于实现「静态含闭包常量」 +# * 🎯初次引入:NARS-Python 方言格式 +# * 🔗:https://stackoverflow.com/questions/73260997/rust-boxed-closure-in-global-variable +[dependencies.lazy_static] +version = "1.4.0" +optional = true + +# Rust版本的PEG解析器 +# * 🎯用于对接一些NARS方言的解析 +# * 📄OpenNARS(操作语法)、ONA(中缀语法) +[dependencies.pest] +version = "2.7.8" +optional = true + +# Rust版本的PEG解析器(派生宏) +[dependencies.pest_derive] +version = "2.7.8" +optional = true + +# 命令行支持/彩色终端 +[dependencies.colored] +version = "2.1.0" +optional = true + +### 定义库的特性 ### [features] -# 默认启用的特性 + +## 默认启用的特性 ## default = [] -# 大杂烩 +## 大杂烩 ## bundled = [ - "cin_implements", - "lazy_static", # 这个「词法Narsese」也在用 + "cin_implements", # 各大CIN的NAVM实现 + "cmdline_support" # 命令行支持 ] -# 各个独立的特性 # -# 具体接口实现: + +## 各个独立的特性 ## + +# 具体接口实现(虚拟机启动器) # # ✅OpenNARS # ✅ONA # ✅PyNARS @@ -101,6 +118,13 @@ pynars = [ # "pest", # ! 【2024-03-27 20:52:17】无需特别解析方言:其输出即为CommonNarsese ] # ✅NARS-Python接口(不稳定) -nars_python = [] +nars_python = [ + "lazy_static", # 这个「词法Narsese」也在用 +] # ✅OpenJunars接口(不稳定) openjunars = [] + +# 命令行支持 # +cmdline_support = [ + "colored" +] diff --git a/README.md b/README.md index 8e8850d..e4348f9 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ 该项目使用[语义化版本 2.0.0](https://semver.org/)进行版本号管理。 -[**NAVM.rs**](https://github.com/ARCJ137442/NAVM.rs)的**运行时**及[CIN](#cin-computer-implement-of-nars)启动器 +[**NAVM.rs**](https://github.com/ARCJ137442/NAVM.rs)对[CIN](#cin-computer-implement-of-nars)的**启动器**、**运行时**及应用程序实现 - 前身为[**BabelNAR.jl**](https://github.com/ARCJ137442/BabelNAR.jl) - ✨为「非公理虚拟机模型」提供程序实现 diff --git a/src/bin/cin_launcher/main.rs b/src/bin/cin_launcher/main.rs index 14f5aa1..368a390 100644 --- a/src/bin/cin_launcher/main.rs +++ b/src/bin/cin_launcher/main.rs @@ -9,7 +9,10 @@ //! TODO: 完成代码 #![allow(unused)] -use babel_nar::{ona::ONA, opennars::OpenNARS, pynars::PyNARS, runtime::CommandVmRuntime}; +use babel_nar::{ + cin_implements::{ona::ONA, opennars::OpenNARS, pynars::PyNARS}, + runtime::CommandVmRuntime, +}; use nar_dev_utils::*; use navm::{ cmd::Cmd, @@ -18,15 +21,6 @@ use navm::{ }; use std::{fmt::Debug, io::stdin}; -mods! { - // CIN搜索 - use pub cin_search; - // 输入输出 - use pub io; - // 路径构建器的各CIN实现 - use pub impls_path_builder; -} - const TEST_PATH_OPENNARS: &str = r"..\..\NARS-executables\opennars-304-T-modified.jar"; const TEST_PATH_ONA: &str = r"..\..\NARS-executables\NAR.exe"; const TEST_PATH_PYNARS: (&str, &str) = ("..\\..\\PyNARS-dev", "pynars.ConsolePlus"); @@ -94,8 +88,8 @@ fn shell(mut nars: CommandVmRuntime) { #[cfg(test)] mod tests { use super::*; - use babel_nar::cxin_js::CXinJS; - use babel_nar::pynars::PyNARS; + use babel_nar::cin_implements::cxin_js::CXinJS; + use babel_nar::cin_implements::pynars::PyNARS; use narsese::conversion::string::impl_lexical::format_instances::FORMAT_ASCII; use navm::cmd::Cmd; use navm::vm::VmLauncher; diff --git a/src/cin_implements/ona/translators.rs b/src/cin_implements/ona/translators.rs index b873790..5765276 100644 --- a/src/cin_implements/ona/translators.rs +++ b/src/cin_implements/ona/translators.rs @@ -24,7 +24,7 @@ use super::dialect::parse as parse_dialect_ona; use crate::{ - ona::{fold_pest_compound, DialectParser, Rule}, + cin_implements::ona::{fold_pest_compound, DialectParser, Rule}, runtime::TranslateError, }; use anyhow::Result; diff --git a/src/bin/cin_launcher/cin_search/anyhow_vm.rs b/src/cmdline_support/cin_search/anyhow_vm.rs similarity index 100% rename from src/bin/cin_launcher/cin_search/anyhow_vm.rs rename to src/cmdline_support/cin_search/anyhow_vm.rs diff --git a/src/bin/cin_launcher/impls_path_builder/mod.rs b/src/cmdline_support/cin_search/impls_path_builder/mod.rs similarity index 88% rename from src/bin/cin_launcher/impls_path_builder/mod.rs rename to src/cmdline_support/cin_search/impls_path_builder/mod.rs index d6837fa..08bc854 100644 --- a/src/bin/cin_launcher/impls_path_builder/mod.rs +++ b/src/cmdline_support/cin_search/impls_path_builder/mod.rs @@ -5,12 +5,13 @@ //! TODO: CXinNARS //! * 🚩【2024-03-31 01:27:09】其它接口完成度不高的CIN,暂时弃了 -use crate::{name_match::is_name_match, path_builder::CinPathBuilder, path_walker::PathWalker}; -use nar_dev_utils::{list, mods}; +use crate::cmdline_support::cin_search::{ + name_match::is_name_match, path_builder::CinPathBuilder, path_walker::PathWalker, +}; use navm::vm::{VmLauncher, VmRuntime}; use std::path::Path; -mods! { +util::mods! { // OpenNARS use pub path_builder_opennars; // ONA @@ -55,9 +56,7 @@ pub fn launchers_from_walker_sorted>( #[cfg(test)] mod tests { use super::*; - use crate::cin_search::path_builder::CinPathBuilder; - use crate::path_walker::PathWalkerV1; - use nar_dev_utils::list; + use crate::cmdline_support::cin_search::path_walker::PathWalkerV1; use std::env::current_dir; #[test] diff --git a/src/bin/cin_launcher/impls_path_builder/path_builder_ona.rs b/src/cmdline_support/cin_search/impls_path_builder/path_builder_ona.rs similarity index 87% rename from src/bin/cin_launcher/impls_path_builder/path_builder_ona.rs rename to src/cmdline_support/cin_search/impls_path_builder/path_builder_ona.rs index 8dd3c1a..de205aa 100644 --- a/src/bin/cin_launcher/impls_path_builder/path_builder_ona.rs +++ b/src/cmdline_support/cin_search/impls_path_builder/path_builder_ona.rs @@ -1,13 +1,12 @@ //! 用于ONA的路径构建器 -use crate::{ - name_match::{is_name_match, name_match, name_match_only_contains}, +use crate::cmdline_support::cin_search::{ + name_match::{name_match, name_match_only_contains}, path_builder::CinPathBuilder, }; -use babel_nar::{ona::ONA, runtime::CommandVmRuntime}; -use nar_dev_utils::{if_return, list, OptionBoost}; -use navm::vm::{VmLauncher, VmRuntime}; -use std::path::{Path, PathBuf}; +use crate::{cin_implements::ona::ONA, runtime::CommandVmRuntime}; +use nar_dev_utils::{if_return, OptionBoost}; +use std::path::Path; /// ONA路径构建器 /// * 🎯判别路径并构建ONA启动器 @@ -59,7 +58,7 @@ impl CinPathBuilder for PathBuilderONA { mod tests { use super::*; use nar_dev_utils::{f_parallel, fail_tests}; - use std::path::{self, Path}; + use std::path::Path; /// 工具/测试单个路径 fn test_matched(path: &str) { diff --git a/src/bin/cin_launcher/impls_path_builder/path_builder_opennars.rs b/src/cmdline_support/cin_search/impls_path_builder/path_builder_opennars.rs similarity index 87% rename from src/bin/cin_launcher/impls_path_builder/path_builder_opennars.rs rename to src/cmdline_support/cin_search/impls_path_builder/path_builder_opennars.rs index 1ed6a24..5c950f4 100644 --- a/src/bin/cin_launcher/impls_path_builder/path_builder_opennars.rs +++ b/src/cmdline_support/cin_search/impls_path_builder/path_builder_opennars.rs @@ -1,13 +1,12 @@ //! 用于OpenNARS的路径构建器 use crate::{ - name_match::{is_name_match, name_match}, - path_builder::CinPathBuilder, + cin_implements::opennars::OpenNARS, + cmdline_support::cin_search::{name_match::name_match, path_builder::CinPathBuilder}, + runtime::CommandVmRuntime, }; -use babel_nar::{opennars::OpenNARS, runtime::CommandVmRuntime}; -use nar_dev_utils::{if_return, list, OptionBoost}; -use navm::vm::{VmLauncher, VmRuntime}; -use std::path::{Path, PathBuf}; +use nar_dev_utils::{if_return, OptionBoost}; +use std::path::Path; /// OpenNARS路径构建器 /// * 🎯判别路径并构建OpenNARS启动器 @@ -55,7 +54,7 @@ impl CinPathBuilder for PathBuilderOpenNARS { mod tests { use super::*; use nar_dev_utils::{f_parallel, fail_tests}; - use std::path::{self, Path}; + use std::path::Path; /// 工具/测试单个路径 fn test_matched(path: &str) { diff --git a/src/bin/cin_launcher/cin_search/mod.rs b/src/cmdline_support/cin_search/mod.rs similarity index 72% rename from src/bin/cin_launcher/cin_search/mod.rs rename to src/cmdline_support/cin_search/mod.rs index c27aee7..feb8598 100644 --- a/src/bin/cin_launcher/cin_search/mod.rs +++ b/src/cmdline_support/cin_search/mod.rs @@ -4,16 +4,19 @@ //! * 🚩输出:NAVM启动器列表 //! * ❓【2024-03-30 19:12:29】是否要考虑返回更细化的「CIN实例位置」而非「CIN启动器」,以避免额外的性能开销? -use nar_dev_utils::mods; - -/// 导出模块 -mods! { - // 路径遍历器 - use pub path_walker; - // 路径构造器 - use pub path_builder; +// 导出模块 +util::mods! { // anyhow | 弃用 // anyhow_vm; // 名称匹配 - use pub name_match; + pub name_match; + + // 路径遍历器 + pub path_walker; + + // 路径构建器 + pub path_builder; + + // 路径构建器的各CIN实现 + "cin_implements" => pub impls_path_builder; } diff --git a/src/bin/cin_launcher/cin_search/name_match.rs b/src/cmdline_support/cin_search/name_match.rs similarity index 100% rename from src/bin/cin_launcher/cin_search/name_match.rs rename to src/cmdline_support/cin_search/name_match.rs diff --git a/src/bin/cin_launcher/cin_search/path_builder.rs b/src/cmdline_support/cin_search/path_builder.rs similarity index 100% rename from src/bin/cin_launcher/cin_search/path_builder.rs rename to src/cmdline_support/cin_search/path_builder.rs diff --git a/src/bin/cin_launcher/cin_search/path_walker.rs b/src/cmdline_support/cin_search/path_walker.rs similarity index 99% rename from src/bin/cin_launcher/cin_search/path_walker.rs rename to src/cmdline_support/cin_search/path_walker.rs index 7c6ec50..f01115c 100644 --- a/src/bin/cin_launcher/cin_search/path_walker.rs +++ b/src/cmdline_support/cin_search/path_walker.rs @@ -211,7 +211,7 @@ impl Iterator for PathWalkerV1<'_> { #[cfg(test)] mod tests { use super::*; - use crate::name_match::is_name_match; + use crate::cmdline_support::cin_search::name_match::is_name_match; use std::env::current_dir; fn _test_path_walker_v1(start: impl Into) { @@ -221,7 +221,7 @@ mod tests { fn deep_criterion(path: &Path) -> bool { path.file_name() .is_some_and(|name| name.to_str().is_some_and(|s| is_name_match("nars", s))) - }; + } // 构建遍历者,加上条件 let walker = PathWalkerV1::new(start, deep_criterion).unwrap(); // 打印遍历者的「祖先列表」 diff --git a/src/bin/cin_launcher/io/mod.rs b/src/cmdline_support/io/mod.rs similarity index 100% rename from src/bin/cin_launcher/io/mod.rs rename to src/cmdline_support/io/mod.rs diff --git a/src/cmdline_support/mod.rs b/src/cmdline_support/mod.rs new file mode 100644 index 0000000..810a0c9 --- /dev/null +++ b/src/cmdline_support/mod.rs @@ -0,0 +1,10 @@ +//! 命令行支持 +//! * 🎯通用、可选地复用「CIN启动器」等「命令行工具」的内容 +//! * 🎯亦可为后续基于UI的应用提供支持 + +util::mods! { + // CIN搜索 + pub cin_search; + // 输入输出 + pub io; +} diff --git a/src/lib.rs b/src/lib.rs index 58ad3ab..50017a7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,13 +9,17 @@ pub extern crate nar_dev_utils as util; // 必选模块 // // 进程IO pub mod process_io; + // 运行时 pub mod runtime; + // (可选的实用)工具 pub mod tools; // 可选模块 // -util::feature_pub_mod_and_reexport! { +util::mods! { // 运行时实现 - "cin_implements" => cin_implements + "cin_implements" => pub cin_implements; + // 命令行支持 + "cmdline_support" => pub cmdline_support; }