fst_no_std/
error.rs

1use core::fmt;
2#[cfg(feature = "std")]
3use std::io;
4
5use crate::raw;
6
7/// A `Result` type alias for this crate's `Error` type.
8#[cfg(feature = "std")]
9pub type Result<T> = std::result::Result<T, Error>;
10
11/// A `Result` type alias for this crate's `Error` type.
12#[cfg(not(feature = "std"))]
13pub type Result<T> = core::result::Result<T, Error>;
14
15/// An error that encapsulates all possible errors in this crate.
16#[derive(Debug)]
17pub enum Error {
18    /// An error that occurred while reading or writing a finite state
19    /// transducer.
20    Fst(raw::Error),
21    /// An IO error that occurred while writing a finite state transducer.
22    #[cfg(feature = "std")]
23    Io(io::Error),
24}
25
26#[cfg(feature = "std")]
27impl From<io::Error> for Error {
28    #[inline]
29    fn from(err: io::Error) -> Error {
30        Error::Io(err)
31    }
32}
33
34impl From<raw::Error> for Error {
35    #[inline]
36    fn from(err: raw::Error) -> Error {
37        Error::Fst(err)
38    }
39}
40
41impl fmt::Display for Error {
42    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
43        match *self {
44            Error::Fst(_) => write!(f, "FST error"),
45            #[cfg(feature = "std")]
46            Error::Io(_) => write!(f, "I/O error"),
47        }
48    }
49}
50
51#[cfg(feature = "std")]
52impl std::error::Error for Error {
53    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
54        match *self {
55            Error::Fst(ref err) => Some(err),
56            #[cfg(feature = "std")]
57            Error::Io(ref err) => Some(err),
58        }
59    }
60}
61
62#[cfg(not(feature = "std"))]
63impl core::error::Error for Error {
64    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
65        match *self {
66            Error::Fst(ref err) => Some(err),
67            #[cfg(feature = "std")]
68            Error::Io(ref err) => Some(err),
69        }
70    }
71}