use std::{fmt, io};
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
Io(io::Error),
Invalid {
offset: u64,
message: &'static str,
},
UnsupportedVersion(u32),
MissingTile(u32),
Limit(&'static str),
WrongFeatureType,
DifferentLibraries,
Query {
offset: usize,
message: String,
},
Geometry(&'static str),
#[cfg(feature = "way-checkpoints")]
WayCheckpoints(&'static str),
}
impl Error {
pub(crate) fn invalid(offset: u64, message: &'static str) -> Self {
Self::Invalid { offset, message }
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Io(e) => write!(f, "{e}"),
Self::Invalid { offset, message } => write!(f, "invalid GOL at {offset:#x}: {message}"),
Self::UnsupportedVersion(v) => {
write!(f, "unsupported GOL format version {v}; expected 2")
}
Self::MissingTile(tip) => write!(f, "tile {tip} is missing or not current"),
Self::Limit(s) => write!(f, "resource limit exceeded: {s}"),
Self::WrongFeatureType => {
f.write_str("operation is not available for this feature type")
}
Self::DifferentLibraries => f.write_str("features belong to different open libraries"),
Self::Query { offset, message } => write!(f, "query error at byte {offset}: {message}"),
Self::Geometry(message) => write!(f, "geometry error: {message}"),
#[cfg(feature = "way-checkpoints")]
Self::WayCheckpoints(message) => write!(f, "way checkpoints: {message}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Io(e) => Some(e),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(value: io::Error) -> Self {
Self::Io(value)
}
}