google_cloud_auth/
build_errors.rs1type BoxError = Box<dyn std::error::Error + Send + Sync + 'static>;
18
19#[derive(thiserror::Error, Debug)]
27#[error(transparent)]
28pub struct Error(ErrorKind);
29
30impl Error {
31 pub fn is_loading(&self) -> bool {
33 matches!(self.0, ErrorKind::Loading(_))
34 }
35
36 pub fn is_parsing(&self) -> bool {
38 matches!(self.0, ErrorKind::Parsing(_))
39 }
40
41 pub fn is_unknown_type(&self) -> bool {
43 matches!(self.0, ErrorKind::UnknownType(_))
44 }
45
46 pub(crate) fn loading<T>(source: T) -> Error
49 where
50 T: Into<BoxError>,
51 {
52 Error(ErrorKind::Loading(source.into()))
53 }
54
55 pub(crate) fn parsing<T>(source: T) -> Error
57 where
58 T: Into<BoxError>,
59 {
60 Error(ErrorKind::Parsing(source.into()))
61 }
62
63 pub(crate) fn unknown_type<T>(source: T) -> Error
65 where
66 T: Into<BoxError>,
67 {
68 Error(ErrorKind::UnknownType(source.into()))
69 }
70}
71
72#[derive(thiserror::Error, Debug)]
73enum ErrorKind {
74 #[error("could not find or open the credentials file {0}")]
75 Loading(#[source] BoxError),
76 #[error("cannot parse the credentials file {0}")]
77 Parsing(#[source] BoxError),
78 #[error("unknown or invalid credentials type {0}")]
79 UnknownType(#[source] BoxError),
80}
81
82#[cfg(test)]
83mod test {
84 use super::*;
85 use std::error::Error as _;
86
87 #[test]
88 fn constructors() {
89 let error = Error::loading("test message");
90 assert!(error.is_loading(), "{error:?}");
91 assert!(error.source().is_some(), "{error:?}");
92 assert!(error.to_string().contains("test message"), "{error}");
93
94 let error = Error::parsing("test message");
95 assert!(error.is_parsing(), "{error:?}");
96 assert!(error.source().is_some(), "{error:?}");
97 assert!(error.to_string().contains("test message"), "{error}");
98
99 let error = Error::unknown_type("test message");
100 assert!(error.is_unknown_type(), "{error:?}");
101 assert!(error.source().is_some(), "{error:?}");
102 assert!(error.to_string().contains("test message"), "{error}");
103 }
104}