bitcoin_internals/
serde.rs1#[cfg(feature = "serde")]
4#[doc(hidden)]
5pub use serde::{de, ser, Deserialize, Deserializer, Serialize, Serializer};
6
7#[cfg(feature = "serde")]
12pub trait IntoDeError: Sized {
13 fn into_de_error<E: de::Error>(self, expected: Option<&dyn de::Expected>) -> E;
18
19 fn try_into_de_error<E>(self, expected: Option<&dyn de::Expected>) -> Result<E, Self>
24 where
25 E: de::Error,
26 {
27 Ok(self.into_de_error(expected))
28 }
29}
30
31#[cfg(feature = "serde")]
32mod impls {
33 use super::*;
34
35 impl IntoDeError for core::convert::Infallible {
36 fn into_de_error<E: de::Error>(self, _expected: Option<&dyn de::Expected>) -> E {
37 match self {}
38 }
39 }
40
41 impl IntoDeError for core::num::ParseIntError {
42 fn into_de_error<E: de::Error>(self, expected: Option<&dyn de::Expected>) -> E {
43 self.try_into_de_error(expected).unwrap_or_else(|_| {
44 let expected = expected.unwrap_or(&"an integer");
45
46 E::custom(format_args!("invalid string, expected {}", expected))
47 })
48 }
49
50 #[cfg(rust_v_1_55)]
51 fn try_into_de_error<E>(self, expected: Option<&dyn de::Expected>) -> Result<E, Self>
52 where
53 E: de::Error,
54 {
55 use core::num::IntErrorKind::Empty;
56
57 let expected = expected.unwrap_or(&"an integer");
58
59 match self.kind() {
60 Empty => Ok(E::invalid_value(de::Unexpected::Str(""), expected)),
61 _ => Err(self),
62 }
63 }
64
65 #[cfg(not(rust_v_1_55))]
66 fn try_into_de_error<E>(self, _expected: Option<&dyn de::Expected>) -> Result<E, Self>
67 where
68 E: de::Error,
69 {
70 Err(self)
71 }
72 }
73}