Skip to main content

ical/tree/
error.rs

1//! # Errors
2//!
3//! The parsing errors.
4//!
5//! [`IcalParseError`] is the single error type returned by `IcalCst::parse`
6//! and the line tokeniser it drives. Each variant pinpoints one structural
7//! failure (a missing CRLF, a missing colon, an absent or malformed envelope)
8//! and carries the offending text for context. Parsing is the only fallible
9//! bridge in the crate; decoding, encoding and serializing never fail, so this
10//! is the whole error surface.
11
12use core::{error, fmt};
13
14use alloc::string::String;
15
16/// An error raised while parsing iCalendar text.
17#[derive(Clone, Debug, Eq, PartialEq)]
18pub enum IcalParseError {
19    /// A line carried no CR?LF separator.
20    MissingCrlf(String),
21    /// A content line carried no colon separating the name from the value.
22    MissingPropertyColon(String),
23    /// A content line's name or parameters were not valid UTF-8; only a value
24    /// may carry a foreign charset.
25    NonUtf8Header(String),
26    /// A card did not open with a BEGIN:VCALENDAR line.
27    ExpectedBegin(String),
28    /// A card was left open by a missing END:VCALENDAR line.
29    MissingEnd(String),
30}
31
32impl fmt::Display for IcalParseError {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::MissingCrlf(data) => {
36                write!(f, "Content is missing a line separator: {data}")
37            }
38            Self::MissingPropertyColon(data) => {
39                write!(f, "Content line is missing a value separator: {data}")
40            }
41            Self::NonUtf8Header(data) => {
42                write!(
43                    f,
44                    "Content line name or parameters are not valid UTF-8: {data}"
45                )
46            }
47            Self::ExpectedBegin(data) => {
48                write!(f, "Card does not open with a BEGIN line: {data}")
49            }
50            Self::MissingEnd(data) => {
51                write!(f, "Card is left open by a missing END line: {data}")
52            }
53        }
54    }
55}
56
57impl error::Error for IcalParseError {}