humanize_rs/
lib.rs

1#![warn(missing_docs)]
2
3//! This lib is used to parse formatted strings to different types
4
5use std::error::Error;
6use std::fmt;
7
8pub mod bytes;
9pub mod duration;
10pub mod num;
11pub mod time;
12
13/// Error parsing formatted strings
14#[derive(Debug, Copy, Clone, Eq, PartialEq)]
15pub enum ParseError {
16    /// Parsing an empty string
17    EmptyInput,
18
19    /// The numeric value is missing
20    MissingValue,
21
22    /// Malformed numeric value
23    InvalidValue,
24
25    /// The unit is missing
26    MissingUnit,
27
28    /// Unknown unit
29    InvalidUnit,
30
31    /// The unit shows multiple times in the string
32    DuplicateUnit,
33
34    /// The numeric value is too large
35    Overflow,
36
37    /// Too short for some pattern
38    TooShort,
39
40    /// Too long for some pattern
41    TooLong,
42
43    /// Malformed string
44    Malformed,
45
46    /// Invalid timezone
47    InvalidTimezone,
48}
49
50impl ParseError {
51    fn description(&self) -> &str {
52        match self {
53            ParseError::EmptyInput => "empty input",
54            ParseError::MissingValue => "missing value",
55            ParseError::InvalidValue => "invalid value",
56            ParseError::MissingUnit => "missing unit",
57            ParseError::InvalidUnit => "invalid unit",
58            ParseError::DuplicateUnit => "duplicate unit",
59            ParseError::Overflow => "value overflow",
60            ParseError::TooShort => "too short",
61            ParseError::TooLong => "too long",
62            ParseError::Malformed => "malformed",
63            ParseError::InvalidTimezone => "invalid timezone",
64        }
65    }
66}
67
68impl fmt::Display for ParseError {
69    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
70        f.pad(self.description())
71    }
72}
73
74impl Error for ParseError {}