use std::{
error::Error,
fmt::{self, Display, Formatter},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct InvalidValueError {
expected: &'static str,
}
impl InvalidValueError {
#[inline]
pub(crate) const fn new(expected: &'static str) -> Self {
Self {
expected,
}
}
#[inline]
pub const fn expected(&self) -> &'static str {
self.expected
}
}
impl Display for InvalidValueError {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "the input is not a valid {}", self.expected)
}
}
impl Error for InvalidValueError {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ValidationError {
MissingFormattedName,
MemberWithoutGroupKind,
}
impl Display for ValidationError {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::MissingFormattedName => f.write_str("a vCard must have at least one FN property"),
Self::MemberWithoutGroupKind => {
f.write_str("the MEMBER property can only be used when KIND is group")
},
}
}
}
impl Error for ValidationError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParseError {
pub line: usize,
pub kind: ParseErrorKind,
}
impl Display for ParseError {
#[inline]
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "{} (at line {})", self.kind, self.line)
}
}
impl Error for ParseError {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseErrorKind {
ExpectedBegin,
ExpectedVersion,
UnsupportedVersion(String),
MissingEnd,
TrailingData,
InvalidLine,
InvalidGroupName,
InvalidParameter(String),
InvalidValue {
property: String,
},
DuplicateProperty(String),
MissingFormattedName,
}
impl Display for ParseErrorKind {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
match self {
Self::ExpectedBegin => f.write_str("expected a BEGIN:VCARD line"),
Self::ExpectedVersion => {
f.write_str("expected a VERSION property right after BEGIN:VCARD")
},
Self::UnsupportedVersion(version) => {
write!(f, "the vCard version {version:?} is not supported")
},
Self::MissingEnd => f.write_str("missing the END:VCARD line"),
Self::TrailingData => f.write_str("unexpected content after END:VCARD"),
Self::InvalidLine => f.write_str("the line is not a well-formed content line"),
Self::InvalidGroupName => f.write_str("the group name is invalid"),
Self::InvalidParameter(name) => write!(f, "the parameter {name} is invalid"),
Self::InvalidValue {
property,
} => write!(f, "the value of the property {property} is invalid"),
Self::DuplicateProperty(name) => {
write!(f, "the property {name} appears more than once")
},
Self::MissingFormattedName => f.write_str("the vCard does not have any FN property"),
}
}
}
impl Error for ParseErrorKind {}