Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion rdkit-sys/tests/test_atoms.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
23 changes: 20 additions & 3 deletions src/graphmol/rw_mol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ pub struct RWMol {
pub(crate) ptr: SharedPtr<rdkit_sys::rw_mol_ffi::RWMol>,
}

#[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,
Expand Down Expand Up @@ -48,11 +56,20 @@ impl RWMol {
ROMol { ptr }
}

pub fn from_smarts(smarts: &str) -> Result<Self, Box<dyn std::error::Error>> {
pub fn from_smarts(smarts: &str) -> Result<Self, RWMolError> {
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())),
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/test_atom.rs
Original file line number Diff line number Diff line change
@@ -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);

Expand Down
11 changes: 9 additions & 2 deletions tests/test_graphmol.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down Expand Up @@ -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))
}