Skip to content

Commit 459cab9

Browse files
committed
refactor(credential): Consolidate Other construction
1 parent 321291f commit 459cab9

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

credential/cargo-credential/src/error.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,7 @@ impl From<&str> for Error {
8080

8181
impl From<anyhow::Error> for Error {
8282
fn from(value: anyhow::Error) -> Self {
83-
let mut prev = None;
84-
for e in value.chain().rev() {
85-
prev = Some(Box::new(StringTypedError {
86-
message: e.to_string(),
87-
source: prev,
88-
}));
89-
}
90-
Error::Other(prev.unwrap())
83+
Error::from(Box::new(StringTypedError::from(value)))
9184
}
9285
}
9386

@@ -116,6 +109,19 @@ impl std::fmt::Display for StringTypedError {
116109
}
117110
}
118111

112+
impl From<anyhow::Error> for StringTypedError {
113+
fn from(value: anyhow::Error) -> Self {
114+
let mut prev = None;
115+
for e in value.chain().rev() {
116+
prev = Some(StringTypedError {
117+
message: e.to_string(),
118+
source: prev.map(Box::new),
119+
});
120+
}
121+
prev.unwrap()
122+
}
123+
}
124+
119125
/// Serializer / deserializer for any boxed error.
120126
/// The string representation of the error, and its `source` chain can roundtrip across
121127
/// the serialization. The actual types are lost (downcast will not work).

0 commit comments

Comments
 (0)