From 3e2cd701d0ddbde0a498c15160f6a2bf73c3b17d Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Mon, 24 Feb 2025 20:55:06 -0600 Subject: [PATCH] Able to fetch a list of module names --- Cargo.lock | 104 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 ++ build.rs | 2 + flake.nix | 5 ++- src/cxx/mgr.cc | 20 +++++++++- src/cxx/mgr.h | 10 +++-- src/main.rs | 6 +++ src/mgr.rs | 42 ++++++++++++++++++++ 8 files changed, 184 insertions(+), 8 deletions(-) create mode 100644 src/main.rs diff --git a/Cargo.lock b/Cargo.lock index 44989a7..e7ee688 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -8,6 +8,12 @@ version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" +[[package]] +name = "bitflags" +version = "2.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f68f53c83ab957f72c32642f3868eec03eb974d1fb82e453128456482613d36" + [[package]] name = "cc" version = "1.2.15" @@ -27,6 +33,12 @@ dependencies = [ "target-lexicon", ] +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + [[package]] name = "clap" version = "4.5.31" @@ -128,12 +140,40 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" +[[package]] +name = "errno" +version = "0.3.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" +dependencies = [ + "libc", + "windows-sys", +] + +[[package]] +name = "fastrand" +version = "2.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37909eebbb50d72f9059c3b6d82c0463f2ff062c9e95845c43a6c9c0355411be" + [[package]] name = "foldhash" version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a0d2fde1f7b3d48b8395d5f2de76c18a528bd6a9cdde438df747bfcba3e05d6f" +[[package]] +name = "getrandom" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43a49c392881ce6d5c3b8cb70f98717b7c07aabbdff06687b9030dbfbe2725f8" +dependencies = [ + "cfg-if", + "libc", + "wasi", + "windows-targets", +] + [[package]] name = "hashbrown" version = "0.15.2" @@ -156,6 +196,12 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "libc" +version = "0.2.170" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "875b3680cb2f8f71bdcf9a30f38d48282f5d3c95cbf9b3fa57269bb5d5c06828" + [[package]] name = "link-cplusplus" version = "1.0.9" @@ -165,12 +211,24 @@ dependencies = [ "cc", ] +[[package]] +name = "linux-raw-sys" +version = "0.4.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d26c52dbd32dccf2d10cac7725f8eae5296885fb5703b261f7d0a0739ec807ab" + [[package]] name = "memchr" version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "once_cell" +version = "1.20.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e" + [[package]] name = "pkg-config" version = "0.3.31" @@ -195,6 +253,19 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "rustix" +version = "0.38.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fdb5bc1ae2baa591800df16c9ca78619bf65c0488b41b96ccec5d11220d8c154" +dependencies = [ + "bitflags", + "errno", + "libc", + "linux-raw-sys", + "windows-sys", +] + [[package]] name = "rustversion" version = "1.0.19" @@ -261,6 +332,7 @@ dependencies = [ "cxx", "cxx-build", "system-deps", + "tempfile", ] [[package]] @@ -293,6 +365,20 @@ version = "0.12.16" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "61c41af27dd6d1e27b1b16b489db798443478cef1f06a660c96db617ba5de3b1" +[[package]] +name = "tempfile" +version = "3.17.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22e5a0acb1f3f55f65cc4a866c361b2fb2a0ff6366785ae6fbb5f85df07ba230" +dependencies = [ + "cfg-if", + "fastrand", + "getrandom", + "once_cell", + "rustix", + "windows-sys", +] + [[package]] name = "termcolor" version = "1.4.1" @@ -354,6 +440,15 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "852e951cb7832cb45cb1169900d19760cfa39b82bc0ea9c0e5a14ae88411c98b" +[[package]] +name = "wasi" +version = "0.13.3+wasi-0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "26816d2e1a4a36a2940b96c5296ce403917633dff8f3440e9b236ed6f6bacad2" +dependencies = [ + "wit-bindgen-rt", +] + [[package]] name = "winapi-util" version = "0.1.9" @@ -444,3 +539,12 @@ checksum = "0e7f4ea97f6f78012141bcdb6a216b2609f0979ada50b20ca5b52dde2eac2bb1" dependencies = [ "memchr", ] + +[[package]] +name = "wit-bindgen-rt" +version = "0.33.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3268f3d866458b787f390cf61f4bbb563b922d091359f9608842999eaee3943c" +dependencies = [ + "bitflags", +] diff --git a/Cargo.toml b/Cargo.toml index 5a2a9a6..aac59a9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,8 @@ cxx = "1.0.141" cxx-build = "1.0.141" system-deps = "7.0.3" +[dev-dependencies] +tempfile = "3.17.1" + [package.metadata.system-deps] sword = ">= 1.8.0" diff --git a/build.rs b/build.rs index c623576..d1b3d4c 100644 --- a/build.rs +++ b/build.rs @@ -6,6 +6,8 @@ fn main() { cxx_build::bridge("src/mgr.rs") .file("src/cxx/mgr.cc") .includes(deps.all_include_paths()) + .cpp_link_stdlib("stdc++") + .flag("-Os") .compile("sword_rs"); println!("cargo:rerun-if-changed=src/mgr.rs"); diff --git a/flake.nix b/flake.nix index 60cb815..4762097 100644 --- a/flake.nix +++ b/flake.nix @@ -44,10 +44,11 @@ # These will just be available packages = with pkgs; [ cargo + clippy + libgcc + rustc.llvm rustc rustfmt - clippy - stdenv.cc pre-commit sword diff --git a/src/cxx/mgr.cc b/src/cxx/mgr.cc index 947fb41..a52d175 100644 --- a/src/cxx/mgr.cc +++ b/src/cxx/mgr.cc @@ -1,8 +1,24 @@ #include "mgr.h" -#include -Mgr::Mgr() : mgr(new sword::SWMgr()) { } +Mgr::Mgr() { } + +// Only the last argument is different from default - we want to limit this to ONLY the given directory +Mgr::Mgr(const char *path) : sword::SWMgr(path, true, 0, false, false) { } + +rust::Vec Mgr::get_modules() const { + rust::Vec v; + v.reserve(this->getModules().size()); + for (sword::ModMap::const_iterator it = this->getModules().begin(); it != this->getModules().end(); ++it) { + v.push_back(rust::String(it->first.c_str())); + } + return v; +} std::unique_ptr new_mgr() { return std::unique_ptr(new Mgr()); } + +std::unique_ptr new_mgr_with_path(const rust::String &path) { + std::string cpath(path); + return std::unique_ptr(new Mgr(cpath.c_str())); +} diff --git a/src/cxx/mgr.h b/src/cxx/mgr.h index 4153bf6..b497730 100644 --- a/src/cxx/mgr.h +++ b/src/cxx/mgr.h @@ -1,14 +1,16 @@ #pragma once #include +#include #include +#include "rust/cxx.h" -class Mgr { +class Mgr : public sword::SWMgr { public: Mgr(); - - private: - sword::SWMgr* mgr; + Mgr(const char*); + rust::Vec get_modules() const; }; std::unique_ptr new_mgr(); +std::unique_ptr new_mgr_with_path(const rust::String &path); diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..a2077b8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,6 @@ +use sword_rs::mgr::{new_mgr, Mgr}; + +fn main() { + let mgr = new_mgr(); + println!("{:?}", mgr.get_modules()); +} diff --git a/src/mgr.rs b/src/mgr.rs index 15be09b..acac18c 100644 --- a/src/mgr.rs +++ b/src/mgr.rs @@ -1,3 +1,5 @@ +pub use ffi::*; + #[cxx::bridge] mod ffi { unsafe extern "C++" { @@ -6,16 +8,56 @@ mod ffi { type Mgr; fn new_mgr() -> UniquePtr; + fn new_mgr_with_path(path: &String) -> UniquePtr; + fn get_modules(&self) -> Vec; } } #[cfg(test)] mod tests { use super::*; + use std::fs::{create_dir, File}; + use std::io::Write; + use tempfile; #[test] fn can_create_mgr() { let mgr = ffi::new_mgr(); assert!(!mgr.is_null()); } + + #[test] + fn reads_module_list() { + // Create a temporary directory to isolate tests + let dir = tempfile::tempdir().unwrap(); + // Create a basic SWORD directory + let mods_d = dir.path().join("mods.d"); + create_dir(&mods_d).unwrap(); + // Write a dummy conf file + let kjv_conf = mods_d.join("kjv.conf"); + { + let mut file = File::create(&kjv_conf).unwrap(); + writeln!(file, "[KJVdummy]").unwrap(); + writeln!(file, "DataPath=./modules/texts/ztext/kjv").unwrap(); + writeln!(file, "ModDrv=RawText").unwrap(); + writeln!(file, "Description=Test description").unwrap(); + writeln!(file, "About=A test conf file").unwrap(); + } + let mgr = ffi::new_mgr_with_path(&dir.path().to_str().unwrap().to_string()); + assert!(mgr.get_modules().len() == 1); + assert!(mgr.get_modules().contains(&String::from("KJVdummy"))); + } + + #[test] + fn has_no_modules() { + let mgr = ffi::new_mgr_with_path( + &tempfile::tempdir() + .unwrap() + .path() + .to_str() + .unwrap() + .to_string(), + ); + assert!(mgr.get_modules().len() == 0); + } }