Skip to main content

content_line_writer/
error.rs

1// Copyright 2024-2025 Hugo Osvaldo Barrera
2//
3// SPDX-License-Identifier: ISC
4
5//! Errors returned when validating content line data.
6
7/// Error returned when a content line's name or a parameter's name is invalid.
8#[derive(Debug, PartialEq)]
9#[allow(clippy::enum_variant_names)]
10pub enum NameError {
11    /// Invalid character found in name.
12    InvalidChar(char),
13    /// The vendor length is of an invalid length
14    ///
15    /// A `vendorid` must be at least three character.
16    InvalidVendorIdLength(usize),
17    /// The name is invalid.
18    InvalidName,
19}
20
21impl std::fmt::Display for NameError {
22    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23        match self {
24            NameError::InvalidChar(c) => write!(f, "invalid character: {c}"),
25            NameError::InvalidVendorIdLength(count) => {
26                write!(f, "invalid vendor ID length: {count}")
27            }
28            NameError::InvalidName => write!(f, "invalid name"),
29        }
30    }
31}
32
33impl std::error::Error for NameError {}
34
35impl From<NameError> for std::io::Error {
36    fn from(err: NameError) -> std::io::Error {
37        std::io::Error::new(std::io::ErrorKind::InvalidInput, err)
38    }
39}
40
41/// The supplied parameter value is invalid.
42#[derive(Debug, PartialEq)]
43pub enum ParamValueError {
44    /// Invalid character found in parameter value.
45    InvalidChar(char),
46    /// Invalid character inside quoted sequence found in parameter value.
47    InvalidQuotedChar(char),
48    /// An opening quote is missing its matching quote.
49    MissingClosingQuote,
50    /// Unexpected character in parameter value.
51    UnexpectedChar(char),
52}
53
54impl std::fmt::Display for ParamValueError {
55    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
56        match self {
57            ParamValueError::InvalidChar(c) => write!(f, "invalid character: {c}"),
58            ParamValueError::InvalidQuotedChar(c) => {
59                write!(f, "invalid character inside quoted string: {c}")
60            }
61            ParamValueError::MissingClosingQuote => write!(f, "missing closing quote"),
62            ParamValueError::UnexpectedChar(c) => write!(f, "unexpected character: {c}"),
63        }
64    }
65}
66
67impl std::error::Error for ParamValueError {}
68
69impl From<ParamValueError> for std::io::Error {
70    fn from(err: ParamValueError) -> std::io::Error {
71        std::io::Error::new(std::io::ErrorKind::InvalidInput, err)
72    }
73}
74
75/// The supplied value contains invalid characters.
76///
77/// Valid characters are `WSP / %x21-7E / NON-US-ASCII`.
78#[derive(Debug, PartialEq)]
79pub struct InvalidValue(pub char);
80
81impl std::fmt::Display for InvalidValue {
82    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
83        write!(
84            f,
85            "Value contains invalid character: {}",
86            self.0.escape_debug()
87        )
88    }
89}
90
91impl std::error::Error for InvalidValue {}
92
93impl From<InvalidValue> for std::io::Error {
94    fn from(err: InvalidValue) -> std::io::Error {
95        std::io::Error::new(std::io::ErrorKind::InvalidInput, err)
96    }
97}