1use crate::types::Instruction;
2use dap::requests::Command;
3
4pub type Result<T> = std::result::Result<T, Error>;
5
6#[derive(Debug, thiserror::Error)]
7pub enum Error {
8 #[error(transparent)]
9 ArgumentError(#[from] ArgumentError),
10
11 #[error(transparent)]
12 AdapterError(#[from] AdapterError),
13
14 #[error("VM error: {0}")]
15 VMError(String),
16
17 #[error("Fuel Client error: {0}")]
18 FuelClientError(String),
19
20 #[error("Session error: {0}")]
21 SessionError(String),
22
23 #[error("I/O error")]
24 IoError(#[from] std::io::Error),
25
26 #[error("ABI error: {0}")]
27 AbiError(String),
28
29 #[error("Json error")]
30 JsonError(#[from] serde_json::Error),
31
32 #[error("Server error: {0}")]
33 DapServerError(#[from] dap::errors::ServerError),
34
35 #[error("Readline error: {0}")]
36 Readline(#[from] rustyline::error::ReadlineError),
37}
38
39#[derive(Debug, thiserror::Error)]
40pub enum ArgumentError {
41 #[error("Invalid argument: {0}")]
42 Invalid(String),
43
44 #[error("Unknown command: {0}")]
45 UnknownCommand(String),
46
47 #[error("Not enough arguments, expected {expected} but got {got}")]
48 NotEnough { expected: usize, got: usize },
49
50 #[error("Too many arguments, expected {expected} but got {got}")]
51 TooMany { expected: usize, got: usize },
52
53 #[error("Invalid number format: {0}")]
54 InvalidNumber(String),
55}
56
57#[allow(clippy::large_enum_variant)]
58#[derive(Debug, thiserror::Error)]
59pub enum AdapterError {
60 #[error("Unhandled command")]
61 UnhandledCommand { command: Command },
62
63 #[error("Missing command")]
64 MissingCommand,
65
66 #[error("Missing configuration")]
67 MissingConfiguration,
68
69 #[error("Missing source path argument")]
70 MissingSourcePathArgument,
71
72 #[error("Missing breakpoint location")]
73 MissingBreakpointLocation,
74
75 #[error("Missing source map")]
76 MissingSourceMap { pc: Instruction },
77
78 #[error("Unknown breakpoint")]
79 UnknownBreakpoint { pc: Instruction },
80
81 #[error("Build failed")]
82 BuildFailed { reason: String },
83
84 #[error("No active test executor")]
85 NoActiveTestExecutor,
86
87 #[error("Test execution failed")]
88 TestExecutionFailed {
89 #[from]
90 source: anyhow::Error,
91 },
92}
93
94impl ArgumentError {
95 pub fn ensure_arg_count(
97 args: &[String],
98 min: usize,
99 max: usize,
100 ) -> std::result::Result<(), ArgumentError> {
101 let count = args.len();
102 if count < min {
103 Err(ArgumentError::NotEnough {
104 expected: min,
105 got: count,
106 })
107 } else if count > max {
108 Err(ArgumentError::TooMany {
109 expected: max,
110 got: count,
111 })
112 } else {
113 Ok(())
114 }
115 }
116}