libfdt_rs/
error.rs

1/// The possible errors `libfdt` can output.
2/// It is a 1-to-1 translation of error`libfdt` can issue.
3#[derive(Debug, Clone)]
4pub enum Error {
5    /// The requested node or property does not exist
6    NotFound,
7    /// Attempted to create a node or property which already exists
8    Exists,
9    /// Operation needed to expand the device
10    /// tree, but its buffer did not have sufficient space to
11    /// contain the expanded tree.
12    NoSpace,
13    /// Function was passed a structure block
14    /// offset which is out-of-bounds, or which points to an
15    /// unsuitable part of the structure for the operation.
16    BadOffset,
17    /// Function was passed a badly formatted path
18    /// (e.g. missing a leading / for a function which requires an
19    /// absolute path)
20    BadPath,
21    /// Function was passed an invalid phandle.
22    /// This can be caused either by an invalid phandle property
23    /// length, or the phandle value was either 0 or -1, which are
24    /// not permitted.
25    BadPhandle,
26    /// Function was passed an incomplete device
27    /// tree created by the sequential-write functions, which is
28    /// not sufficiently complete for the requested operation.
29    BadState,
30    Truncated,
31    BadMagic,
32    BadVersion,
33    BadStructure,
34    BadLayout,
35    Internal,
36    BadNCells,
37    BadValue,
38    BadOverlay,
39    NoPhandle,
40    BadFlags,
41    Alignment,
42    Unknown(i32),
43}
44
45impl Error {
46    pub fn parse(ret: i32) -> Result<i32, Error> {
47        if ret >= 0 {
48            return Ok(ret);
49        }
50
51        let err = u32::try_from(-ret).unwrap();
52
53        match err {
54            libfdt_sys::FDT_ERR_NOTFOUND => Err(Error::NotFound),
55            libfdt_sys::FDT_ERR_EXISTS => Err(Error::Exists),
56            libfdt_sys::FDT_ERR_NOSPACE => Err(Error::NoSpace),
57            libfdt_sys::FDT_ERR_BADOFFSET => Err(Error::BadOffset),
58            libfdt_sys::FDT_ERR_BADPATH => Err(Error::BadPath),
59            libfdt_sys::FDT_ERR_BADPHANDLE => Err(Error::BadPhandle),
60            libfdt_sys::FDT_ERR_BADSTATE => Err(Error::BadState),
61            libfdt_sys::FDT_ERR_TRUNCATED => Err(Error::Truncated),
62            libfdt_sys::FDT_ERR_BADMAGIC => Err(Error::BadMagic),
63            libfdt_sys::FDT_ERR_BADVERSION => Err(Error::BadVersion),
64            libfdt_sys::FDT_ERR_BADSTRUCTURE => Err(Error::BadStructure),
65            libfdt_sys::FDT_ERR_BADLAYOUT => Err(Error::BadLayout),
66            libfdt_sys::FDT_ERR_INTERNAL => Err(Error::Internal),
67            libfdt_sys::FDT_ERR_BADNCELLS => Err(Error::BadNCells),
68            libfdt_sys::FDT_ERR_BADVALUE => Err(Error::BadValue),
69            libfdt_sys::FDT_ERR_BADOVERLAY => Err(Error::BadOverlay),
70            libfdt_sys::FDT_ERR_NOPHANDLES => Err(Error::NoPhandle),
71            libfdt_sys::FDT_ERR_BADFLAGS => Err(Error::BadFlags),
72            libfdt_sys::FDT_ERR_ALIGNMENT => Err(Error::Alignment),
73            _ => Err(Error::Unknown(ret)),
74        }
75    }
76}