rust_uci/
error.rs

1// Copyright 2021, Benjamin Ludewig
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4use std::ffi::NulError;
5use std::fmt::{Debug, Display, Formatter};
6use std::option::Option::None;
7use std::str::Utf8Error;
8
9#[derive(Debug, Clone)]
10pub enum Error {
11    Message(String),
12    Utf8Error(Utf8Error),
13    NulError(NulError),
14}
15
16pub type Result<T> = std::result::Result<T, Error>;
17
18impl From<Utf8Error> for Error {
19    fn from(err: Utf8Error) -> Self {
20        Self::Utf8Error(err)
21    }
22}
23
24impl From<NulError> for Error {
25    fn from(err: NulError) -> Self {
26        Self::NulError(err)
27    }
28}
29
30impl std::error::Error for Error {
31    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
32        match self {
33            Error::Message(_) => None,
34            Error::Utf8Error(err) => Some(err),
35            Error::NulError(err) => Some(err),
36        }
37    }
38}
39
40impl Display for Error {
41    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
42        match self {
43            Error::Message(msg) => Display::fmt(msg, f),
44            Error::Utf8Error(err) => Display::fmt(err, f),
45            Error::NulError(err) => Display::fmt(err, f),
46        }
47    }
48}