1 #[cfg(feature = "std")] 2 use std::error::Error; 3 4 use alloc::fmt::{self, Display, Formatter}; 5 6 #[derive(Debug, Clone)] 7 /// Error types for parsing values. 8 pub enum ValueIncorrectError { 9 Negative(f64), 10 NotNumber(char), 11 NoValue, 12 } 13 14 impl Display for ValueIncorrectError { 15 #[inline] fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error>16 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { 17 match self { 18 ValueIncorrectError::Negative(value) => { 19 f.write_fmt(format_args!("The value `{}` is negative.", value)) 20 } 21 ValueIncorrectError::NotNumber(c) => { 22 f.write_fmt(format_args!("The character {:?} is not a number.", c)) 23 } 24 ValueIncorrectError::NoValue => f.write_str("No value."), 25 } 26 } 27 } 28 29 #[cfg(feature = "std")] 30 impl Error for ValueIncorrectError {} 31 32 #[derive(Debug, Clone)] 33 /// Errors for `ByteUnit`. 34 pub struct UnitIncorrectError { 35 pub character: char, 36 pub expected_characters: &'static [char], 37 pub also_expect_no_character: bool, 38 } 39 40 impl Display for UnitIncorrectError { 41 #[inline] fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error>42 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { 43 let expected_characters_length = self.expected_characters.len(); 44 45 if expected_characters_length == 0 { 46 f.write_fmt(format_args!( 47 "The character {:?} is incorrect. No character is expected.", 48 self.character 49 )) 50 } else { 51 f.write_fmt(format_args!("The character {:?} is incorrect.", self.character))?; 52 53 f.write_fmt(format_args!(" {:?}", self.expected_characters[0]))?; 54 55 if expected_characters_length > 1 { 56 for c in self.expected_characters[1..].iter().take(expected_characters_length - 2) { 57 f.write_fmt(format_args!(", {:?}", c))?; 58 } 59 } 60 61 if self.also_expect_no_character { 62 f.write_fmt(format_args!( 63 ", {:?} or no character is expected.", 64 self.expected_characters[expected_characters_length - 1] 65 )) 66 } else { 67 f.write_fmt(format_args!( 68 " or {:?} is expected.", 69 self.expected_characters[expected_characters_length - 1] 70 )) 71 } 72 } 73 } 74 } 75 76 #[cfg(feature = "std")] 77 impl Error for UnitIncorrectError {} 78 79 #[derive(Debug, Clone)] 80 /// Error types for `Byte` and `ByteUnit`. 81 pub enum ByteError { 82 ValueIncorrect(ValueIncorrectError), 83 UnitIncorrect(UnitIncorrectError), 84 } 85 86 impl From<ValueIncorrectError> for ByteError { 87 #[inline] from(error: ValueIncorrectError) -> Self88 fn from(error: ValueIncorrectError) -> Self { 89 ByteError::ValueIncorrect(error) 90 } 91 } 92 93 impl From<UnitIncorrectError> for ByteError { 94 #[inline] from(error: UnitIncorrectError) -> Self95 fn from(error: UnitIncorrectError) -> Self { 96 ByteError::UnitIncorrect(error) 97 } 98 } 99 100 impl Display for ByteError { 101 #[inline] fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error>102 fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), fmt::Error> { 103 match self { 104 ByteError::ValueIncorrect(error) => Display::fmt(error, f), 105 ByteError::UnitIncorrect(error) => Display::fmt(error, f), 106 } 107 } 108 } 109 110 #[cfg(feature = "std")] 111 impl Error for ByteError {} 112