security_framework/
base.rs

1//! Support types for other modules.
2
3use core_foundation::string::CFString;
4use core_foundation_sys::base::OSStatus;
5use std::num::NonZeroI32;
6use std::{error, fmt, result};
7
8/// A `Result` type commonly returned by functions.
9pub type Result<T, E = Error> = result::Result<T, E>;
10
11/// A Security Framework error.
12#[derive(Copy, Clone)]
13pub struct Error(NonZeroI32);
14
15impl fmt::Debug for Error {
16    #[cold]
17    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
18        let mut builder = fmt.debug_struct("Error");
19        builder.field("code", &self.0);
20        if let Some(message) = self.message() {
21            builder.field("message", &message);
22        }
23        builder.finish()
24    }
25}
26
27impl Error {
28    /// Creates a new `Error` from a status code.
29    /// The code must not be zero
30    #[inline]
31    #[must_use]
32    pub fn from_code(code: OSStatus) -> Self {
33        Self(NonZeroI32::new(code).unwrap_or_else(|| NonZeroI32::new(1).unwrap()))
34    }
35
36    /// Returns a string describing the current error, if available.
37    #[inline(always)]
38    #[must_use]
39    pub fn message(self) -> Option<String> {
40        self.inner_message()
41    }
42
43    #[cold]
44    fn inner_message(self) -> Option<String> {
45        use core_foundation::base::TCFType;
46        use security_framework_sys::base::SecCopyErrorMessageString;
47        use std::ptr;
48
49        unsafe {
50            let s = SecCopyErrorMessageString(self.code(), ptr::null_mut());
51            if s.is_null() {
52                None
53            } else {
54                Some(CFString::wrap_under_create_rule(s).to_string())
55            }
56        }
57    }
58
59    /// Returns the code of the current error.
60    #[inline(always)]
61    #[must_use]
62    pub const fn code(self) -> OSStatus {
63        self.0.get() as _
64    }
65}
66
67impl From<OSStatus> for Error {
68    #[inline(always)]
69    fn from(code: OSStatus) -> Self {
70        Self::from_code(code)
71    }
72}
73
74impl fmt::Display for Error {
75    #[cold]
76    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
77        if let Some(message) = self.message() {
78            write!(fmt, "{message}")
79        } else {
80            write!(fmt, "error code {}", self.code())
81        }
82    }
83}
84
85impl error::Error for Error {}