soft_ascii_string/
error.rs

1use std::fmt::{self, Debug};
2use std::error::Error;
3
4/// Error returned if FromStr failed
5#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
6pub struct StringFromStrError;
7impl Error for StringFromStrError {
8    fn description(&self) -> &str {
9        "&str does contain non us-ascii chars and can not be converted to a SoftAsciiString"
10    }
11}
12
13impl fmt::Display for StringFromStrError {
14    fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
15        write!(fter, "{}", self.description())
16    }
17}
18
19/// Error returned if creating a SoftAsciiStr/SoftAsciiString failed
20#[derive(Debug, PartialEq, Eq, Clone, Hash)]
21pub struct FromSourceError<S: Debug> {
22    source: S
23}
24
25impl<S> FromSourceError<S>
26    where S: Debug
27{
28
29    /// creates a new FromSourceError
30    pub fn new(source: S) -> Self {
31        FromSourceError { source }
32    }
33
34    /// returns a reference to the source
35    ///
36    /// the source is the input which was meant to be converted into a
37    /// SoftAsciiStr/String
38    pub fn source(&self) -> &S {
39        &self.source
40    }
41
42
43    // Note that Into, can not be implemented due to possible conflicting
44    // implementations
45    /// returns the source
46    ///
47    /// the source is the input which was meant to be converted into a
48    /// SoftAsciiStr/String
49    pub fn into_source(self) -> S {
50        self.source
51    }
52}
53
54
55
56impl<S> Error for FromSourceError<S>
57    where S: Debug
58{
59    fn description(&self) -> &str {
60        concat!("could not create a SoftAscii representation of the source",
61                "as the source contained non us-ascii chars")
62    }
63}
64
65impl<S> fmt::Display for FromSourceError<S>
66    where S: Debug
67{
68    fn fmt(&self, fter: &mut fmt::Formatter) -> fmt::Result {
69        write!(fter, "source contains non us-ascii chars: {:?}", self.source)
70    }
71}