bun_cli/
error.rs

1use std::fmt;
2use std::io;
3
4/// Custom error types for the bun-cli application
5#[derive(Debug)]
6pub enum BunCliError {
7    /// IO error occurred
8    Io(io::Error),
9    /// Command execution failed
10    CommandFailed { command: String, message: String },
11    /// Invalid project name
12    InvalidProjectName(String),
13    /// Bun is not installed
14    BunNotInstalled,
15    /// Template copy failed
16    TemplateCopyFailed(String),
17    /// Dependency installation failed
18    DependencyFailed { dependency: String, message: String },
19}
20
21impl fmt::Display for BunCliError {
22    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
23        match self {
24            BunCliError::Io(err) => write!(f, "IO error: {}", err),
25            BunCliError::CommandFailed { command, message } => {
26                write!(f, "Command '{}' failed: {}", command, message)
27            }
28            BunCliError::InvalidProjectName(name) => {
29                write!(f, "Invalid project name '{}': must not be empty and should contain only valid characters", name)
30            }
31            BunCliError::BunNotInstalled => {
32                write!(f, "Bun is not installed. Please install Bun globally from https://bun.sh")
33            }
34            BunCliError::TemplateCopyFailed(message) => {
35                write!(f, "Failed to copy templates: {}", message)
36            }
37            BunCliError::DependencyFailed { dependency, message } => {
38                write!(f, "Failed to install dependency '{}': {}", dependency, message)
39            }
40        }
41    }
42}
43
44impl std::error::Error for BunCliError {
45    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
46        match self {
47            BunCliError::Io(err) => Some(err),
48            _ => None,
49        }
50    }
51}
52
53impl From<io::Error> for BunCliError {
54    fn from(err: io::Error) -> Self {
55        BunCliError::Io(err)
56    }
57}
58
59pub type Result<T> = std::result::Result<T, BunCliError>;