Skip to main content

molpack/
error.rs

1use molrs::types::F;
2use std::fmt;
3
4#[derive(Debug, Clone)]
5pub enum PackError {
6    /// Molecules could not satisfy constraints even without distance tolerances.
7    ConstraintsFailed(String),
8    /// Maximum iterations reached without convergence.
9    MaxIterations,
10    /// No molecules were provided.
11    NoTargets,
12    /// A molecule has no atoms.
13    EmptyMolecule(usize),
14    /// A restraint declared a periodic box whose `max - min` is
15    /// non-positive on at least one axis.
16    InvalidPBCBox { min: [F; 3], max: [F; 3] },
17    /// Two or more restraints declared periodic boxes with different
18    /// bounds or different per-axis periodicity flags. Only one periodic
19    /// box is allowed per packing run.
20    ConflictingPeriodicBoxes {
21        first: ([F; 3], [F; 3], [bool; 3]),
22        second: ([F; 3], [F; 3], [bool; 3]),
23    },
24}
25
26impl fmt::Display for PackError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            PackError::ConstraintsFailed(msg) => {
30                write!(f, "Packmol failed to satisfy constraints: {msg}")
31            }
32            PackError::MaxIterations => {
33                write!(f, "Maximum iterations reached without convergence")
34            }
35            PackError::NoTargets => write!(f, "No targets provided"),
36            PackError::EmptyMolecule(i) => write!(f, "Target {i} has no atoms"),
37            PackError::InvalidPBCBox { min, max } => write!(
38                f,
39                "Invalid PBC box: min={:?}, max={:?} (all max-min components must be > 0)",
40                min, max
41            ),
42            PackError::ConflictingPeriodicBoxes { first, second } => write!(
43                f,
44                "Conflicting periodic boxes declared by restraints: {first:?} vs {second:?}. \
45                 At most one periodic InsideBoxRestraint is allowed per packing run."
46            ),
47        }
48    }
49}
50
51impl std::error::Error for PackError {}