rdk_sys/bindings/
inchi.rs

1#[cxx::bridge(namespace = "RDKit")]
2pub mod ffi {
3    unsafe extern "C++" {
4        include!("cpp/include/inchi.h");
5        include!("cpp/include/ro_mol.h");
6
7        pub type ROMol = crate::ro_mol_ffi::ROMol;
8
9        pub fn mol_to_inchi(mol: SharedPtr<ROMol>) -> String;
10        pub fn inchi_to_mol(inchi: &CxxString, sanitize: bool, remove_hydrogens: bool) -> Result<SharedPtr<ROMol>>;
11        pub fn inchi_to_inchi_key(inchi: &CxxString) -> Result<String>;
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use crate::ro_mol_ffi;
18    use crate::inchi_ffi;
19
20    #[test]
21    fn test_mol_to_inchi() {
22        cxx::let_cxx_string!(smile = "C");
23        let romol = ro_mol_ffi::smiles_to_mol(&smile).unwrap();
24        let inchi = inchi_ffi::mol_to_inchi(romol);
25        assert_eq!(inchi, "InChI=1S/CH4/h1H4");
26    }
27
28    #[test]
29    fn test_good_inchi_to_mol() {
30        cxx::let_cxx_string!(inchi = "InChI=1S/C2H6/c1-2/h1-2H3");
31        let romol = inchi_ffi::inchi_to_mol(&inchi, true, true).unwrap();
32        assert!(!romol.is_null());
33        assert_eq!(inchi_ffi::mol_to_inchi(romol.clone()), "InChI=1S/C2H6/c1-2/h1-2H3");
34        assert_eq!(ro_mol_ffi::mol_to_smiles(romol), "CC");
35    }
36
37    #[test]
38    fn test_bad_inchi_to_mol() {
39        cxx::let_cxx_string!(bad_inchi = "asd");
40        let romol = inchi_ffi::inchi_to_mol(&bad_inchi, true, true);
41        assert!(romol.is_ok());
42        assert!(romol.unwrap().is_null());
43    }
44
45    #[test]
46    fn test_good_inchi_to_inchi_key() {
47        cxx::let_cxx_string!(inchi = "InChI=1S/CH4/h1H4");
48        let inchi_key = inchi_ffi::inchi_to_inchi_key(&inchi).unwrap();
49        assert_eq!(inchi_key, "VNWKTOKETHGBQD-UHFFFAOYSA-N");
50    }
51
52    #[test]
53    fn test_bad_inchi_to_inchi_key() {
54        cxx::let_cxx_string!(bad_inchi = "asd");
55        let inchi_key = inchi_ffi::inchi_to_inchi_key(&bad_inchi);
56        assert!(inchi_key.is_ok());
57        assert!(inchi_key.unwrap().is_empty());
58    }
59}