1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use std::borrow::Cow;
use std::error::Error;
use std::fmt::Display;

#[derive(Debug)]
pub struct ConversionFailure {
    pub from: Cow<'static, str>,
    pub to: Cow<'static, str>,
}

impl Error for ConversionFailure {}

impl Display for ConversionFailure {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Could not convert from `{}` to `{}`", self.from, self.to)
    }
}

impl ConversionFailure {
    pub fn new<A, B>(from: A, to: B) -> ConversionFailure
    where
        A: Into<Cow<'static, str>>,
        B: Into<Cow<'static, str>>,
    {
        ConversionFailure {
            from: from.into(),
            to: to.into(),
        }
    }
}