use crate::MolHandle;
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn mol_with_atom_added(mol: &MolHandle, element_symbol: &str) -> Result<MolHandle, JsValue> {
let element = chematic_core::Element::from_symbol(element_symbol)
.ok_or_else(|| JsValue::from_str(&format!("unknown element: {element_symbol}")))?;
let atom = chematic_core::Atom::new(element);
let (new_mol, _) = mol.inner.with_atom_added(atom);
Ok(MolHandle {
inner: std::rc::Rc::new(new_mol),
})
}
#[wasm_bindgen]
pub fn mol_next_atom_idx(mol: &MolHandle) -> u32 {
mol.inner.atom_count() as u32
}
#[wasm_bindgen]
pub fn mol_with_bond_added(
mol: &MolHandle,
a: u32,
b: u32,
order: u32,
) -> Result<MolHandle, JsValue> {
let bond_order = match order {
2 => chematic_core::BondOrder::Double,
3 => chematic_core::BondOrder::Triple,
_ => chematic_core::BondOrder::Single,
};
let (new_mol, _bond_idx) = mol
.inner
.with_bond_added(
chematic_core::AtomIdx(a),
chematic_core::AtomIdx(b),
bond_order,
)
.map_err(|e| JsValue::from_str(&e.to_string()))?;
Ok(MolHandle {
inner: std::rc::Rc::new(new_mol),
})
}
#[wasm_bindgen]
pub fn mol_with_atom_charge(mol: &MolHandle, idx: u32, charge: i32) -> Result<MolHandle, JsValue> {
if idx as usize >= mol.inner.atom_count() {
return Err(JsValue::from_str(&format!(
"atom index {idx} out of range (molecule has {} atoms)",
mol.inner.atom_count()
)));
}
let new_mol = mol
.inner
.with_atom_charge(chematic_core::AtomIdx(idx), charge as i8);
Ok(MolHandle {
inner: std::rc::Rc::new(new_mol),
})
}
#[wasm_bindgen]
pub fn mol_with_atom_element(
mol: &MolHandle,
idx: u32,
element_symbol: &str,
) -> Result<MolHandle, JsValue> {
if idx as usize >= mol.inner.atom_count() {
return Err(JsValue::from_str(&format!(
"atom index {idx} out of range (molecule has {} atoms)",
mol.inner.atom_count()
)));
}
let el = chematic_core::Element::from_symbol(element_symbol)
.ok_or_else(|| JsValue::from_str(&format!("unknown element: {element_symbol}")))?;
let new_mol = mol.inner.with_atom_element(chematic_core::AtomIdx(idx), el);
Ok(MolHandle {
inner: std::rc::Rc::new(new_mol),
})
}
#[wasm_bindgen]
pub fn mol_with_atom_removed(mol: &MolHandle, idx: u32) -> Result<MolHandle, JsValue> {
if idx as usize >= mol.inner.atom_count() {
return Err(JsValue::from_str(&format!(
"atom index {idx} out of range (molecule has {} atoms)",
mol.inner.atom_count()
)));
}
let (new_mol, _remap) = mol.inner.with_atom_removed(chematic_core::AtomIdx(idx));
Ok(MolHandle {
inner: std::rc::Rc::new(new_mol),
})
}
#[wasm_bindgen]
pub fn mol_with_bond_removed(mol: &MolHandle, idx: u32) -> Result<MolHandle, JsValue> {
if idx as usize >= mol.inner.bond_count() {
return Err(JsValue::from_str(&format!(
"bond index {idx} out of range (molecule has {} bonds)",
mol.inner.bond_count()
)));
}
let new_mol = mol.inner.with_bond_removed(chematic_core::BondIdx(idx));
Ok(MolHandle {
inner: std::rc::Rc::new(new_mol),
})
}