1use std::fmt;
2use std::io;
3use std::num::{ParseFloatError, ParseIntError};
4use std::str::Utf8Error;
5
6#[derive(Debug)]
7pub enum ProcError {
8 Io(io::Error),
9 Utf8(Utf8Error),
10 ParseInt(ParseIntError),
11 ParseFloat(ParseFloatError),
12 UnexpectedFormat(String),
13 ParseError,
14 InternalError,
15 PermissionDenied,
16 NotFound,
17}
18
19impl fmt::Display for ProcError {
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 match self {
22 ProcError::Io(e) => write!(f, "IO error: {}", e),
23 ProcError::Utf8(e) => write!(f, "UTF-8 error: {}", e),
24 ProcError::ParseInt(e) => write!(f, "Parse int error: {}", e),
25 ProcError::ParseFloat(e) => write!(f, "Parse float error: {}", e),
26 ProcError::UnexpectedFormat(s) => write!(f, "Unexpected format: {}", s),
27 ProcError::ParseError => write!(f, "Parse error"),
28 ProcError::InternalError => write!(f, "Internal error"),
29 ProcError::PermissionDenied => write!(f, "Permission denied"),
30 ProcError::NotFound => write!(f, "Not found"),
31 }
32 }
33}
34
35impl std::error::Error for ProcError {
36 fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37 match self {
38 ProcError::Io(e) => Some(e),
39 ProcError::Utf8(e) => Some(e),
40 ProcError::ParseInt(e) => Some(e),
41 ProcError::ParseFloat(e) => Some(e),
42 _ => None,
43 }
44 }
45}
46
47impl From<io::Error> for ProcError {
48 fn from(err: io::Error) -> Self {
49 match err.kind() {
50 io::ErrorKind::PermissionDenied => ProcError::PermissionDenied,
51 io::ErrorKind::NotFound => ProcError::NotFound,
52 _ => ProcError::Io(err),
53 }
54 }
55}
56
57impl From<Utf8Error> for ProcError {
58 fn from(err: Utf8Error) -> Self {
59 ProcError::Utf8(err)
60 }
61}
62
63impl From<ParseIntError> for ProcError {
64 fn from(err: ParseIntError) -> Self {
65 ProcError::ParseInt(err)
66 }
67}
68
69impl From<ParseFloatError> for ProcError {
70 fn from(err: ParseFloatError) -> Self {
71 ProcError::ParseFloat(err)
72 }
73}
74
75pub type ProcResult<T> = Result<T, ProcError>;