rpsl/
error.rs

1use std::fmt;
2
3use thiserror::Error;
4
5#[derive(Error, Debug)]
6pub enum InvalidNameError {
7    #[error("cannot be empty")]
8    Empty,
9    #[error("cannot contain characters that are not part of the extended ASCII set")]
10    NonAscii,
11    #[error("cannot start with a non-letter ASCII character")]
12    NonAsciiAlphabeticFirstChar,
13    #[error("cannot end with a non-letter or non-digit ASCII character")]
14    NonAsciiAlphanumericLastChar,
15}
16
17#[derive(Error, Debug)]
18pub enum InvalidValueError {
19    #[error("cannot contain characters that are not part of the extended ASCII set")]
20    NonExtendedAscii,
21    #[error("cannot contain ASCII control characters")]
22    ContainsControlChar,
23}
24
25#[derive(Error, Debug)]
26/// An error that can occur when parsing or trying to create an attribute that is invalid.
27pub enum AttributeError {
28    /// The name of the attribute is invalid.
29    #[error("Invalid attribute name: {0}")]
30    InvalidName(#[from] InvalidNameError),
31    /// The value of the attribute is invalid.
32    #[error("Invalid attribute value: {0}")]
33    InvalidValue(#[from] InvalidValueError),
34}
35
36/// An error that can occur when parsing RPSL text.
37///
38/// # Example
39/// ```
40/// # use rpsl::parse_object;
41/// let rpsl = "\
42/// role;        ACME Company
43///
44/// ";
45/// let err = parse_object(rpsl).unwrap_err();
46/// let message = "\
47/// parse error at line 1, column 5
48///   |
49/// 1 | role;        ACME Company
50///   |     ^
51/// invalid separator
52/// expected `:`";
53/// assert_eq!(err.to_string(), message);
54/// ```
55#[derive(Error, Debug)]
56pub struct ParseError(String);
57
58impl fmt::Display for ParseError {
59    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
60        write!(f, "{}", self.0)
61    }
62}
63
64impl From<winnow::error::ParseError<&str, winnow::error::ContextError>> for ParseError {
65    fn from(value: winnow::error::ParseError<&str, winnow::error::ContextError>) -> Self {
66        Self(value.to_string())
67    }
68}