From d60d2ae57a1a33b93555e79f39fdd6d63ab10341 Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Mon, 4 Dec 2023 15:34:31 +0100 Subject: [PATCH 1/9] Add error handling to RWMol::from_smarts --- src/graphmol/rw_mol.rs | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/graphmol/rw_mol.rs b/src/graphmol/rw_mol.rs index 6467120..a940553 100644 --- a/src/graphmol/rw_mol.rs +++ b/src/graphmol/rw_mol.rs @@ -1,4 +1,5 @@ use std::fmt::Formatter; +use std::io::{Error, ErrorKind}; use cxx::{let_cxx_string, SharedPtr}; use rdkit_sys::*; @@ -48,11 +49,16 @@ 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 }) + if ptr.is_null() { + Err(Error::new(ErrorKind::InvalidData, "Invalid smart")) + } else { + Ok(RWMol { ptr }) + + } } } From d8d0b7d4b3b2311f9eb9554653dcf801ffe61c49 Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Mon, 4 Dec 2023 15:35:02 +0100 Subject: [PATCH 2/9] Add test for invalid input --- tests/test_graphmol.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_graphmol.rs b/tests/test_graphmol.rs index df95351..da300c6 100644 --- a/tests/test_graphmol.rs +++ b/tests/test_graphmol.rs @@ -1,3 +1,4 @@ +use std::io::ErrorKind; use rdkit::{ detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters, @@ -294,3 +295,11 @@ 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 e =RWMol::from_smarts(smarts).unwrap_err(); + assert_eq!(e.kind(), ErrorKind::InvalidData); + +} From d12879fd42cff31b1299187343a89a1d22b932a6 Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Wed, 6 Dec 2023 10:29:59 +0100 Subject: [PATCH 3/9] add empty SMARTS handling and facilitate errors --- src/graphmol/rw_mol.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/graphmol/rw_mol.rs b/src/graphmol/rw_mol.rs index a940553..8f88e66 100644 --- a/src/graphmol/rw_mol.rs +++ b/src/graphmol/rw_mol.rs @@ -1,5 +1,4 @@ use std::fmt::Formatter; -use std::io::{Error, ErrorKind}; use cxx::{let_cxx_string, SharedPtr}; use rdkit_sys::*; @@ -49,12 +48,13 @@ impl RWMol { ROMol { ptr } } - pub fn from_smarts(smarts: &str) -> Result { + pub fn from_smarts(smarts: &str) -> Result { + if smarts.is_empty(){ return Err("empty SMARTS"); } let_cxx_string!(smarts = smarts); - let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts)?; + let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts).unwrap(); if ptr.is_null() { - Err(Error::new(ErrorKind::InvalidData, "Invalid smart")) + Err("invalid SMARTS") } else { Ok(RWMol { ptr }) From f6da32a5838b980f60baaaac650fa2dc38b8fb75 Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Wed, 6 Dec 2023 10:30:54 +0100 Subject: [PATCH 4/9] =?UTF-8?q?Adapt=20error=20handling=20and=20add=20test?= =?UTF-8?q?s=20f=C3=BCr=20empty=20SMARTS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_graphmol.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/tests/test_graphmol.rs b/tests/test_graphmol.rs index da300c6..27720e8 100644 --- a/tests/test_graphmol.rs +++ b/tests/test_graphmol.rs @@ -1,4 +1,3 @@ -use std::io::ErrorKind; use rdkit::{ detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters, @@ -300,6 +299,14 @@ fn test_building_rwmol_from_smarts() { fn test_building_rwmol_from_invalid_smarts() { let smarts = "string"; let e =RWMol::from_smarts(smarts).unwrap_err(); - assert_eq!(e.kind(), ErrorKind::InvalidData); + assert_eq!(e, "invalid SMARTS"); } + +#[test] +fn test_building_rwmol_from_empty_smarts() { + let smarts = ""; + let e =RWMol::from_smarts(smarts).unwrap_err(); + assert_eq!(e, "empty SMARTS"); + +} \ No newline at end of file From 8eea60fb0109a48dcf724f75b750d37d9f07299e Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Thu, 7 Dec 2023 13:13:42 +0100 Subject: [PATCH 5/9] Add enum error handling --- src/graphmol/rw_mol.rs | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/graphmol/rw_mol.rs b/src/graphmol/rw_mol.rs index 8f88e66..1f0343e 100644 --- a/src/graphmol/rw_mol.rs +++ b/src/graphmol/rw_mol.rs @@ -9,6 +9,16 @@ 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), + #[error("Could not convert smarts to RWMol (empty smarts)")] + EmptyInputError +} + impl RWMol { pub fn from_mol_block( mol_block: &str, @@ -48,16 +58,20 @@ impl RWMol { ROMol { ptr } } - pub fn from_smarts(smarts: &str) -> Result { - if smarts.is_empty(){ return Err("empty SMARTS"); } + pub fn from_smarts(smarts: &str) -> Result { + if smarts.is_empty(){ return Err(RWMolError::EmptyInputError); } let_cxx_string!(smarts = smarts); - let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts).unwrap(); - if ptr.is_null() { - Err("invalid SMARTS") - } else { - 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())), } } } From a9c9e84ef93e3392d8f4ee8c228468f6216964ee Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Thu, 7 Dec 2023 13:14:25 +0100 Subject: [PATCH 6/9] Adapt tests to enum errors --- tests/test_graphmol.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/tests/test_graphmol.rs b/tests/test_graphmol.rs index 27720e8..07c659d 100644 --- a/tests/test_graphmol.rs +++ b/tests/test_graphmol.rs @@ -1,8 +1,4 @@ -use rdkit::{ - detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, - MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters, - TautomerEnumerator, Uncharger, -}; +use rdkit::{detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters, TautomerEnumerator, Uncharger, RWMolError}; #[test] fn test_rdmol() { @@ -298,15 +294,14 @@ fn test_building_rwmol_from_smarts() { #[test] fn test_building_rwmol_from_invalid_smarts() { let smarts = "string"; - let e =RWMol::from_smarts(smarts).unwrap_err(); - assert_eq!(e, "invalid SMARTS"); + let rw_mol =RWMol::from_smarts(smarts); + assert_eq!(rw_mol.err(), Some(RWMolError::UnknownConversionError)) } #[test] fn test_building_rwmol_from_empty_smarts() { let smarts = ""; - let e =RWMol::from_smarts(smarts).unwrap_err(); - assert_eq!(e, "empty SMARTS"); - + let rw_mol =RWMol::from_smarts(smarts); + assert_eq!(rw_mol.err(), Some(RWMolError::EmptyInputError)) } \ No newline at end of file From 27ded30e3d33d2a5cf7686edcb424b0363946c33 Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Thu, 7 Dec 2023 13:14:40 +0100 Subject: [PATCH 7/9] Make romol mut --- rdkit-sys/tests/test_atoms.rs | 2 +- tests/test_atom.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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/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); From 79d9b4ecf5d081a78ab73812396c073d42329250 Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Sat, 9 Dec 2023 09:40:51 +0100 Subject: [PATCH 8/9] Run linter --- src/graphmol/rw_mol.rs | 6 ++++-- tests/test_graphmol.rs | 13 ++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/graphmol/rw_mol.rs b/src/graphmol/rw_mol.rs index 1f0343e..6ba166c 100644 --- a/src/graphmol/rw_mol.rs +++ b/src/graphmol/rw_mol.rs @@ -16,7 +16,7 @@ pub enum RWMolError { #[error("could not convert smarts to RWMol (exception)")] ConversionException(String), #[error("Could not convert smarts to RWMol (empty smarts)")] - EmptyInputError + EmptyInputError, } impl RWMol { @@ -59,7 +59,9 @@ impl RWMol { } pub fn from_smarts(smarts: &str) -> Result { - if smarts.is_empty(){ return Err(RWMolError::EmptyInputError); } + if smarts.is_empty() { + return Err(RWMolError::EmptyInputError); + } let_cxx_string!(smarts = smarts); let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts); diff --git a/tests/test_graphmol.rs b/tests/test_graphmol.rs index 07c659d..7ecbde0 100644 --- a/tests/test_graphmol.rs +++ b/tests/test_graphmol.rs @@ -1,4 +1,8 @@ -use rdkit::{detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, MolSanitizeException, ROMol, ROMolError, RWMol, SmilesParserParams, SubstructMatchParameters, TautomerEnumerator, Uncharger, RWMolError}; +use rdkit::{ + detect_chemistry_problems, fragment_parent, substruct_match, CleanupParameters, + MolSanitizeException, ROMol, ROMolError, RWMol, RWMolError, SmilesParserParams, + SubstructMatchParameters, TautomerEnumerator, Uncharger, +}; #[test] fn test_rdmol() { @@ -294,14 +298,13 @@ fn test_building_rwmol_from_smarts() { #[test] fn test_building_rwmol_from_invalid_smarts() { let smarts = "string"; - let rw_mol =RWMol::from_smarts(smarts); + let rw_mol = RWMol::from_smarts(smarts); assert_eq!(rw_mol.err(), Some(RWMolError::UnknownConversionError)) - } #[test] fn test_building_rwmol_from_empty_smarts() { let smarts = ""; - let rw_mol =RWMol::from_smarts(smarts); + let rw_mol = RWMol::from_smarts(smarts); assert_eq!(rw_mol.err(), Some(RWMolError::EmptyInputError)) -} \ No newline at end of file +} From 5461f0ce27202127baac6be83ec32e19f92e9bad Mon Sep 17 00:00:00 2001 From: Alexandra Ebberg Date: Wed, 27 Dec 2023 11:07:20 +0100 Subject: [PATCH 9/9] Remove empty smarts error --- src/graphmol/rw_mol.rs | 5 ----- tests/test_graphmol.rs | 7 ------- 2 files changed, 12 deletions(-) diff --git a/src/graphmol/rw_mol.rs b/src/graphmol/rw_mol.rs index 6ba166c..9c3b5f7 100644 --- a/src/graphmol/rw_mol.rs +++ b/src/graphmol/rw_mol.rs @@ -15,8 +15,6 @@ pub enum RWMolError { UnknownConversionError, #[error("could not convert smarts to RWMol (exception)")] ConversionException(String), - #[error("Could not convert smarts to RWMol (empty smarts)")] - EmptyInputError, } impl RWMol { @@ -59,9 +57,6 @@ impl RWMol { } pub fn from_smarts(smarts: &str) -> Result { - if smarts.is_empty() { - return Err(RWMolError::EmptyInputError); - } let_cxx_string!(smarts = smarts); let ptr = rdkit_sys::rw_mol_ffi::smarts_to_mol(&smarts); diff --git a/tests/test_graphmol.rs b/tests/test_graphmol.rs index 7ecbde0..b3d4275 100644 --- a/tests/test_graphmol.rs +++ b/tests/test_graphmol.rs @@ -301,10 +301,3 @@ fn test_building_rwmol_from_invalid_smarts() { let rw_mol = RWMol::from_smarts(smarts); assert_eq!(rw_mol.err(), Some(RWMolError::UnknownConversionError)) } - -#[test] -fn test_building_rwmol_from_empty_smarts() { - let smarts = ""; - let rw_mol = RWMol::from_smarts(smarts); - assert_eq!(rw_mol.err(), Some(RWMolError::EmptyInputError)) -}