Skip to main content

cli/
error.rs

1//! Shared crate error types.
2
3use std::fmt;
4use std::io;
5
6pub type Result<T> = std::result::Result<T, CliError>;
7
8#[derive(Debug)]
9pub enum CliError {
10    Io(io::Error),
11    InvalidConfiguration(String),
12    UnsupportedPackageManager(String),
13    UnsupportedCommand(String),
14    RunnerFailed { command: String, reason: String },
15}
16
17impl fmt::Display for CliError {
18    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
19        match self {
20            Self::Io(error) => write!(formatter, "{error}"),
21            Self::InvalidConfiguration(message) => {
22                write!(formatter, "Invalid Nest CLI configuration: {message}")
23            }
24            Self::UnsupportedPackageManager(name) => {
25                write!(formatter, "Package manager {name} is not managed.")
26            }
27            Self::UnsupportedCommand(command) => {
28                write!(formatter, "Unsupported Nest CLI command: {command}")
29            }
30            Self::RunnerFailed { command, reason } => {
31                write!(formatter, "Failed to execute command `{command}`: {reason}")
32            }
33        }
34    }
35}
36
37impl std::error::Error for CliError {
38    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
39        match self {
40            Self::Io(error) => Some(error),
41            _ => None,
42        }
43    }
44}
45
46impl From<io::Error> for CliError {
47    fn from(error: io::Error) -> Self {
48        Self::Io(error)
49    }
50}