systemconfiguration/
error.rs1use std::{error::Error, fmt};
2
3use crate::{bridge, ffi};
4
5pub type Result<T> = std::result::Result<T, SystemConfigurationError>;
6
7#[derive(Clone, Debug, Eq, PartialEq)]
8pub struct SystemConfigurationError {
9 pub function: &'static str,
10 pub code: i32,
11 pub message: String,
12}
13
14impl SystemConfigurationError {
15 pub(crate) fn new(function: &'static str, code: i32, message: impl Into<String>) -> Self {
16 Self {
17 function,
18 code,
19 message: message.into(),
20 }
21 }
22
23 pub(crate) fn last(function: &'static str) -> Self {
24 let code = unsafe { ffi::core::sc_last_error_code() };
25 let message = bridge::take_optional_string(unsafe { ffi::core::sc_last_error_message() })
26 .unwrap_or_else(|| format!("SystemConfiguration returned error code {code}"));
27 Self::new(function, code, message)
28 }
29
30 pub(crate) fn null(function: &'static str, message: impl Into<String>) -> Self {
31 Self::new(function, 0, message)
32 }
33}
34
35impl fmt::Display for SystemConfigurationError {
36 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
37 write!(
38 f,
39 "{} failed (code {}): {}",
40 self.function, self.code, self.message
41 )
42 }
43}
44
45impl Error for SystemConfigurationError {}