kutil_std/string/
parse.rs

1use std::fmt;
2
3//
4// ParseError
5//
6
7/// Parse error.
8#[derive(Debug)]
9pub struct ParseError(Option<String>);
10
11impl ParseError {
12    /// Constructor.
13    pub fn new(message: Option<String>) -> Self {
14        Self(message)
15    }
16}
17
18impl From<String> for ParseError {
19    fn from(message: String) -> Self {
20        Self::new(Some(message))
21    }
22}
23
24impl From<&str> for ParseError {
25    fn from(message: &str) -> Self {
26        Self::new(Some(message.into()))
27    }
28}
29
30impl fmt::Display for ParseError {
31    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
32        match &self.0 {
33            Some(message) => write!(formatter, "could not parse: {}", message),
34            None => fmt::Display::fmt("could not parse", formatter),
35        }
36    }
37}