parse_changelog/
error.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3use core::fmt;
4
5pub(crate) type Result<T, E = Error> = core::result::Result<T, E>;
6
7/// An error that occurred during parsing changelog or configuring the parser.
8// TODO: in next breaking, add PhantomData<Box<dyn fmt::Display + Send + Sync>> to make error type !UnwindSafe & !RefUnwindSafe for forward compatibility.
9#[derive(Debug)]
10pub struct Error(ErrorKind);
11
12// Hiding error variants from a library's public error type to prevent
13// dependency updates from becoming breaking changes.
14// We can add `is_*` methods that indicate the kind of error if needed, but
15// don't expose dependencies' types directly in the public API.
16#[derive(Debug)]
17pub(crate) enum ErrorKind {
18    /// The specified format is not a valid regular expression or supported by
19    /// [regex] crate.
20    ///
21    /// This error only occurs during configuring the parser.
22    ///
23    /// [regex]: https://docs.rs/regex
24    Regex(regex::Error),
25    /// The specified format is a valid regular expression but not a format
26    /// that accepted by the parser.
27    ///
28    /// This error only occurs during configuring the parser.
29    Format(Box<str>),
30    /// An error that occurred during parsing changelog.
31    Parse(Box<str>),
32}
33
34impl Error {
35    pub(crate) fn new(e: impl Into<ErrorKind>) -> Self {
36        Self(e.into())
37    }
38
39    pub(crate) fn format(e: impl Into<String>) -> Self {
40        Self(ErrorKind::Format(e.into().into_boxed_str()))
41    }
42
43    pub(crate) fn parse(e: impl Into<String>) -> Self {
44        Self(ErrorKind::Parse(e.into().into_boxed_str()))
45    }
46
47    /// Returns `true` if this error is that occurred during configuring the parser.
48    #[must_use]
49    pub fn is_format(&self) -> bool {
50        matches!(self.0, ErrorKind::Format(..) | ErrorKind::Regex(..))
51    }
52
53    /// Returns `true` if this error is that occurred during parsing changelog.
54    #[must_use]
55    pub fn is_parse(&self) -> bool {
56        matches!(self.0, ErrorKind::Parse(..))
57    }
58}
59
60impl fmt::Display for Error {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match &self.0 {
63            ErrorKind::Regex(e) => fmt::Display::fmt(e, f),
64            ErrorKind::Format(e) | ErrorKind::Parse(e) => fmt::Display::fmt(e, f),
65        }
66    }
67}
68
69impl std::error::Error for Error {
70    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
71        match &self.0 {
72            ErrorKind::Regex(e) => Some(e),
73            _ => None,
74        }
75    }
76}
77
78impl From<regex::Error> for ErrorKind {
79    fn from(e: regex::Error) -> Self {
80        Self::Regex(e)
81    }
82}
83
84// Note: Do not implement From<ThirdPartyErrorType> to prevent dependency
85// updates from becoming breaking changes.
86// Implementing `From<StdErrorType>` should also be avoided whenever possible,
87// as it would be a breaking change to remove the implementation if the
88// conversion is no longer needed due to changes in the internal implementation.