1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//! An IPLD type that borrows most of it's contents from an underlying type.
//!
//! IPLD introduces two fundamental concepts, Data Types and Schemas/Representations.
//! In short, Data Types are a small set of common types (lists, maps, links, etc)
//! necessary for generally modelling Linked Data and can be serialized/deserialized
//! by an IPLD CodecExt. However, more advanced types might benefit from having schemas,
//! alternate serialization/deserialization behaviour, or runtime dependencies to
//! aid in verification when encoding/decoding the type to/from raw blocks.
//!
//! This type fulfills the role of a low-allocation mapping between IPLD Schemas
//! /Representations and underlying Codecs by actually providing two mappings:
//!     - one from IPLD Data type <-> IPLD CodecExt (via Serde data model)
//!     - one from Rust types, schemas & representations <-> IPLD (via `TryFrom`)
//! In the first case, Serde only borrows from the type on serialization, and
//! whenever possible provides borrowed types on deserialization. Likewise,
//! `TryFrom<Ipld>` and `TryInto<Ipld>` are implemented to borrow
//! many of their fields or lazily iterate over them.

use crate::CodecExt;
use cid::Cid;
use serde::{
    de::{self, Visitor},
    serde_if_integer128, Deserialize, Deserializer, Serialize, Serializer,
};
use std::{
    cell::RefCell, fmt, marker::PhantomData, slice::Iter as SliceIter, vec::IntoIter as VecIter,
};

/// Ipld that borrows from an underlying type.
#[derive(Clone, Debug)]
pub enum Ipld<'a, C>
where
    C: CodecExt,
{
    /// Represents the absence of a value or the value undefined.
    Null(PhantomData<C>),
    /// Represents a boolean value.
    Bool(bool),
    /// Represents an i8.
    Int8(i8),
    /// Represents an i16.
    Int16(i16),
    /// Represents an i32.
    Int32(i32),
    /// Represents an i64.
    Int64(i64),
    /// Represents an i128.
    Int128(i128),
    /// Represents an u8.
    Uint8(u8),
    /// Represents an u16.
    Uint16(u16),
    /// Represents an u32.
    Uint32(u32),
    /// Represents an u64.
    Uint64(u64),
    /// Represents an u128.
    Uint128(u128),
    /// Represents an f32.
    Float32(f32),
    /// Represents an f64.
    Float64(f64),
    /// Represents an UTF-8 string.
    String(&'a str),
    /// Represents a sequence of bytes.
    Bytes(&'a [u8]),
    /// Represents a list.
    List(IpldListIter<'a, C>),
    /// Represents a map.
    Map(IpldMapIter<'a, C>),
    /// Represents a link to an Ipld node
    Link(Cid),
}

// Iters

/// Wrapper around `Iterator`s commonly used with IPLD lists.
///
/// Uses `RefCell` to bypass serde's strict borrowing requirements.
#[derive(Clone, Debug)]
pub enum IpldListIter<'a, C: CodecExt> {
    Slice(RefCell<SliceIter<'a, Ipld<'a, C>>>),
    Vec(RefCell<VecIter<Ipld<'a, C>>>),
}

impl<'a, C> From<SliceIter<'a, Ipld<'a, C>>> for IpldListIter<'a, C>
where
    C: CodecExt,
{
    fn from(iter: SliceIter<'a, Ipld<'a, C>>) -> Self {
        IpldListIter::Slice(RefCell::new(iter))
    }
}

impl<'a, C> From<VecIter<Ipld<'a, C>>> for IpldListIter<'a, C>
where
    C: CodecExt,
{
    fn from(iter: VecIter<Ipld<'a, C>>) -> Self {
        IpldListIter::Vec(RefCell::new(iter))
    }
}

/// Wrapper around `Iterator`s commonly used with IPLD maps.
///
/// Uses `RefCell` to bypass serde's strict borrowing requirements.
#[derive(Clone, Debug)]
pub enum IpldMapIter<'a, C: CodecExt> {
    Vec(RefCell<VecIter<(&'a str, Ipld<'a, C>)>>),
}

impl<'a, C> From<VecIter<(&'a str, Ipld<'a, C>)>> for IpldMapIter<'a, C>
where
    C: CodecExt,
{
    fn from(iter: VecIter<(&'a str, Ipld<'a, C>)>) -> Self {
        IpldMapIter::Vec(RefCell::new(iter))
    }
}

// Serde

impl<'a, C> Serialize for Ipld<'a, C>
where
    C: CodecExt,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Ipld::Null(_) => serializer.serialize_none(),
            Ipld::Bool(b) => serializer.serialize_bool(*b),
            Ipld::Int8(n) => serializer.serialize_i8(*n),
            Ipld::Int16(n) => serializer.serialize_i16(*n),
            Ipld::Int32(n) => serializer.serialize_i32(*n),
            Ipld::Int64(n) => serializer.serialize_i64(*n),
            Ipld::Int128(n) => serializer.serialize_i128(*n),
            Ipld::Uint8(n) => serializer.serialize_u8(*n),
            Ipld::Uint16(n) => serializer.serialize_u16(*n),
            Ipld::Uint32(n) => serializer.serialize_u32(*n),
            Ipld::Uint64(n) => serializer.serialize_u64(*n),
            Ipld::Uint128(n) => serializer.serialize_u128(*n),
            Ipld::Float32(n) => serializer.serialize_f32(*n),
            Ipld::Float64(n) => serializer.serialize_f64(*n),
            Ipld::String(s) => serializer.serialize_str(s),
            Ipld::Bytes(b) => C::serialize_bytes(*b, serializer),
            Ipld::List(list_iter) => match list_iter {
                IpldListIter::Slice(iter) => serializer.collect_seq(&mut *(iter.borrow_mut())),
                IpldListIter::Vec(iter) => serializer.collect_seq(&mut *(iter.borrow_mut())),
            },
            Ipld::Map(map_iter) => match map_iter {
                IpldMapIter::Vec(iter) => serializer.collect_map(&mut *(iter.borrow_mut())),
            },
            Ipld::Link(cid) => C::serialize_link(cid, serializer),
        }
    }
}

