Krys4lide/crates/fsv/src/ssv.rs

84 lines
2.4 KiB
Rust

use thiserror::Error;
use fsv_sys::{get_library_path, Error as FsvError, SSVLibrary, SSVLibrary_Common, SUPPORTED_FSV_VERSIONS, V1_40_13, V1_40_14};
#[derive(Error, Debug)]
pub enum Error {
#[error(transparent)]
SysLibrary(#[from] FsvError)
}
/// Enum to hold the different versions of the SSV library
enum SsvLibraryVersion {
V1_40_13(SSVLibrary<V1_40_13>),
V1_40_14(SSVLibrary<V1_40_14>),
}
/// Struct to hold the SSV library and access its functions
pub struct SSV {
library: SsvLibraryVersion,
}
impl SSV {
fn new(version: SUPPORTED_FSV_VERSIONS) -> Result<Self, Error> {
let library = match version {
SUPPORTED_FSV_VERSIONS::V1_40_13 => {
let lib_path = get_library_path(&version);
let library = SSVLibrary::<V1_40_13>::new(&lib_path)?;
SsvLibraryVersion::V1_40_13(library)
},
SUPPORTED_FSV_VERSIONS::V1_40_14 => {
let lib_path = get_library_path(&version);
let library = SSVLibrary::<V1_40_14>::new(&lib_path)?;
SsvLibraryVersion::V1_40_14(library)
},
};
Ok(Self {
library,
})
}
/// # Initialize the SSV library
/// Implement: SSV_InitLIB2
pub fn init_library(&self, sesam_ini_path: &str) -> Result<(), Error> {
let sesam_ini_path = std::ffi::CString::new(sesam_ini_path).expect("CString::new failed");
match &self.library {
SsvLibraryVersion::V1_40_13(library) => {
let result = unsafe { library.ssv_init_lib2(sesam_ini_path.as_ptr()) }?;
println!("SSV_InitLIB2 result: {}", result);
},
SsvLibraryVersion::V1_40_14(library) => {
let result = unsafe { library.ssv_init_lib2(sesam_ini_path.as_ptr()) }?;
println!("SSV_InitLIB2 result: {}", result);
},
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use std::env;
use utils::config::load_config;
use super::*;
fn init() -> SSV {
load_config().unwrap();
SSV::new(SUPPORTED_FSV_VERSIONS::V1_40_13).expect("SSV::new failed")
}
#[test]
fn test_init_library() {
let lib = init();
let sesam_ini_path = env::var("SESAM_INI_PATH").expect("SESAM_INI_PATH must be set");
lib.init_library(&sesam_ini_path).expect("init_library failed");
}
#[test]
fn test_ssv() {
assert_eq!("ssv", "ssv");
}
}