use std::fmt::{Debug, Display, Formatter};
#[derive(Clone, Hash, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
IncompatibleBoundary(String, String),
IndexConversion {
source_type: String,
target_type: String,
value: String,
},
IndexOverflow { index_type: String, value: String },
VerticesContainerFull { attempted: usize, maximum: usize },
ResourcePoolFull { attempted: usize, maximum: usize },
InvalidGeometry(String),
InvalidShell {
reason: String,
surface_count: usize,
},
InvalidRing { reason: String, vertex_count: usize },
InvalidLineString { reason: String, vertex_count: usize },
InvalidReference {
element_type: String,
index: usize,
max_index: usize,
},
InvalidThemeName { theme_type: String, theme: String },
InvalidGeometryType { expected: String, found: String },
IncompleteGeometry(String),
UnsupportedVersion(String, String),
InvalidCityObjectType(String),
InvalidJson(String),
MissingVersion,
UnsupportedCityJSONVersion(String),
Import(String),
}
pub type Result<T> = std::result::Result<T, Error>;
impl Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
Error::IncompatibleBoundary(source_boundarytype, target_boundarytype) => {
write!(
f,
"cannot convert a {source_boundarytype} to a {target_boundarytype}"
)
}
Error::IndexConversion {
source_type,
target_type,
value,
} => write!(
f,
"failed to convert index from {source_type} to {target_type}: value {value}"
),
Error::IndexOverflow { index_type, value } => {
write!(f, "index overflow for {index_type}: value {value}")
}
Error::VerticesContainerFull { attempted, maximum } => write!(
f,
"attempted to store {attempted} vertices in a container with capacity {maximum}"
),
Error::ResourcePoolFull { attempted, maximum } => write!(
f,
"attempted to store {attempted} resources in a pool with maximum {maximum} slots"
),
Error::InvalidGeometry(msg) => write!(f, "{msg}"),
Error::InvalidShell {
reason,
surface_count,
} => write!(
f,
"Invalid shell: {reason} (surface count: {surface_count})"
),
Error::InvalidRing {
reason,
vertex_count,
} => write!(f, "Invalid ring: {reason} (vertex count: {vertex_count})"),
Error::InvalidLineString {
reason,
vertex_count,
} => write!(
f,
"Invalid linestring: {reason} (vertex count: {vertex_count})"
),
Error::InvalidReference {
element_type,
index,
max_index,
} => write!(
f,
"Invalid {element_type} index: {index} (max: {max_index})"
),
Error::InvalidThemeName { theme_type, theme } => {
write!(f, "Invalid {theme_type} theme name: {theme}")
}
Error::InvalidGeometryType { expected, found } => {
write!(
f,
"Invalid geometry type: expected {expected}, found {found}"
)
}
Error::IncompleteGeometry(msg) => write!(f, "Incomplete geometry: {msg}"),
Error::UnsupportedVersion(v, supported) => {
write!(
f,
"the CityJSON version should be one of {supported}, but got {v}"
)
}
Error::InvalidCityObjectType(v) => write!(f, "invalid CityObject type: {v}"),
Error::InvalidJson(msg) => write!(f, "Invalid JSON: {msg}"),
Error::MissingVersion => write!(f, "Missing 'version' field in CityJSON document"),
Error::UnsupportedCityJSONVersion(version) => {
write!(f, "Unsupported CityJSON version: {version}")
}
Error::Import(msg) => write!(f, "Import error: {msg}"),
}
}
}
impl Debug for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
write!(f, "{self}")
}
}
impl std::error::Error for Error {}
impl From<std::io::Error> for Error {
fn from(value: std::io::Error) -> Self {
Error::Import(value.to_string())
}
}