use crate::point::Point;
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum PathBuildError {
#[error("paths must contain at least one point")]
Empty,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Path {
steps: Vec<Point>,
cost: usize,
}
impl Path {
pub fn from_steps(steps: Vec<Point>) -> Result<Self, PathBuildError> {
if steps.is_empty() {
return Err(PathBuildError::Empty);
}
let cost = steps.len().saturating_sub(1);
Ok(Self { steps, cost })
}
pub fn from_steps_with_cost(steps: Vec<Point>, cost: usize) -> Result<Self, PathBuildError> {
if steps.is_empty() {
return Err(PathBuildError::Empty);
}
Ok(Self { steps, cost })
}
#[must_use]
pub fn steps(&self) -> &[Point] {
&self.steps
}
#[must_use]
pub fn len(&self) -> usize {
self.steps.len()
}
#[must_use]
pub const fn is_empty(&self) -> bool {
false
}
#[must_use]
pub fn cost(&self) -> usize {
self.cost
}
#[must_use]
pub fn start(&self) -> Point {
self.steps[0]
}
#[must_use]
pub fn goal(&self) -> Point {
self.steps[self.steps.len() - 1]
}
}