diff --git a/rdkit-sys/tests/test_atoms.rs b/rdkit-sys/tests/test_atoms.rs index b46da68..f5d9d5c 100644 --- a/rdkit-sys/tests/test_atoms.rs +++ b/rdkit-sys/tests/test_atoms.rs @@ -1,7 +1,7 @@ #[test] fn test_atoms() { cxx::let_cxx_string!(smiles = "c1ccccc1CCCCCCCC"); - let romol = rdkit_sys::ro_mol_ffi::smiles_to_mol(&smiles).unwrap(); + let mut romol = rdkit_sys::ro_mol_ffi::smiles_to_mol(&smiles).unwrap(); let num_atoms = rdkit_sys::ro_mol_ffi::get_num_atoms(&romol, true); diff --git a/src/graphmol/rw_mol.rs b/src/graphmol/rw_mol.rs index 6467120..9c3b5f7 100644 --- a/src/graphmol/rw_mol.rs +++ b/src/graphmol/rw_mol.rs @@ -9,6 +9,14 @@ pub struct RWMol { pub(crate) ptr: SharedPtr, } +#[derive(Debug, PartialEq, thiserror::Error)] +pub enum RWMolError { + #[error("Could not convert smarts to RWMol (nullptr)")] + UnknownConversionError, + #[error("could not convert smarts to RWMol (exception)")] + ConversionException(String), +} + impl RWMol { pub fn from_mol_block( mol_block: &str, @@ -48,11 +56,20 @@ impl RWMol { ROMol { ptr } } - pub fn from_smarts(smarts: &str) -> Result> { + pub fn from_smarts(smarts: &str) -> Result { let_cxx_string!(smarts = smarts); - let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts)?; - Ok(RWMol { ptr }) + let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts); + match ptr { + Ok(ptr) => { + if ptr.is_null() { + Err(RWMolError::UnknownConversionError) + } else { + Ok(RWMol { ptr }) + } + } + Err(e) => Err(RWMolError::ConversionException(e.to_string())), + } } } diff --git a/tests/test_atom.rs b/tests/test_atom.rs index 356aebc..8e2deb8 100644 --- a/tests/test_atom.rs +++ b/tests/test_atom.rs @@ -1,6 +1,6 @@ #[test] fn test_atom() { - let romol = rdkit::ROMol::from_smiles("C").unwrap(); + let mut romol = rdkit::ROMol::from_smiles("C").unwrap(); let atom = romol.atom_with_idx(0); diff --git a/tests/test_graphmol.rs b/tests/test_graphmol.rs index df95351..b3d4275 100644 --- a/tests/test_graphmol.rs +++ b/tests/test_graphmol.rs @@ -1,7 +1,7 @@ use rdkit::{ detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, - MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters, - TautomerEnumerator, Uncharger, + MolSanitizeException, ROMol, ROMolError, RWMol, RWMolError, SmilesParserParams, + SubstructMatchParameters, TautomerEnumerator, Uncharger, }; #[test] @@ -294,3 +294,10 @@ fn test_building_rwmol_from_smarts() { let result = substruct_match(&ro_mol, &query_mol, &SubstructMatchParameters::default()); assert_eq!(result.len(), 0); } + +#[test] +fn test_building_rwmol_from_invalid_smarts() { + let smarts = "string"; + let rw_mol = RWMol::from_smarts(smarts); + assert_eq!(rw_mol.err(), Some(RWMolError::UnknownConversionError)) +}