typlite/
error.rs

1use core::fmt;
2use std::{borrow::Cow, ops::Deref};
3
4/// An error that can occur during the conversion process.
5#[derive(Clone)]
6pub struct Error(Box<Repr>);
7
8#[derive(Clone)]
9enum Repr {
10    /// Just a message.
11    Msg(Cow<'static, str>),
12}
13
14impl fmt::Display for Error {
15    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16        match self.0.deref() {
17            Repr::Msg(s) => write!(f, "{s}"),
18        }
19    }
20}
21
22impl fmt::Debug for Error {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        <Self as fmt::Display>::fmt(self, f)
25    }
26}
27
28impl From<std::io::Error> for Error {
29    fn from(e: std::io::Error) -> Self {
30        Error(Box::new(Repr::Msg(e.to_string().into())))
31    }
32}
33
34impl From<fmt::Error> for Error {
35    fn from(e: fmt::Error) -> Self {
36        Error(Box::new(Repr::Msg(e.to_string().into())))
37    }
38}
39
40impl From<&'static str> for Error {
41    fn from(s: &'static str) -> Self {
42        Error(Box::new(Repr::Msg(s.into())))
43    }
44}
45
46impl From<String> for Error {
47    fn from(s: String) -> Self {
48        Error(Box::new(Repr::Msg(s.into())))
49    }
50}
51
52impl From<Cow<'static, str>> for Error {
53    fn from(s: Cow<'static, str>) -> Self {
54        Error(Box::new(Repr::Msg(s)))
55    }
56}