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