From 87fde0db108c4fc0d16d5d82de045f854c9b8291 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Mon, 24 Feb 2025 22:35:47 -0600 Subject: [PATCH] Fetching modules and their names --- src/cxx/mgr.h | 5 +++++ src/cxx/module.h | 23 +++++++++++++++++++++++ src/mgr.rs | 13 +++++++++++-- 3 files changed, 39 insertions(+), 2 deletions(-) create mode 100644 src/cxx/module.h diff --git a/src/cxx/mgr.h b/src/cxx/mgr.h index b497730..8d0b6c5 100644 --- a/src/cxx/mgr.h +++ b/src/cxx/mgr.h @@ -3,13 +3,18 @@ #include #include #include + #include "rust/cxx.h" +#include "module.h" class Mgr : public sword::SWMgr { public: Mgr(); Mgr(const char*); rust::Vec get_modules() const; + std::unique_ptr get_module(const rust::String &name) { + return std::unique_ptr(new Module(this->getModule(std::string(name).c_str()))); + }; }; std::unique_ptr new_mgr(); diff --git a/src/cxx/module.h b/src/cxx/module.h new file mode 100644 index 0000000..9a852d4 --- /dev/null +++ b/src/cxx/module.h @@ -0,0 +1,23 @@ +#pragma once +#include +#include + +#include "rust/cxx.h" + +class Module { + private: + sword::SWModule* module; + + public: + Module(sword::SWModule* module) : module(module) { }; + + rust::String get_name() const { + return rust::String(this->module->getName()); + } + + rust::String strip_text() { + return std::string( + (const char*) this->module->stripText() + ); + } +}; diff --git a/src/mgr.rs b/src/mgr.rs index acac18c..4a21028 100644 --- a/src/mgr.rs +++ b/src/mgr.rs @@ -4,12 +4,18 @@ pub use ffi::*; mod ffi { unsafe extern "C++" { include!("sword_rs/src/cxx/mgr.h"); + include!("sword_rs/src/cxx/module.h"); type Mgr; + type Module; fn new_mgr() -> UniquePtr; fn new_mgr_with_path(path: &String) -> UniquePtr; - fn get_modules(&self) -> Vec; + fn get_modules(self: &Mgr) -> Vec; + fn get_module(self: Pin<&mut Mgr>, name: &String) -> UniquePtr; + + fn get_name(self: &Module) -> String; + fn strip_text(self: Pin<&mut Module>) -> String; } } @@ -43,9 +49,12 @@ mod tests { 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()); + let mut 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"))); + + let mut module = mgr.pin_mut().get_module(&String::from("KJVdummy")); + assert!(module.pin_mut().get_name() == "KJVdummy"); } #[test]