Debian Codegraph @

third_party/rust/object/src/build/error.rs

raw
1 use alloc::string::String;
2 use core::{fmt, result};
3 #[cfg(feature = "std")]
4 use std::error;
5
6 use crate::{read, write};
7
8 /// The error type used within the build module.
9 #[derive(Debug, Clone, PartialEq, Eq)]
10 pub struct Error(pub(super) String);
11
12 impl Error {
13 pub(super) fn new(message: impl Into<String>) -> Self {
14 Error(message.into())
15 }
16 }
17
18 impl fmt::Display for Error {
19 #[inline]
20 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21 f.write_str(&self.0)
22 }
23 }
24
25 #[cfg(feature = "std")]
26 impl error::Error for Error {}
27
28 impl From<read::Error> for Error {
29 fn from(error: read::Error) -> Error {
30 Error(format!("{}", error))
31 }
32 }
33
34 impl From<write::Error> for Error {
35 fn from(error: write::Error) -> Error {
36 Error(error.0)
37 }
38 }
39
40 /// The result type used within the build module.
41 pub type Result<T> = result::Result<T, Error>;