1use thiserror::Error;
6
7#[derive(Error, Debug)]
9pub enum ProcError {
10 #[error("No process found matching '{0}'\n Try: proc list to list all processes")]
12 ProcessNotFound(String),
13
14 #[error("No process listening on port {0}\n Try: proc ports")]
16 PortNotFound(u16),
17
18 #[error("Permission denied for PID {0}\n Try: sudo proc <command>")]
20 PermissionDenied(u32),
21
22 #[error("Invalid input: {0}")]
24 InvalidInput(String),
25
26 #[error("System error: {0}")]
28 SystemError(String),
29
30 #[error("Operation timed out: {0}")]
32 Timeout(String),
33
34 #[error("Failed to parse: {0}")]
36 ParseError(String),
37
38 #[error("Not supported on this platform: {0}")]
40 NotSupported(String),
41
42 #[error("Process {0} is no longer running")]
44 ProcessGone(u32),
45
46 #[error("Signal failed: {0}")]
48 SignalError(String),
49}
50
51impl From<std::io::Error> for ProcError {
52 fn from(err: std::io::Error) -> Self {
53 match err.kind() {
54 std::io::ErrorKind::PermissionDenied => ProcError::PermissionDenied(0),
55 std::io::ErrorKind::NotFound => ProcError::ProcessNotFound("unknown".to_string()),
56 _ => ProcError::SystemError(err.to_string()),
57 }
58 }
59}
60
61impl From<serde_json::Error> for ProcError {
62 fn from(err: serde_json::Error) -> Self {
63 ProcError::ParseError(err.to_string())
64 }
65}
66
67impl From<regex::Error> for ProcError {
68 fn from(err: regex::Error) -> Self {
69 ProcError::InvalidInput(format!("Invalid pattern: {}", err))
70 }
71}
72
73impl From<dialoguer::Error> for ProcError {
74 fn from(err: dialoguer::Error) -> Self {
75 ProcError::SystemError(format!("Dialog error: {}", err))
76 }
77}
78
79pub type Result<T> = std::result::Result<T, ProcError>;
81
82#[derive(Debug, Clone, Copy)]
84pub enum ExitCode {
85 Success = 0,
87 GeneralError = 1,
89 NotFound = 2,
91 PermissionDenied = 3,
93 InvalidInput = 4,
95}
96
97impl From<&ProcError> for ExitCode {
98 fn from(err: &ProcError) -> Self {
99 match err {
100 ProcError::ProcessNotFound(_) | ProcError::PortNotFound(_) => ExitCode::NotFound,
101 ProcError::PermissionDenied(_) => ExitCode::PermissionDenied,
102 ProcError::InvalidInput(_) => ExitCode::InvalidInput,
103 _ => ExitCode::GeneralError,
104 }
105 }
106}