Rust-friendly binding for Versification
This commit is contained in:
@@ -2,3 +2,4 @@ mod cxx;
|
|||||||
pub mod mgr;
|
pub mod mgr;
|
||||||
pub mod module;
|
pub mod module;
|
||||||
pub mod versekey;
|
pub mod versekey;
|
||||||
|
pub mod versification;
|
||||||
|
|||||||
@@ -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());
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user