Skip to main content

parse_changelog/
error.rs

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