Added better error checking and tests for appropriate failures.

This commit is contained in:
Patrick Marsee 2025-01-20 13:50:07 -05:00
parent 600fd0c276
commit fbf1a9214b
5 changed files with 444 additions and 143 deletions

View file

@ -6,19 +6,21 @@ use std::iter::Iterator;
#[derive(Debug)]
pub enum Error {
IndexError,
EndpointError,
OffsetError,
NeighborError,
IndexError(usize),
EndpointError(usize),
OffsetError(f32),
ConnectionError(usize),
StateError(f32),
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::IndexError => write!(f, "Nonexistent internal curve index"),
Self::EndpointError => write!(f, "Nonexistent endpoint index"),
Self::OffsetError => write!(f, "Invalid offset"),
Self::NeighborError => write!(f, "Invalid Neighbor"),
Self::IndexError(index) => write!(f, "Nonexistent sub-segment index: {index}."),
Self::EndpointError(endpoint) => write!(f, "Nonexistent endpoint index: {endpoint}."),
Self::OffsetError(offset) => write!(f, "Invalid offset: {offset}."),
Self::ConnectionError(endpoint) => write!(f, "Endpoint {endpoint} is not connected."),
Self::StateError(state) => write!(f, "Invalid state: {state}."),
}
}
}
@ -106,6 +108,9 @@ pub trait RailSegment {
/** Borrow the connector associated with the given endpoint. */
fn borrow_connection(&self, endpoint: usize) -> Result<&RailConnector, Error>;
/** Copy the connector associated with the given endpoint. */
fn get_connection(&self, endpoint: usize) -> Result<RailConnector, Error>;
/** Check if a connection is established on the given endpoint. */
fn check_connection(&self, endpoint: usize) -> Result<bool, Error>;
@ -132,5 +137,5 @@ pub trait RailSegment {
fn get_state(&self) -> f32;
/** Set the internal state float value. */
fn set_state(&mut self, value: f32);
fn set_state(&mut self, value: f32) -> Result<f32, Error>;
}