use crate::{LayerId, Path, Solve, SolveError, Trellis, TrellisError};
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Solved {
trellis: Trellis,
path: Path,
}
impl Solved {
pub fn path(&self) -> &Path {
&self.path
}
pub fn cost(&self) -> u32 {
self.path.cost
}
pub fn trellis(&self) -> &Trellis {
&self.trellis
}
#[allow(clippy::result_large_err)]
pub fn append(mut self, width: u32) -> Result<(Trellis, LayerId), (Solved, TrellisError)> {
match self.trellis.add_layer(width) {
Ok(id) => Ok((self.trellis, id)),
Err(e) => Err((self, e)),
}
}
pub fn reopen(self) -> Trellis {
self.trellis
}
}
impl Trellis {
pub fn solve<S: Solve>(self, solver: &S) -> Result<Solved, (Trellis, SolveError)> {
match solver.solve(&self) {
Ok(path) => Ok(Solved {
trellis: self,
path,
}),
Err(e) => Err((self, e)),
}
}
}