smart_string/pascal_string/
error.rs

1use core::fmt;
2use core::str::Utf8Error;
3
4/// An error returned when a conversion from a `&str` to a `PascalString` fails.
5#[derive(Debug, Clone, Copy, PartialEq, Eq)]
6pub enum TryFromStrError {
7    /// The string is too long to fit into a `PascalString`.
8    TooLong,
9}
10
11/// An error returned when a conversion from a `&[u8]` to a `PascalString` fails.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum TryFromBytesError {
14    /// The string is too long to fit into a `PascalString`.
15    TooLong,
16    /// The string is not valid UTF-8.
17    Utf8Error(Utf8Error),
18}
19
20impl From<Utf8Error> for TryFromBytesError {
21    fn from(e: Utf8Error) -> Self {
22        TryFromBytesError::Utf8Error(e)
23    }
24}
25
26impl From<TryFromStrError> for TryFromBytesError {
27    fn from(e: TryFromStrError) -> Self {
28        match e {
29            TryFromStrError::TooLong => TryFromBytesError::TooLong,
30        }
31    }
32}
33
34impl fmt::Display for TryFromStrError {
35    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
36        match self {
37            TryFromStrError::TooLong => f.write_str("string too long"),
38        }
39    }
40}
41
42impl fmt::Display for TryFromBytesError {
43    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
44        match self {
45            TryFromBytesError::TooLong => f.write_str("string too long"),
46            TryFromBytesError::Utf8Error(e) => e.fmt(f),
47        }
48    }
49}