pub type Result<T> = std::result::Result<T, Status>;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Status {
Success,
Failure,
InvalidParam,
OutOfMemory,
Timeout,
Interrupted,
PathInvalid,
NavMeshInvalid,
BufferTooSmall,
InProgress,
AlreadyExists,
NotFound,
PartialResult,
TileOutOfBounds,
AgentInvalid,
AgentNotFound,
CrowdError,
CorridorError,
NeighborhoodError,
DataCorrupted,
TileCacheError,
WrongMagic,
WrongVersion,
}
impl Status {
pub fn is_failure(&self) -> bool {
*self != Status::Success && *self != Status::PartialResult
}
pub fn is_success(&self) -> bool {
*self == Status::Success || *self == Status::PartialResult
}
pub fn to_result<T>(self, value: T) -> Result<T> {
if self.is_success() {
Ok(value)
} else {
Err(self)
}
}
}
impl std::error::Error for Status {}
impl std::fmt::Display for Status {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Status::Success => write!(f, "Success"),
Status::Failure => write!(f, "Failure"),
Status::InvalidParam => write!(f, "Invalid parameter"),
Status::OutOfMemory => write!(f, "Out of memory"),
Status::Timeout => write!(f, "Operation timed out"),
Status::Interrupted => write!(f, "Operation interrupted"),
Status::PathInvalid => write!(f, "Invalid path"),
Status::NavMeshInvalid => write!(f, "Invalid navigation mesh"),
Status::BufferTooSmall => write!(f, "Buffer too small"),
Status::InProgress => write!(f, "Operation in progress"),
Status::AlreadyExists => write!(f, "Value already exists"),
Status::NotFound => write!(f, "Value not found"),
Status::PartialResult => write!(f, "Partial result"),
Status::TileOutOfBounds => write!(f, "Tile out of bounds"),
Status::AgentInvalid => write!(f, "Invalid agent"),
Status::AgentNotFound => write!(f, "Agent not found"),
Status::CrowdError => write!(f, "Crowd error"),
Status::CorridorError => write!(f, "Path corridor error"),
Status::NeighborhoodError => write!(f, "Neighborhood error"),
Status::DataCorrupted => write!(f, "Data corrupted"),
Status::TileCacheError => write!(f, "Tile cache error"),
Status::WrongMagic => write!(f, "Wrong magic number"),
Status::WrongVersion => write!(f, "Wrong version"),
}
}
}
impl From<std::io::Error> for Status {
fn from(_: std::io::Error) -> Self {
Status::Failure
}
}
#[macro_export]
macro_rules! dtry {
($expr:expr) => {
match $expr {
Ok(val) => val,
Err(err) => return Err(err),
}
};
}
#[macro_export]
macro_rules! dtassert {
($expr:expr) => {
if !$expr {
return Err($crate::waymark::Status::Failure);
}
};
($expr:expr, $status:expr) => {
if !$expr {
return Err($status);
}
};
}
#[macro_export]
macro_rules! dtunwrap {
($expr:expr) => {
match $expr {
Some(val) => val,
None => return Err($crate::waymark::Status::InvalidParam),
}
};
($expr:expr, $status:expr) => {
match $expr {
Some(val) => val,
None => return Err($status),
}
};
}