pwd_grp/
error.rs

1//! Error handling and error types
2//!
3//! Most of the interfaces return a [`std::io::Error`].
4//! Usually an error means that the underlying `get*_r` call failed.
5//!
6//! But, there are some other obscure error cases.
7//! In those situations, an [`io::Error`] will be returned
8//! which has one of the errors in this module
9//! as its [`source`](std::error::Error::source).
10
11use super::*;
12
13define_derive_deftly! {
14    IntoIoError for struct, expect items:
15
16    impl From<$ttype> for io::Error {
17        fn from(us: $ttype) -> io::Error {
18            io::Error::new(${tmeta(io_kind) as expr}, us)
19        }
20    }
21}
22
23/// Error that occurs if `getpw*_r` or `getgr*_r` return unexpected `NULL`s
24///
25/// This should not occur.
26/// (But it doesn't seem to be entirely explicitly ruled out by the spec.)
27///
28/// (When the `source` of an `io::Error`, `ErrorKind` is [`Other`].)
29#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
30#[non_exhaustive]
31#[derive(Error)]
32#[error("unexpected null pointer from getpw* or getgr*")]
33#[derive(Deftly)]
34#[derive_deftly(IntoIoError)]
35#[deftly(io_kind = "Other")]
36pub struct UnexpectedNullPointerError;
37
38/// Error that occurs if an entry is unreasonably large
39///
40/// If a password or group entry is too large for the system's
41/// address space, this error will be returned.
42///
43/// (When the `source` of an `io::Error`, `ErrorKind` is [`OutOfMemory`].)
44#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
45#[non_exhaustive]
46#[derive(Error)]
47#[error(
48    "getpw*_r or getgr*_r or getgroups required unreasonably large buffer"
49)]
50#[derive(Deftly)]
51#[derive_deftly(IntoIoError)]
52#[deftly(io_kind = "OutOfMemory")]
53pub struct TooLargeBufferRequiredError;
54
55/// Error that occurs if non-UTF-8 data is found
56///
57/// (When the `source` of an `io::Error`, `ErrorKind` is [`InvalidData`].)
58#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
59#[non_exhaustive]
60#[derive(Error)]
61#[error(
62    "non-UTF-8 data in system password or group database, in field {field}"
63)]
64#[derive(Deftly)]
65#[derive_deftly(IntoIoError)]
66#[deftly(io_kind = "InvalidData")]
67pub struct NonUtf8Error {
68    pub(crate) field: Box<str>,
69}
70
71impl NonUtf8Error {
72    /// Return the libc field name that had non-UTF-8
73    pub fn field(&self) -> &str {
74        &self.field
75    }
76}
77
78/// Error that occurs if a nul byte was passed to `get*_nam`
79///
80/// The C APIs do not permit nul bytes in names.
81///
82/// (When returned as the
83/// (When the `source` of an `io::Error`, `ErrorKind` is [`InvalidInput`].)
84pub type NulError = std::ffi::NulError;