From be7d2873e60aa6bae1156ec11baec6a98d634223 Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Tue, 25 Feb 2025 10:46:07 -0600 Subject: [PATCH] Now with the ability to ask for formtted text --- build.rs | 1 + src/cxx/mgr.cc | 44 +++++++++++++++++++++++++++----------------- src/cxx/mgr.h | 26 +++++++++++++++----------- src/cxx/mod.rs | 3 ++- src/cxx/module.cc | 19 +++++++++++++++++++ src/cxx/module.h | 29 ++++++++++++----------------- src/mgr.rs | 23 +++++++++++++++++++++++ 7 files changed, 99 insertions(+), 46 deletions(-) create mode 100644 src/cxx/module.cc diff --git a/build.rs b/build.rs index 4bb8a93..48d021a 100644 --- a/build.rs +++ b/build.rs @@ -5,6 +5,7 @@ fn main() { cxx_build::bridge("src/cxx/mod.rs") .file("src/cxx/mgr.cc") + .file("src/cxx/module.cc") .includes(deps.all_include_paths()) .cpp_link_stdlib("stdc++") .flag("-Os") diff --git a/src/cxx/mgr.cc b/src/cxx/mgr.cc index a52d175..9181ffc 100644 --- a/src/cxx/mgr.cc +++ b/src/cxx/mgr.cc @@ -1,24 +1,34 @@ #include "mgr.h" +#include -Mgr::Mgr() { } +namespace sword_rs { + 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) { } + // Set the desired output format + Mgr::Mgr(uint8_t format) : sword::SWMgr(0, 0, true, new sword::MarkupFilterMgr(format)) { } -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; -} + // 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) { } -std::unique_ptr new_mgr() { - return std::unique_ptr(new Mgr()); -} + 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_with_path(const rust::String &path) { - std::string cpath(path); - return std::unique_ptr(new Mgr(cpath.c_str())); + std::unique_ptr new_mgr() { + return std::unique_ptr(new Mgr()); + } + + std::unique_ptr new_mgr_format(uint8_t format) { + return std::unique_ptr(new Mgr(format)); + } + + 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 8d0b6c5..68361eb 100644 --- a/src/cxx/mgr.h +++ b/src/cxx/mgr.h @@ -7,15 +7,19 @@ #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()))); - }; -}; +namespace sword_rs { + class Mgr : public sword::SWMgr { + public: + Mgr(); + Mgr(uint8_t format); + 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(); -std::unique_ptr new_mgr_with_path(const rust::String &path); + std::unique_ptr new_mgr(); + std::unique_ptr new_mgr_format(uint8_t format); + std::unique_ptr new_mgr_with_path(const rust::String &path); +} diff --git a/src/cxx/mod.rs b/src/cxx/mod.rs index b80c16a..438e497 100644 --- a/src/cxx/mod.rs +++ b/src/cxx/mod.rs @@ -1,4 +1,4 @@ -#[cxx::bridge] +#[cxx::bridge(namespace = "sword_rs")] pub mod ffi { unsafe extern "C++" { include!("sword_rs/src/cxx/mgr.h"); @@ -7,6 +7,7 @@ pub mod ffi { type Mgr; fn new_mgr() -> UniquePtr; + fn new_mgr_format(format: u8) -> UniquePtr; fn new_mgr_with_path(path: &String) -> UniquePtr; fn get_modules(self: &Mgr) -> Vec; fn get_module(self: Pin<&mut Mgr>, name: &String) -> UniquePtr; diff --git a/src/cxx/module.cc b/src/cxx/module.cc new file mode 100644 index 0000000..b0df901 --- /dev/null +++ b/src/cxx/module.cc @@ -0,0 +1,19 @@ +#include "module.h" + +namespace sword_rs { + Module::Module(sword::SWModule* module) : module(module) { } + + rust::String Module::get_name() const { + return rust::String(this->module->getName()); + } + + rust::String Module::strip_text() { + return std::string( + (const char*) this->module->stripText() + ); + } + + void Module::set_key_text(const rust::String& keyText) { + this->module->setKeyText(std::string(keyText).c_str()); + } +} diff --git a/src/cxx/module.h b/src/cxx/module.h index 9dea142..6ce5572 100644 --- a/src/cxx/module.h +++ b/src/cxx/module.h @@ -1,27 +1,22 @@ #pragma once + #include #include #include "rust/cxx.h" -class Module { - private: - sword::SWModule* module; +namespace sword_rs { + class Module { + private: + sword::SWModule* module; - public: - Module(sword::SWModule* module) : module(module) { }; + public: + Module(sword::SWModule* module); - rust::String get_name() const { - return rust::String(this->module->getName()); - } + rust::String get_name() const; - rust::String strip_text() { - return std::string( - (const char*) this->module->stripText() - ); - } + rust::String strip_text(); - void set_key_text(const rust::String& keyText) { - this->module->setKeyText(std::string(keyText).c_str()); - } -}; + void set_key_text(const rust::String& keyText); + }; +} diff --git a/src/mgr.rs b/src/mgr.rs index e83ec7f..656ccd9 100644 --- a/src/mgr.rs +++ b/src/mgr.rs @@ -2,6 +2,23 @@ use crate::cxx::ffi; use crate::module::Module; use cxx::UniquePtr; +// The enum we are mimicking in sword has no name, therefore we cannot readily +// check ourselves against it +pub enum Format { + Unknown = 0, + Plain = 1, + ThML = 2, + GBF = 3, + HTML = 4, + HTMLHref = 5, + RTF = 6, + OSIS = 7, + WebIf = 8, + TEI = 9, + XHTML = 10, + Latex = 11, +} + pub struct Mgr { mgr: UniquePtr, } @@ -13,6 +30,12 @@ impl Mgr { } } + pub fn new_with_format(format: Format) -> Self { + Self { + mgr: ffi::new_mgr_format(format as u8), + } + } + pub fn new_with_path(path: &String) -> Self { Self { mgr: ffi::new_mgr_with_path(path),