Rust-friendly binding for Versification

This commit is contained in:
Greg Hellings
2025-06-29 00:22:01 -05:00
parent 0ec0a0c7ac
commit 86ae509dd0
2 changed files with 77 additions and 0 deletions
+1
View File
@@ -2,3 +2,4 @@ mod cxx;
pub mod mgr;
pub mod module;
pub mod versekey;
pub mod versification;
+76
View File
@@ -0,0 +1,76 @@
/// Rust-friendly wrapper for the CXX versification bridge.
use crate::cxx::ffi;
use cxx::UniquePtr;
use std::fmt;
/// Represents a versification system (opaque pointer from C++).
pub struct VersificationSystem {
ptr: *const ffi::VersificationSystem,
}
impl fmt::Debug for VersificationSystem {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "VersificationSystem({:p})", self.ptr)
}
}
/// Safe Rust wrapper for the CXX Versification class.
pub struct Versification {
inner: UniquePtr<ffi::Versification>,
}
impl Versification {
/// Create a new Versification manager.
pub fn new() -> Self {
Self {
inner: ffi::new_versification(),
}
}
/// Get the list of available versification system names.
pub fn versifications(&self) -> Vec<String> {
self.inner.getVersifications()
}
/// Get a versification system by name.
///
/// Returns None if the system is not found.
pub fn get_system(&self, name: &str) -> Option<VersificationSystem> {
let mut name_owned = name.to_string();
let ptr = self.inner.getVersification(&mut name_owned);
if ptr.is_null() {
None
} else {
Some(VersificationSystem { ptr })
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn knows_versifications() {
let v = Versification::new();
let list = v.versifications();
assert!(list.contains(&"KJV".to_string()) || !list.is_empty());
}
#[test]
fn get_system_returns_some_for_known() {
let v = Versification::new();
let list = v.versifications();
if let Some(first) = list.first() {
let sys = v.get_system(first);
assert!(sys.is_some());
}
}
#[test]
fn get_system_returns_none_for_unknown() {
let v = Versification::new();
let sys = v.get_system("notarealversification");
assert!(sys.is_none());
}
}