feat: Création de la crate fsv, couche de haut niveau pour l'usage des librairies FSV

This commit is contained in:
Florian Briand 2024-10-01 19:13:03 +02:00
parent add40f32c5
commit 3c1e691cb8
Signed by: florian_briand
GPG Key ID: CC981B9E6B98E70B
5 changed files with 104 additions and 0 deletions

View File

@ -1963,6 +1963,15 @@ dependencies = [
"libc", "libc",
] ]
[[package]]
name = "fsv"
version = "0.1.0"
dependencies = [
"fsv-sys",
"thiserror",
"utils",
]
[[package]] [[package]]
name = "fsv-sys" name = "fsv-sys"
version = "0.1.0" version = "0.1.0"

View File

@ -4,6 +4,7 @@ members = [
"crates/backend", "crates/backend",
"crates/desktop", "crates/desktop",
"crates/sesam-vitale", "crates/sesam-vitale",
"crates/fsv",
"crates/fsv-sys", "crates/fsv-sys",
"crates/utils", "crates/utils",
"migration", "migration",

10
crates/fsv/Cargo.toml Normal file
View File

@ -0,0 +1,10 @@
[package]
name = "fsv"
version = "0.1.0"
edition = "2021"
[dependencies]
thiserror = "1.0.64"
fsv-sys = { path = "../fsv-sys" }
utils = { path = "../utils" }

1
crates/fsv/src/lib.rs Normal file
View File

@ -0,0 +1 @@
mod ssv;

83
crates/fsv/src/ssv.rs Normal file
View File

@ -0,0 +1,83 @@
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");
}
}