#![forbid(unsafe_code)]
use chematic::chem::molecular_weight;
use crate::chem_env::mol_from_smiles;
use crate::search::Route;
pub fn step_balanced(target: &str, precursors: &[String]) -> bool {
let target_mw = mol_from_smiles(target)
.ok()
.map(|m| molecular_weight(&m))
.unwrap_or(0.0);
if target_mw == 0.0 {
return true;
}
let precursor_mw: f64 = precursors
.iter()
.filter_map(|s| mol_from_smiles(s).ok())
.map(|m| molecular_weight(&m))
.sum();
target_mw <= precursor_mw * 1.01
}
pub fn route_balanced(route: &Route) -> bool {
route
.steps
.iter()
.all(|s| step_balanced(&s.target, &s.precursors))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn halobenzene_to_bare_benzene_is_unbalanced() {
assert!(!step_balanced("Clc1ccccc1", &["c1ccccc1".to_string()]));
assert!(!step_balanced("Ic1ccccc1", &["c1ccccc1".to_string()]));
assert!(!step_balanced("Fc1ccccc1", &["c1ccccc1".to_string()]));
}
#[test]
fn halogen_swap_stays_balanced() {
assert!(step_balanced("Clc1ccccc1", &["Brc1ccccc1".to_string()]));
}
}