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
use super::*;
use std::collections::*;
use std::ffi::CString;

#[cfg(feature = "url")]
use url_inner::Url;

#[cfg(feature = "semver")]
use semver_inner::{Version, VersionReq};

/// # Implements base to be parsed by [Sparse](crate)
pub trait Sparsable {
    /// Initialize recusively a [Sparsable](Sparsable) pointer
    fn sparse_init(
        &mut self,
        state: &mut SparseState,
        metadata: &SparseMetadata,
        depth: u32,
    ) -> Result<(), SparseError>;

    /// Update recusively a [Sparsable](Sparsable) pointer
    fn sparse_updt(
        &mut self,
        state: &mut SparseState,
        metadata: &SparseMetadata,
        depth: u32,
    ) -> Result<(), SparseError> {
        self.sparse_init(state, metadata, depth)
    }

    /// Check if the current depth isn't too much.
    /// This is the cyclic pointer protection mechanism
    fn check_depth(&self, depth: u32) -> Result<(), SparseError> {
        match depth < MAX_SPARSE_DEPTH {
            true => Ok(()),
            false => Err(SparseError::CyclicRef),
        }
    }
}

macro_rules! impl_sparsable_nothing {
    ($x:ident) => {
        impl Sparsable for $x {
            fn sparse_init(
                &mut self,
                _state: &mut SparseState,
                _metadata: &SparseMetadata,
                _depth: u32,
            ) -> Result<(), SparseError> {
                Ok(())
            }
        }
    };
}

#[cfg(feature = "url")]
impl Sparsable for Url {
    fn sparse_init(
        &mut self,
        _state: &mut SparseState,
        _metadata: &SparseMetadata,
        _depth: u32,
    ) -> Result<(), SparseError> {
        Ok(())
    }
}

#[cfg(feature = "semver")]
impl Sparsable for Version {
    fn sparse_init(
        &mut self,
        _state: &mut SparseState,
        _metadata: &SparseMetadata,
        _depth: u32,
    ) -> Result<(), SparseError> {
        Ok(())
    }
}

#[cfg(feature = "semver")]
impl Sparsable for VersionReq {
    fn sparse_init(
        &mut self,
        _state: &mut SparseState,
        _metadata: &SparseMetadata,
        _depth: u32,
    ) -> Result<(), SparseError> {
        Ok(())
    }
}

impl Sparsable for serde_json::Value {
    fn sparse_init(
        &mut self,
        _state: &mut SparseState,
        _metadata: &SparseMetadata,
        _depth: u32,
    ) -> Result<(), SparseError> {
        Ok(())
    }
}

impl<T> Sparsable for Option<T>
where
    T: Serialize + DeserializeOwned + SparsableTrait,
{
    fn sparse_init(
        &mut self,
        state: &mut SparseState,
        metadata: &SparseMetadata,
        depth: u32,
    ) -> Result<(), SparseError> {
        match self {
            Some(x) => x.sparse_init(state, metadata, depth + 1)?,
            None => (),
        };
        Ok(())
    }

    fn sparse_updt(
        &mut self,
        state: &mut SparseState,
        metadata: &SparseMetadata,
        depth: u32,
    ) -> Result<(), SparseError> {
        match self {
            Some(x) => x.sparse_updt(state, metadata, depth + 1)?,
            None => (),
        };
        Ok(())
    }
}

impl<'a> Sparsable for &'a str {
    fn sparse_init(
        &mut self,
        _state: &mut SparseState,
        _metadata: &SparseMetadata,
        _depth: u32,
    ) -> Result<(), SparseError> {
        Ok(())
    }
}

impl<'a> Sparsable for &'a [u8] {
    fn sparse_init(
        &mut self,
        _state: &mut SparseState,
        _metadata: &SparseMetadata,
        _depth: u32,
    ) -> Result<(), SparseError> {
        Ok(())
    }
}

impl<K, V> Sparsable for HashMap<K, V>
where
    V: Sparsable,
{
    fn sparse_init(
        &mut self,
        state: &mut SparseState,
        metadata: &SparseMetadata,
        depth: u32,
    ) -> Result<(), SparseError> {
        let ndepth = depth + 1;
        for i in self.values_mut() {
            i.sparse_init(state, metadata, ndepth)?;
        }
        Ok(())
    }
}

macro_rules! impl_sparsable_iter {
    ($x:ident) => {
        impl<T> Sparsable for $x<T>
        where
            T: Sparsable,
        {
            fn sparse_init(
                &mut self,
                state: &mut SparseState,
                metadata: &SparseMetadata,
                depth: u32,
            ) -> Result<(), SparseError> {
                let ndepth = depth + 1;
                for i in self.iter_mut() {
                    i.sparse_init(state, metadata, ndepth)?;
                }
                Ok(())
            }
        }
    };
}

impl_sparsable_nothing!(bool);
impl_sparsable_nothing!(i8);
impl_sparsable_nothing!(i16);
impl_sparsable_nothing!(i32);
impl_sparsable_nothing!(i64);
impl_sparsable_nothing!(isize);
impl_sparsable_nothing!(u8);
impl_sparsable_nothing!(u16);
impl_sparsable_nothing!(u32);
impl_sparsable_nothing!(u64);
impl_sparsable_nothing!(i128);
impl_sparsable_nothing!(usize);
impl_sparsable_nothing!(f32);
impl_sparsable_nothing!(f64);
impl_sparsable_nothing!(char);
impl_sparsable_nothing!(String);
impl_sparsable_nothing!(CString);
impl_sparsable_iter!(Vec);
impl_sparsable_iter!(VecDeque);
impl_sparsable_iter!(LinkedList);