Skip to main content

knowledge_base_crud/
error.rs

1use knowledge_base_validation::Diagnostic;
2use std::fmt;
3use std::io;
4use std::path::PathBuf;
5
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9    Read { path: PathBuf, source: io::Error },
10    ParseStatementBatch { path: PathBuf, source: serde_yaml::Error },
11    ParseReference { path: PathBuf, source: serde_yaml::Error },
12    ParseAllocation { path: PathBuf, source: serde_yaml::Error },
13    InvalidRequest(String),
14    ParseEntity { path: PathBuf, source: serde_yaml::Error },
15    InvalidRepository(String),
16    Edit { path: PathBuf, message: String },
17    Validation(Vec<Diagnostic>),
18    Write { path: PathBuf, source: io::Error },
19    ConcurrentChange(PathBuf),
20    Commit { message: String },
21}
22
23impl fmt::Display for Error {
24    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
25        match self {
26            Self::Read { path, source } => write!(formatter, "cannot read {}: {source}", path.display()),
27            Self::ParseStatementBatch { path, source } => {
28                write!(formatter, "cannot parse statement manifest {}: {source}", path.display())
29            }
30            Self::ParseReference { path, source } => write!(formatter, "cannot parse reference {}: {source}", path.display()),
31            Self::ParseAllocation { path, source } => write!(formatter, "cannot parse identifier allocation {}: {source}", path.display()),
32            Self::InvalidRequest(message) => write!(formatter, "invalid mutation request: {message}"),
33            Self::ParseEntity { path, source } => {
34                write!(formatter, "cannot parse entity {}: {source}", path.display())
35            }
36            Self::InvalidRepository(message) => write!(formatter, "cannot query knowledge base: {message}"),
37            Self::Edit { path, message } => {
38                write!(formatter, "cannot edit resource {}: {message}", path.display())
39            }
40            Self::Validation(diagnostics) => {
41                writeln!(formatter, "mutation would not produce a valid knowledge base:")?;
42                for (index, diagnostic) in diagnostics.iter().enumerate() {
43                    if index + 1 == diagnostics.len() {
44                        write!(formatter, "{diagnostic}")?;
45                    } else {
46                        writeln!(formatter, "{diagnostic}")?;
47                    }
48                }
49                Ok(())
50            }
51            Self::Write { path, source } => {
52                write!(formatter, "cannot write {}: {source}", path.display())
53            }
54            Self::ConcurrentChange(path) => {
55                write!(formatter, "resource changed while applying mutation: {}", path.display())
56            }
57            Self::Commit { message } => formatter.write_str(message),
58        }
59    }
60}
61
62impl std::error::Error for Error {
63    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
64        match self {
65            Self::Read { source, .. } | Self::Write { source, .. } => Some(source),
66            Self::ParseStatementBatch { source, .. } | Self::ParseReference { source, .. } | Self::ParseAllocation { source, .. } | Self::ParseEntity { source, .. } => {
67                Some(source)
68            }
69            Self::InvalidRequest(_) | Self::InvalidRepository(_) | Self::Edit { .. } | Self::Validation(_) | Self::ConcurrentChange(_) | Self::Commit { .. } => None,
70        }
71    }
72}