use std::fmt;
use crate::{AtomIndex, MoleculeValidationError};
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct AtomConnection {
pub from: AtomIndex,
pub to: AtomIndex,
pub order: Option<u8>,
}
impl AtomConnection {
pub fn new(
from: AtomIndex,
to: AtomIndex,
order: Option<u8>,
) -> Result<Self, MoleculeValidationError> {
if from == to {
return Err(MoleculeValidationError::SelfConnection { index: from.get() });
}
if order == Some(0) {
return Err(MoleculeValidationError::ZeroConnectionOrder);
}
Ok(Self { from, to, order })
}
pub(crate) fn validate_indices(
self,
atom_count: usize,
) -> Result<Self, MoleculeValidationError> {
for index in [self.from, self.to] {
if index.get() >= atom_count {
return Err(MoleculeValidationError::InvalidConnectionIndex {
index: index.get(),
atom_count,
});
}
}
Ok(self)
}
}
impl fmt::Display for AtomConnection {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.order {
Some(order) => write!(formatter, "{}-{}({order})", self.from, self.to),
None => write!(formatter, "{}-{}", self.from, self.to),
}
}
}