#![forbid(unsafe_code)]
pub mod layout;
pub mod svg;
use chematic_core::Molecule;
pub use layout::{Layout, Point, compute_layout};
pub use svg::render_svg;
pub fn depict_svg(mol: &Molecule) -> String {
let layout = compute_layout(mol);
render_svg(mol, &layout)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::layout::BOND_LEN;
use chematic_smiles::parse;
fn mol(smiles: &str) -> Molecule {
parse(smiles).unwrap_or_else(|e| panic!("Failed to parse '{}': {:?}", smiles, e))
}
#[test]
fn test_layout_benzene_six_distinct_coords() {
let m = mol("c1ccccc1");
assert_eq!(m.atom_count(), 6);
let layout = compute_layout(&m);
assert_eq!(layout.coords.len(), 6);
for i in 0..6 {
for j in (i + 1)..6 {
let d = layout.coords[i].dist(&layout.coords[j]);
assert!(
d > BOND_LEN / 2.0,
"Atoms {} and {} are too close: {:.2} < {:.2}",
i,
j,
d,
BOND_LEN / 2.0
);
}
}
}
#[test]
fn test_layout_single_atom() {
let m = mol("[C]");
assert_eq!(m.atom_count(), 1);
let layout = compute_layout(&m);
assert_eq!(layout.coords.len(), 1);
let p = layout.coords[0];
assert!((p.x).abs() < 1.0 && (p.y).abs() < 1.0, "Single atom not near origin: {:?}", p);
}
#[test]
fn test_layout_ethane_bond_length() {
let m = mol("CC");
assert_eq!(m.atom_count(), 2);
let layout = compute_layout(&m);
assert_eq!(layout.coords.len(), 2);
let d = layout.coords[0].dist(&layout.coords[1]);
let tolerance = BOND_LEN * 0.01;
assert!(
(d - BOND_LEN).abs() < tolerance,
"Ethane bond distance {:.4} != BOND_LEN {:.4} (±{:.4})",
d,
BOND_LEN,
tolerance
);
}
#[test]
fn test_layout_naphthalene_ten_coords() {
let m = mol("c1ccc2ccccc2c1");
assert_eq!(m.atom_count(), 10);
let layout = compute_layout(&m);
assert_eq!(layout.coords.len(), 10);
for i in 0..10 {
for j in (i + 1)..10 {
let d = layout.coords[i].dist(&layout.coords[j]);
assert!(
d > BOND_LEN / 2.0,
"Naphthalene atoms {} and {} too close: {:.2}",
i,
j,
d
);
}
}
let (min_x, min_y, max_x, max_y) = layout.bounding_box();
assert!(max_x - min_x < 6.0 * BOND_LEN, "Naphthalene too wide");
assert!(max_y - min_y < 6.0 * BOND_LEN, "Naphthalene too tall");
}
#[test]
fn test_layout_disconnected_fragments_no_overlap() {
let m = mol("CC.CC");
assert_eq!(m.atom_count(), 4);
let layout = compute_layout(&m);
for i in 0..2 {
for j in 2..4 {
let d = layout.coords[i].dist(&layout.coords[j]);
assert!(
d >= BOND_LEN,
"Atoms from different fragments too close: atoms {} and {}, dist {:.2}",
i,
j,
d
);
}
}
}
#[test]
fn test_svg_benzene_no_text() {
let m = mol("c1ccccc1");
let layout = compute_layout(&m);
let svg = render_svg(&m, &layout);
assert!(svg.contains("<svg"), "SVG must start with <svg");
assert!(svg.contains("<line"), "SVG must have bond lines");
assert!(!svg.contains("<text"), "Benzene SVG must have no atom text labels");
}
#[test]
fn test_svg_pyridine_contains_nitrogen_label() {
let m = mol("c1ccncc1");
let layout = compute_layout(&m);
let svg = render_svg(&m, &layout);
assert!(svg.contains("<text"), "Pyridine SVG must have a text label");
assert!(svg.contains('N'), "Pyridine SVG must contain 'N' label");
}
#[test]
fn test_svg_aspirin_non_empty() {
let m = mol("CC(=O)Oc1ccccc1C(=O)O");
let layout = compute_layout(&m);
let svg = render_svg(&m, &layout);
assert!(!svg.is_empty(), "Aspirin SVG must be non-empty");
assert!(svg.contains("<line"), "Aspirin SVG must contain line elements");
}
#[test]
fn test_svg_double_bond_two_lines() {
let m = mol("C=C");
let layout = compute_layout(&m);
let svg = render_svg(&m, &layout);
let count = svg.matches("<line").count();
assert!(count >= 2, "C=C SVG should have >= 2 <line elements, got {}", count);
}
#[test]
fn test_depict_svg_caffeine_valid() {
let m = mol("Cn1cnc2c1c(=O)n(c(=O)n2C)C");
let svg = depict_svg(&m);
assert!(svg.starts_with("<svg"), "Caffeine SVG must start with <svg");
assert!(svg.ends_with("</svg>"), "Caffeine SVG must end with </svg>");
}
#[test]
fn test_depict_svg_water_contains_o() {
let m = mol("[OH2]");
let svg = depict_svg(&m);
assert!(svg.contains('O'), "Water SVG must contain 'O'");
}
#[test]
fn test_depict_svg_single_carbon_no_label() {
let m = mol("C");
let svg = depict_svg(&m);
assert!(svg.starts_with("<svg"), "Single C SVG must start with <svg");
assert!(svg.ends_with("</svg>"), "Single C SVG must end with </svg>");
assert!(!svg.contains("<text"), "Single C SVG should have no text label");
}
}