From 945823370b0ac766f4a8d70876fbfbb031363fbe Mon Sep 17 00:00:00 2001 From: Greg Hellings Date: Tue, 25 Feb 2025 19:49:02 -0600 Subject: [PATCH] Add the ability to read type --- src/cxx/mod.rs | 1 + src/cxx/module.cc | 4 ++++ src/cxx/module.h | 2 ++ src/lib.rs | 2 +- src/main.rs | 33 +++++++++++++++++++++------------ src/module.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 13 deletions(-) diff --git a/src/cxx/mod.rs b/src/cxx/mod.rs index 1cd0c61..0577a76 100644 --- a/src/cxx/mod.rs +++ b/src/cxx/mod.rs @@ -16,6 +16,7 @@ pub mod ffi { fn decrement(self: Pin<&mut Module>); fn get_name(self: &Module) -> String; + fn get_type(self: &Module) -> String; fn increment(self: Pin<&mut Module>); fn is_skip_consecutive_links(self: &Module) -> bool; fn render_text(self: Pin<&mut Module>) -> String; diff --git a/src/cxx/module.cc b/src/cxx/module.cc index 3e3d920..57acc37 100644 --- a/src/cxx/module.cc +++ b/src/cxx/module.cc @@ -11,6 +11,10 @@ namespace sword_rs { return rust::String(this->module->getName()); } + rust::String Module::get_type() const { + return rust::String(this->module->getType()); + } + void Module::increment() { this->module->increment(); } diff --git a/src/cxx/module.h b/src/cxx/module.h index e6d4d0b..b3e383a 100644 --- a/src/cxx/module.h +++ b/src/cxx/module.h @@ -17,6 +17,8 @@ namespace sword_rs { rust::String get_name() const; + rust::String get_type() const; + void increment(); bool is_skip_consecutive_links() const; diff --git a/src/lib.rs b/src/lib.rs index e39426e..b137cae 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,3 @@ mod cxx; pub mod mgr; -mod module; +pub mod module; diff --git a/src/main.rs b/src/main.rs index 50d7516..98b2b7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,18 +5,27 @@ fn main() { let mut mgr = Mgr::new_with_format(Format::HTMLHref); for mod_name in mgr.get_modules() { let mut module = mgr.get_module(&mod_name); - println!("Module: {}", module.get_name()); - let random_number: i32 = rand::rng().random_range(1..=20); - let random_number_str = format!("John.3.{}", random_number); - module.set_key_text(&random_number_str); - module.set_skip_consecutive_links(true); - for i in 0..10 { - module.increment(); - println!( - "Text John 3:{} is \"{}\"", - random_number + i, - module.render_text() - ); + // module.get_type(); + println!( + "Module: {} which is a {}", + module.get_name(), + module.get_type().get_name() + ); + if module.get_type() == sword_rs::module::Type::BiblicalTexts { + let random_number: i32 = rand::rng().random_range(1..=20); + let random_number_str = format!("John.3.{}", random_number); + module.set_key_text(&random_number_str); + module.set_skip_consecutive_links(true); + for i in 0..10 { + println!( + "Text John 3:{} is \"{}\"", + random_number + i, + module.render_text() + ); + module.increment(); + } + } else { + println!("This is not a Bible"); } println!("=============================="); } diff --git a/src/module.rs b/src/module.rs index 8bca5bf..4cbaef7 100644 --- a/src/module.rs +++ b/src/module.rs @@ -1,6 +1,40 @@ use crate::cxx::ffi; use cxx::UniquePtr; +#[derive(PartialEq)] +pub enum Type { + Unknown, + BiblicalTexts, + Commentaries, + DailyDevotional, + GenericBooks, + Lexicons, +} + +impl Type { + pub fn new(type_str: &String) -> Self { + match type_str.as_str() { + "Biblical Texts" => Type::BiblicalTexts, + "Commentaries" => Type::Commentaries, + "Daily Devotional" => Type::DailyDevotional, + "Generic Books" => Type::GenericBooks, + "Lexicons / Dictionaries" => Type::Lexicons, + _ => Type::Unknown, + } + } + + pub fn get_name(&self) -> String { + match self { + Type::BiblicalTexts => String::from("Biblical Texts"), + Type::Commentaries => String::from("Commentaries"), + Type::DailyDevotional => String::from("Daily Devotional"), + Type::GenericBooks => String::from("Generic Books"), + Type::Lexicons => String::from("Lexicons / Dictionaries"), + Type::Unknown => String::from("Unknown"), + } + } +} + pub struct Module { module: UniquePtr, } @@ -18,6 +52,14 @@ impl Module { self.module.get_name() } + pub fn get_type(&self) -> Type { + // This line I will use to see what the C++ library returns + // for all module types. + // You can also check swmgr.cpp for the list + // println!("{}", self.module.get_type()); + Type::new(&self.module.get_type()) + } + pub fn increment(&mut self) { self.module.pin_mut().increment(); }