sashite_sin/error.rs
1//! Errors produced when parsing a SIN token.
2
3/// The reason a string could not be parsed as a SIN token.
4///
5/// Returned by the parsing entry points ([`crate::Identifier::parse`],
6/// [`core::str::FromStr`], [`TryFrom`]) and by [`crate::Letter::try_from_char`].
7///
8/// This enum is `#[non_exhaustive]`: future revisions may add variants without
9/// a breaking change, so downstream `match` expressions should include a
10/// wildcard arm.
11///
12/// # Examples
13///
14/// ```
15/// use sashite_sin::{Identifier, ParseError};
16///
17/// assert_eq!("".parse::<Identifier>(), Err(ParseError::Empty));
18/// assert_eq!("CC".parse::<Identifier>(), Err(ParseError::TooLong));
19/// assert_eq!("1".parse::<Identifier>(), Err(ParseError::InvalidLetter));
20/// ```
21#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
22#[non_exhaustive]
23pub enum ParseError {
24 /// The input was empty.
25 Empty,
26 /// The input was longer than the single character a token occupies.
27 TooLong,
28 /// The input was not a single ASCII letter.
29 InvalidLetter,
30}
31
32impl core::fmt::Display for ParseError {
33 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
34 let message = match self {
35 Self::Empty => "empty SIN token",
36 Self::TooLong => "SIN token longer than one character",
37 Self::InvalidLetter => "SIN token must contain exactly one ASCII letter",
38 };
39 f.write_str(message)
40 }
41}
42
43impl core::error::Error for ParseError {}