dependabot_config/
error.rs

1// SPDX-License-Identifier: Apache-2.0 OR MIT
2
3use core::fmt;
4use std::io;
5
6/// An error that occurred during parsing the Dependabot configuration.
7// TODO: in next breaking, add PhantomData<Box<dyn fmt::Display + Send + Sync>> to make error type !UnwindSafe & !RefUnwindSafe for forward compatibility.
8#[derive(Debug)]
9pub struct Error(ErrorKind);
10
11// Hiding error variants from a library's public error type to prevent
12// dependency updates from becoming breaking changes.
13// We can add `is_*` methods that indicate the kind of error if needed, but
14// don't expose dependencies' types directly in the public API.
15#[derive(Debug)]
16pub(crate) enum ErrorKind {
17    /// An error that occurred during parsing the configuration.
18    Yaml(serde_yaml::Error),
19}
20
21impl Error {
22    pub(crate) fn new(e: impl Into<ErrorKind>) -> Self {
23        Self(e.into())
24    }
25}
26
27impl fmt::Display for Error {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match &self.0 {
30            ErrorKind::Yaml(e) => fmt::Display::fmt(e, f),
31        }
32    }
33}
34
35impl std::error::Error for Error {
36    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
37        match &self.0 {
38            ErrorKind::Yaml(e) => Some(e),
39        }
40    }
41}
42
43impl From<Error> for io::Error {
44    fn from(e: Error) -> Self {
45        match e.0 {
46            ErrorKind::Yaml(e) => Self::new(io::ErrorKind::InvalidData, e),
47        }
48    }
49}
50
51impl From<serde_yaml::Error> for ErrorKind {
52    fn from(e: serde_yaml::Error) -> Self {
53        Self::Yaml(e)
54    }
55}
56
57// Note: Do not implement From<ThirdPartyErrorType> to prevent dependency
58// updates from becoming breaking changes.
59// Implementing `From<StdErrorType>` should also be avoided whenever possible,
60// as it would be a breaking change to remove the implementation if the
61// conversion is no longer needed due to changes in the internal implementation.