|
| 1 | +use std::fmt::Formatter; |
| 2 | + |
| 3 | +use super::ChunkInfo; |
| 4 | +use candid::types::{CandidType, Type, TypeInner}; |
| 5 | +use serde::de::{Deserialize, Deserializer, Error, IgnoredAny, MapAccess, SeqAccess, Visitor}; |
| 6 | +use serde_bytes::ByteArray; |
| 7 | +// ChunkInfo can be deserialized from both `blob` and `record { hash: blob }`. |
| 8 | +// This impl can be removed when both mainnet and dfx no longer return `blob`. |
| 9 | +impl CandidType for ChunkInfo { |
| 10 | + fn _ty() -> Type { |
| 11 | + Type(<_>::from(TypeInner::Unknown)) |
| 12 | + } |
| 13 | + fn idl_serialize<S>(&self, _serializer: S) -> Result<(), S::Error> |
| 14 | + where |
| 15 | + S: candid::types::Serializer, |
| 16 | + { |
| 17 | + unimplemented!() |
| 18 | + } |
| 19 | +} |
| 20 | +impl<'de> Deserialize<'de> for ChunkInfo { |
| 21 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 22 | + where |
| 23 | + D: Deserializer<'de>, |
| 24 | + { |
| 25 | + deserializer.deserialize_any(ChunkInfoVisitor) |
| 26 | + } |
| 27 | +} |
| 28 | +struct ChunkInfoVisitor; |
| 29 | +impl<'de> Visitor<'de> for ChunkInfoVisitor { |
| 30 | + type Value = ChunkInfo; |
| 31 | + fn expecting(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result { |
| 32 | + formatter.write_str("blob or record {hash: blob}") |
| 33 | + } |
| 34 | + fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E> |
| 35 | + where |
| 36 | + E: Error, |
| 37 | + { |
| 38 | + // deserialize_any combined with visit_bytes produces an extra 6 byte for difficult reasons |
| 39 | + let v = if v.len() == 33 && v[0] == 6 { |
| 40 | + &v[1..] |
| 41 | + } else { |
| 42 | + v |
| 43 | + }; |
| 44 | + Ok(ChunkInfo { |
| 45 | + hash: v |
| 46 | + .try_into() |
| 47 | + .map_err(|_| E::invalid_length(v.len(), &"32 bytes"))?, |
| 48 | + }) |
| 49 | + } |
| 50 | + fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error> |
| 51 | + where |
| 52 | + A: SeqAccess<'de>, |
| 53 | + { |
| 54 | + let mut hash = [0; 32]; |
| 55 | + for (i, n) in hash.iter_mut().enumerate() { |
| 56 | + *n = seq |
| 57 | + .next_element()? |
| 58 | + .ok_or_else(|| A::Error::invalid_length(i, &"32 bytes"))?; |
| 59 | + } |
| 60 | + if seq.next_element::<IgnoredAny>()?.is_some() { |
| 61 | + Err(A::Error::invalid_length( |
| 62 | + seq.size_hint().unwrap_or(33), |
| 63 | + &"32 bytes", |
| 64 | + )) |
| 65 | + } else { |
| 66 | + Ok(ChunkInfo { hash }) |
| 67 | + } |
| 68 | + } |
| 69 | + fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error> |
| 70 | + where |
| 71 | + A: MapAccess<'de>, |
| 72 | + { |
| 73 | + while let Some(k) = map.next_key::<Field>()? { |
| 74 | + eprintln!("here"); |
| 75 | + if matches!(k, Field::Hash) { |
| 76 | + return Ok(ChunkInfo { |
| 77 | + hash: map.next_value::<ByteArray<32>>()?.into_array(), |
| 78 | + }); |
| 79 | + } else { |
| 80 | + map.next_value::<IgnoredAny>()?; |
| 81 | + } |
| 82 | + } |
| 83 | + Err(A::Error::missing_field("hash")) |
| 84 | + } |
| 85 | +} |
| 86 | +// Needed because candid cannot infer field names without specifying them in _ty() |
| 87 | +enum Field { |
| 88 | + Hash, |
| 89 | + Other, |
| 90 | +} |
| 91 | +impl<'de> Deserialize<'de> for Field { |
| 92 | + fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 93 | + where |
| 94 | + D: Deserializer<'de>, |
| 95 | + { |
| 96 | + deserializer.deserialize_identifier(FieldVisitor) |
| 97 | + } |
| 98 | +} |
| 99 | +struct FieldVisitor; |
| 100 | +impl<'de> Visitor<'de> for FieldVisitor { |
| 101 | + type Value = Field; |
| 102 | + fn expecting(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result { |
| 103 | + formatter.write_str("a field name") |
| 104 | + } |
| 105 | + fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> |
| 106 | + where |
| 107 | + E: Error, |
| 108 | + { |
| 109 | + if v == "hash" { |
| 110 | + Ok(Field::Hash) |
| 111 | + } else { |
| 112 | + Ok(Field::Other) |
| 113 | + } |
| 114 | + } |
| 115 | + fn visit_u32<E>(self, v: u32) -> Result<Self::Value, E> |
| 116 | + where |
| 117 | + E: Error, |
| 118 | + { |
| 119 | + if v == 1158164430 { |
| 120 | + Ok(Field::Hash) |
| 121 | + } else { |
| 122 | + Ok(Field::Other) |
| 123 | + } |
| 124 | + } |
| 125 | +} |
0 commit comments