safe_libc/
errno.rs

1//
2// Created:  Thu 16 Apr 2020 01:19:12 PM PDT
3// Modified: Fri 21 Nov 2025 01:17:44 PM PST
4//
5// Copyright (C) 2020 Robert Gill <rtgill82@gmail.com>
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to
9// deal in the Software without restriction, including without limitation the
10// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11// sell copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies of the Software, its documentation and marketing & publicity
16// materials, and acknowledgment shall be given in the documentation, materials
17// and software packages that this Software was used.
18//
19// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
22// THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
23// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25//
26
27use std::ffi::NulError;
28use std::{cmp,fmt};
29use std::{error,result};
30
31use crate::string::strerror;
32
33#[cfg(target_family = "unix")]
34#[doc(inline)]
35pub use crate::posix::errno::*;
36
37#[cfg(target_family = "windows")]
38#[doc(inline)]
39pub use crate::windows::errno::*;
40
41pub type Result<T> = result::Result<T, Error>;
42
43pub struct Error {
44    errmsg: String,
45    errnum: i32,
46    source: Option<NulError>
47}
48
49impl Error {
50    pub fn new(errnum: i32) -> Error {
51        let source = None;
52        match strerror(errnum) {
53            Ok(errmsg) => Error { errmsg, errnum, source },
54            Err(err) => err
55        }
56    }
57
58    pub fn new_msg(errnum: i32, errmsg: String) -> Error {
59        let source = None;
60        Error { errmsg, errnum, source }
61    }
62
63    pub fn errno() -> Error {
64        Error::new(errno())
65    }
66
67    pub fn msg(&self) -> &str {
68        &self.errmsg
69    }
70
71    pub fn num(&self) -> i32 {
72        self.errnum
73    }
74}
75
76impl error::Error for Error {
77    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
78        self.source.as_ref().map(|e| e as &dyn error::Error)
79    }
80}
81
82impl fmt::Debug for Error {
83    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
84        match &self.source {
85            Some(e) => <NulError as fmt::Debug>::fmt(e, f),
86            None => write!(f, "Error {{ errmsg: \"{}\", errnum: {} }}",
87                               self.errmsg, self.errnum)
88        }
89    }
90}
91
92impl fmt::Display for Error {
93    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94        match &self.source {
95            Some(e) => write!(f, "Error: {}", e),
96            None => write!(f, "Error {}: {}", self.errnum, self.errmsg)
97        }
98    }
99}
100
101impl From<NulError> for Error {
102    fn from(value: NulError) -> Self {
103        Error {
104            errmsg: String::from("Interior NUL byte"),
105            errnum: 0,
106            source: Some(value)
107        }
108    }
109}
110
111impl cmp::Eq for Error { }
112
113impl cmp::PartialEq for Error {
114    fn eq(&self, other: &Self) -> bool {
115        self.errnum == other.errnum
116    }
117}