1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! Andaman client error handler

// derive macro that implements the From<anyhow::Error> trait

#[derive(Debug)]
pub enum ProjectError {
    NoManifest,
    InvalidManifest(String),
    Multiple(Vec<Self>),
    HclError(hcl::error::Error),
    Other(String),
}

impl From<hcl::error::Error> for ProjectError {
    fn from(e: hcl::error::Error) -> Self {
        ProjectError::HclError(e)
    }
}

impl From<anyhow::Error> for ProjectError {
    fn from(err: anyhow::Error) -> Self {
        ProjectError::Other(err.to_string())
    }
}

impl std::fmt::Display for ProjectError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ProjectError::NoManifest => write!(f, "No manifest found"),
            ProjectError::InvalidManifest(e) => write!(f, "Invalid manifest: {}", e),
            ProjectError::Other(msg) => write!(f, "{}", msg),
            ProjectError::HclError(e) => write!(
                f,
                "Error parsing HCL: {}{}",
                e.to_owned(),
                e.to_owned()
                    .location()
                    .map(|l| format!(" at {}:{}", l.line, l.col))
                    .unwrap_or_default()
            ),
            ProjectError::Multiple(errors) => {
                write!(f, "Multiple errors:")?;
                for error in errors {
                    write!(f, "\n - {}", error)?;
                }
                Ok(())
            }
        }
    }
}