impl<'de, C> Deserialize<'de> for Ipld<'de, C>
where
    C: CodecExt,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_any(IpldVisitor(PhantomData))
    }
}

#[doc(hidden)]
#[macro_export(local_inner_macros)]
macro_rules! visit {
    ($type:ty : $visit_fn:ident $member:ident) => {
        #[inline]
        fn $visit_fn<E>(self, value: $type) -> Result<Self::Value, E>
        where
            E: de::Error,
        {
            Ok(Ipld::$member(value))
        }
    };
}

/// `Visitor` for `Deserialize`ing a `Ipld`.
struct IpldVisitor<C>(PhantomData<C>);

impl<'de, C> Visitor<'de> for IpldVisitor<C>
where
    C: 'de + CodecExt,
{
    type Value = Ipld<'de, C>;

    fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
        formatter.write_str("an IPLD dag")
    }

    #[inline]
    fn visit_none<E>(self) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Ipld::Null(PhantomData))
    }

    #[inline]
    fn visit_unit<E>(self) -> Result<Self::Value, E>
    where
        E: de::Error,
    {
        Ok(Ipld::Null(PhantomData))
    }

    visit!(bool : visit_bool Bool);
    visit!(i8 : visit_i8 Int8);
    visit!(i16 : visit_i16 Int16);
    visit!(i32 : visit_i32 Int32);
    visit!(i64 : visit_i64 Int64);
    visit!(u8 : visit_u8 Uint8);
    visit!(u16 : visit_u16 Uint16);
    visit!(u32 : visit_u32 Uint32);
    visit!(u64 : visit_u64 Uint64);
    visit!(f32 : visit_f32 Float32);
    visit!(f64 : visit_f64 Float64);

    serde_if_integer128! {
        visit!(i128 : visit_i128 Int128);
        visit!(u128 : visit_u128 Uint128);
    }

    visit!(&'de str : visit_borrowed_str String);
    visit!(&'de [u8] : visit_borrowed_bytes Bytes);

    #[inline]
    fn visit_newtype_struct<D>(self, deserializer: D) -> Result<Self::Value, D::Error>
    where
        D: de::Deserializer<'de>,
    {
        C::deserialize_unknown(deserializer, self)
    }

    #[inline]
    fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
    where
        A: de::SeqAccess<'de>,
    {
        let mut vec = if let Some(len) = seq.size_hint() {
            Vec::with_capacity(len)
        } else {
            Vec::new()
        };

        while let Some(ipld) = seq.next_element()? {
            vec.push(ipld);
        }
        Ok(Ipld::List(IpldListIter::from(vec.into_iter())))
    }

    #[inline]
    fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
    where
        A: de::MapAccess<'de>,
    {
        let mut vec = if let Some(len) = map.size_hint() {
            Vec::with_capacity(len)
        } else {
            Vec::new()
        };

        while let Some(ipld) = map.next_entry()? {
            vec.push(ipld);
        }
        Ok(Ipld::Map(IpldMapIter::from(vec.into_iter())))
    }
}

impl<'de, C> crate::IpldVisitor<'de> for IpldVisitor<C>
where
    C: 'de + CodecExt,
{
    fn visit_link<E>(self, cid: Cid) -> Result<<Self as Visitor<'de>>::Value, E>
    where
        E: de::Error,
    {
        Ok(Ipld::Link(cid))
    }
}