MCPcopy Create free account
hub / github.com/ElementsProject/lightning / deserialize

Method deserialize

plugins/lsps-plugin/src/proto/lsps0.rs:113–157  ·  view source on GitHub ↗
(deserializer: D)

Source from the content-addressed store, hash-verified

111
112impl<'de> Deserialize<'de> for Msat {
113 fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
114 where
115 D: Deserializer<'de>,
116 {
117 struct MsatVisitor;
118
119 impl<'de> de::Visitor<'de> for MsatVisitor {
120 type Value = Msat;
121
122 fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
123 formatter.write_str("a string representing a number")
124 }
125
126 fn visit_str<E>(self, value: &str) -> std::result::Result<Msat, E>
127 where
128 E: de::Error,
129 {
130 value
131 .parse::<u64>()
132 .map(Msat::from_msat)
133 .map_err(|_| E::custom(format!("Invalid number string: {}", value)))
134 }
135
136 // Also handle if JSON mistakenly has a number instead of string
137 fn visit_u64<E>(self, value: u64) -> std::result::Result<Msat, E>
138 where
139 E: de::Error,
140 {
141 Ok(Msat::from_msat(value))
142 }
143
144 fn visit_i64<E>(self, value: i64) -> std::result::Result<Msat, E>
145 where
146 E: de::Error,
147 {
148 if value < 0 {
149 Err(E::custom("Msat cannot be negative"))
150 } else {
151 Ok(Msat::from_msat(value as u64))
152 }
153 }
154 }
155
156 deserializer.deserialize_any(MsatVisitor)
157 }
158}
159
160/// Represents parts-per-million as defined in LSPS0.ppm. Gets it's own type

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected