circuitpython_deploy/
error.rs

1use std::io;
2use thiserror::Error;
3
4#[derive(Error, Debug)]
5pub enum CpdError {
6    #[error("IO error: {0}")]
7    Io(#[from] io::Error),
8
9    #[error("No CircuitPython boards detected.\n\nTroubleshooting:\n  • Ensure your CircuitPython board is connected via USB\n  • Check that the board appears as a removable drive\n  • Try pressing the RESET button on your board\n  • Use --board <path> to specify the board manually")]
10    BoardNotFound,
11
12    #[error("Multiple CircuitPython boards detected.\n\nPlease specify which board to use:\n  • Use --board <path> to specify manually, or\n  • Run without --yes flag for interactive selection")]
13    MultipleBoardsFound,
14
15    #[error("Invalid board path: {path}\n\nThe specified path does not exist or is not a valid CircuitPython board.\nUse --list-boards to see detected boards.")]
16    InvalidBoardPath { path: String },
17
18    #[error("Backup directory creation failed: {path}")]
19    BackupDirectoryCreationFailed { path: String },
20
21    #[error("File copy failed: {from} -> {to}")]
22    FileCopyFailed { from: String, to: String },
23
24    #[error("Invalid ignore pattern: {pattern}")]
25    InvalidIgnorePattern { pattern: String },
26
27    #[error("Configuration error: {message}")]
28    Configuration { message: String },
29
30    #[error("Permission denied: {path}")]
31    #[allow(dead_code)]
32    PermissionDenied { path: String },
33
34    #[error("Disk full or insufficient space")]
35    #[allow(dead_code)]
36    InsufficientSpace,
37
38    #[error("Deployment was cancelled by user")]
39    #[allow(dead_code)]
40    Cancelled,
41}
42
43pub type Result<T> = std::result::Result<T, CpdError>;
44
45impl CpdError {
46    pub fn is_recoverable(&self) -> bool {
47        match self {
48            CpdError::Io(_) => false,
49            CpdError::BoardNotFound => false,
50            CpdError::MultipleBoardsFound => false,
51            CpdError::InvalidBoardPath { .. } => false,
52            CpdError::BackupDirectoryCreationFailed { .. } => false,
53            CpdError::FileCopyFailed { .. } => true, // Can continue with other files
54            CpdError::InvalidIgnorePattern { .. } => true,
55            CpdError::Configuration { .. } => false,
56            CpdError::PermissionDenied { .. } => true,
57            CpdError::InsufficientSpace => false,
58            CpdError::Cancelled => false,
59        }
60    }
61}