forc_debug/
error.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use crate::types::Instruction;
use dap::requests::Command;

pub type Result<T> = std::result::Result<T, Error>;

#[derive(Debug, thiserror::Error)]
pub enum Error {
    #[error(transparent)]
    ArgumentError(#[from] ArgumentError),

    #[error(transparent)]
    AdapterError(#[from] AdapterError),

    #[error("VM error: {0}")]
    VMError(String),

    #[error("Fuel Client error: {0}")]
    FuelClientError(String),

    #[error("Session error: {0}")]
    SessionError(String),

    #[error("I/O error")]
    IoError(std::io::Error),

    #[error("Json error")]
    JsonError(#[from] serde_json::Error),

    #[error("Server error: {0}")]
    DapServerError(#[from] dap::errors::ServerError),

    #[error("Readline error: {0}")]
    Readline(#[from] rustyline::error::ReadlineError),
}

#[derive(Debug, thiserror::Error)]
pub enum ArgumentError {
    #[error("Invalid argument: {0}")]
    Invalid(String),

    #[error("Not enough arguments, expected {expected} but got {got}")]
    NotEnough { expected: usize, got: usize },

    #[error("Too many arguments, expected {expected} but got {got}")]
    TooMany { expected: usize, got: usize },

    #[error("Invalid number format: {0}")]
    InvalidNumber(String),
}

#[derive(Debug, thiserror::Error)]
pub enum AdapterError {
    #[error("Unhandled command")]
    UnhandledCommand { command: Command },

    #[error("Missing command")]
    MissingCommand,

    #[error("Missing configuration")]
    MissingConfiguration,

    #[error("Missing source path argument")]
    MissingSourcePathArgument,

    #[error("Missing breakpoint location")]
    MissingBreakpointLocation,

    #[error("Missing source map")]
    MissingSourceMap { pc: Instruction },

    #[error("Unknown breakpoint")]
    UnknownBreakpoint { pc: Instruction },

    #[error("Build failed")]
    BuildFailed { reason: String },

    #[error("No active test executor")]
    NoActiveTestExecutor,

    #[error("Test execution failed")]
    TestExecutionFailed {
        #[from]
        source: anyhow::Error,
    },
}

impl ArgumentError {
    /// Ensures argument count falls within [min, max] range.
    pub fn ensure_arg_count(
        args: &[String],
        min: usize,
        max: usize,
    ) -> std::result::Result<(), ArgumentError> {
        let count = args.len();
        if count < min {
            Err(ArgumentError::NotEnough {
                expected: min,
                got: count,
            })
        } else if count > max {
            Err(ArgumentError::TooMany {
                expected: max,
                got: count,
            })
        } else {
            Ok(())
        }
    }
}