darwin_kperf/framework/
error.rs1use core::{error, ffi::c_int, fmt};
4
5use darwin_kperf_sys::kperfdata;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum FrameworkErrorKind {
14 None,
16 InvalidArgument,
18 OutOfMemory,
20 Io,
22 BufferTooSmall,
24 CurrentSystemUnknown,
26 DatabasePathInvalid,
28 DatabaseNotFound,
30 DatabaseArchitectureUnsupported,
32 DatabaseVersionUnsupported,
34 DatabaseCorrupt,
36 EventNotFound,
38 ConflictingEvents,
40 AllCountersMustBeForced,
42 EventUnavailable,
44 CheckErrno,
46 Other(c_int),
48}
49
50impl FrameworkErrorKind {
51 const fn from_code(code: c_int) -> Self {
53 match code {
54 kperfdata::KPEP_CONFIG_ERROR_NONE => Self::None,
55 kperfdata::KPEP_CONFIG_ERROR_INVALID_ARGUMENT => Self::InvalidArgument,
56 kperfdata::KPEP_CONFIG_ERROR_OUT_OF_MEMORY => Self::OutOfMemory,
57 kperfdata::KPEP_CONFIG_ERROR_IO => Self::Io,
58 kperfdata::KPEP_CONFIG_ERROR_BUFFER_TOO_SMALL => Self::BufferTooSmall,
59 kperfdata::KPEP_CONFIG_ERROR_CUR_SYSTEM_UNKNOWN => Self::CurrentSystemUnknown,
60 kperfdata::KPEP_CONFIG_ERROR_DB_PATH_INVALID => Self::DatabasePathInvalid,
61 kperfdata::KPEP_CONFIG_ERROR_DB_NOT_FOUND => Self::DatabaseNotFound,
62 kperfdata::KPEP_CONFIG_ERROR_DB_ARCH_UNSUPPORTED => {
63 Self::DatabaseArchitectureUnsupported
64 }
65 kperfdata::KPEP_CONFIG_ERROR_DB_VERSION_UNSUPPORTED => Self::DatabaseVersionUnsupported,
66 kperfdata::KPEP_CONFIG_ERROR_DB_CORRUPT => Self::DatabaseCorrupt,
67 kperfdata::KPEP_CONFIG_ERROR_EVENT_NOT_FOUND => Self::EventNotFound,
68 kperfdata::KPEP_CONFIG_ERROR_CONFLICTING_EVENTS => Self::ConflictingEvents,
69 kperfdata::KPEP_CONFIG_ERROR_COUNTERS_NOT_FORCED => Self::AllCountersMustBeForced,
70 kperfdata::KPEP_CONFIG_ERROR_EVENT_UNAVAILABLE => Self::EventUnavailable,
71 kperfdata::KPEP_CONFIG_ERROR_ERRNO => Self::CheckErrno,
72 _ => Self::Other(code),
73 }
74 }
75
76 const fn as_str(self) -> &'static str {
77 match self {
78 Self::None => "success",
79 Self::InvalidArgument => "invalid argument",
80 Self::OutOfMemory => "out of memory",
81 Self::Io => "I/O error",
82 Self::BufferTooSmall => "buffer too small",
83 Self::CurrentSystemUnknown => "current system unknown",
84 Self::DatabasePathInvalid => "database path invalid",
85 Self::DatabaseNotFound => "database not found",
86 Self::DatabaseArchitectureUnsupported => "database architecture unsupported",
87 Self::DatabaseVersionUnsupported => "database version unsupported",
88 Self::DatabaseCorrupt => "database corrupt",
89 Self::EventNotFound => "event not found",
90 Self::ConflictingEvents => "conflicting events",
91 Self::AllCountersMustBeForced => "all counters must be forced",
92 Self::EventUnavailable => "event unavailable",
93 Self::CheckErrno => "check errno",
94 Self::Other(_) => "unknown error",
95 }
96 }
97}
98
99impl fmt::Display for FrameworkErrorKind {
100 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
101 fmt.write_str(self.as_str())
102 }
103}
104
105pub struct FrameworkError {
110 code: c_int,
111 kind: FrameworkErrorKind,
112}
113
114impl FrameworkError {
115 #[must_use]
117 pub const fn from_code(code: c_int) -> Self {
118 Self {
119 code,
120 kind: FrameworkErrorKind::from_code(code),
121 }
122 }
123
124 #[must_use]
126 pub const fn kind(&self) -> FrameworkErrorKind {
127 self.kind
128 }
129}
130
131impl fmt::Debug for FrameworkError {
132 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
133 fmt.debug_struct("Error")
134 .field("kind", &self.kind)
135 .finish_non_exhaustive()
136 }
137}
138
139impl fmt::Display for FrameworkError {
140 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
141 write!(fmt, "kpep error {}: {}", self.code, self.kind)
142 }
143}
144
145impl error::Error for FrameworkError {}