Able to fetch a list of module names

This commit is contained in:
Greg Hellings
2025-02-24 20:55:06 -06:00
parent 1a6e51579b
commit 3e2cd701d0
8 changed files with 184 additions and 8 deletions
+18 -2
View File
@@ -1,8 +1,24 @@
#include "mgr.h"
#include <swmgr.h>
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<rust::String> Mgr::get_modules() const {
rust::Vec<rust::String> 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<Mgr> new_mgr() {
return std::unique_ptr<Mgr>(new Mgr());
}
std::unique_ptr<Mgr> new_mgr_with_path(const rust::String &path) {
std::string cpath(path);
return std::unique_ptr<Mgr>(new Mgr(cpath.c_str()));
}
+6 -4
View File
@@ -1,14 +1,16 @@
#pragma once
#include <memory>
#include <string>
#include <sword/swmgr.h>
#include "rust/cxx.h"
class Mgr {
class Mgr : public sword::SWMgr {
public:
Mgr();
private:
sword::SWMgr* mgr;
Mgr(const char*);
rust::Vec<rust::String> get_modules() const;
};
std::unique_ptr<Mgr> new_mgr();
std::unique_ptr<Mgr> new_mgr_with_path(const rust::String &path);
+6
View File
@@ -0,0 +1,6 @@
use sword_rs::mgr::{new_mgr, Mgr};
fn main() {
let mgr = new_mgr();
println!("{:?}", mgr.get_modules());
}
+42
View File
@@ -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<Mgr>;
fn new_mgr_with_path(path: &String) -> UniquePtr<Mgr>;
fn get_modules(&self) -> Vec<String>;
}
}
#[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);
}
}