liblingo/util/
errors.rs

1use std::error::Error;
2use std::fmt::{Display, Formatter};
3use std::path::PathBuf;
4use std::process::{Command, ExitStatus};
5
6use std::sync::Arc;
7
8pub type AnyError = dyn Error + Send + Sync;
9pub type BuildResult = Result<(), Box<AnyError>>;
10
11#[derive(Debug)]
12pub enum LingoError {
13    Shared(Arc<AnyError>),
14    CommandFailed(Command, ExitStatus),
15    UnknownAppNames(Vec<String>),
16    InvalidProjectLocation(PathBuf),
17    UseWestBuildToBuildApp,
18    InvalidMainReactor,
19    NoLibraryInLingoToml(String),
20    LingoVersionMismatch(String),
21}
22
23impl Display for LingoError {
24    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
25        match self {
26            LingoError::Shared(err) => {
27                write!(f, "{}", err)
28            }
29            LingoError::CommandFailed(command, status) => {
30                write!(f, "Command exited with status {}: {:?}", status, command)
31            }
32            LingoError::UnknownAppNames(names) => {
33                write!(f, "Unknown app names: {}", names.join(", "))
34            }
35            LingoError::InvalidProjectLocation(path) => {
36                write!(f, "Cannot initialize repository in {}", path.display())
37            }
38            LingoError::UseWestBuildToBuildApp => {
39                write!(f, "Use `west lf-build` to build and run Zephyr programs.")
40            }
41            LingoError::InvalidMainReactor => {
42                write!(
43                    f,
44                    "Not a valid path path to a file that contains a main reactor"
45                )
46            }
47            LingoError::NoLibraryInLingoToml(path) => {
48                write!(
49                    f,
50                    "A dependency was specified that doesn't export a library see {path}"
51                )
52            }
53            LingoError::LingoVersionMismatch(message) => {
54                write!(
55                    f,
56                    "Version specified in Lingo.toml doesn't match the version in the location {message}"
57                )
58            }
59        }
60    }
61}
62
63impl Error for LingoError {}