1use std::fmt;
4
5#[derive(Debug)]
6pub enum Error {
7 Parse {
9 map: String,
10 offset: usize,
11 expected: Vec<String>,
12 },
13 MapNotFound(String),
15 DependencyCycle(Vec<String>),
17 Unsupported(String),
20 MapLogic(String),
23 Model(String),
26 Io(String),
28}
29
30impl fmt::Display for Error {
31 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32 match self {
33 Error::Parse { map, offset, expected } => write!(
34 f,
35 "parse failed in {map} at offset {offset} (expected one of: {})",
36 expected.join(", ")
37 ),
38 Error::MapNotFound(name) => write!(f, "couldn't locate map {name:?}"),
39 Error::DependencyCycle(chain) => {
40 write!(f, "dependency cycle: {}", chain.join(" -> "))
41 }
42 Error::Unsupported(what) => write!(f, "unsupported: {what}"),
43 Error::MapLogic(what) => write!(f, "map logic error: {what}"),
44 Error::Model(what) => write!(f, "model error: {what}"),
45 Error::Io(what) => write!(f, "io error: {what}"),
46 }
47 }
48}
49
50impl std::error::Error for Error {}
51
52impl From<std::io::Error> for Error {
53 fn from(err: std::io::Error) -> Self {
54 Error::Io(err.to_string())
55 }
56}
57
58pub type Result<T> = std::result::Result<T, Error>;