devicemapper/
result.rs

1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5use std::{error::Error, fmt};
6
7use crate::core::errors;
8
9/// A very simple breakdown of outer layer errors.
10#[derive(Clone, Debug)]
11pub enum ErrorEnum {
12    /// generic error code
13    Error,
14    /// invalid value passed as argument
15    Invalid,
16    /// something not found
17    NotFound,
18}
19
20impl fmt::Display for ErrorEnum {
21    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
22        fmt::Debug::fmt(self, f)
23    }
24}
25
26/// Super error type, with constructors distinguishing outer errors from
27/// core errors.
28#[derive(Clone, Debug)]
29pub enum DmError {
30    /// DM errors
31    Dm(ErrorEnum, String),
32    /// Errors in the core devicemapper functionality
33    Core(errors::Error),
34}
35
36/// return result for DM functions
37pub type DmResult<T> = Result<T, DmError>;
38
39impl From<errors::Error> for DmError {
40    fn from(err: errors::Error) -> DmError {
41        DmError::Core(err)
42    }
43}
44
45impl fmt::Display for DmError {
46    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47        match *self {
48            DmError::Core(ref err) => write!(f, "DM Core error: {err}"),
49            DmError::Dm(ref err, ref msg) => write!(f, "DM error: {err}: {msg}"),
50        }
51    }
52}
53
54impl Error for DmError {}