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
289
290
291
292
293
294
295
296
297
298
299
300
301
//! BOC (Bag Of Cells) implementation.

use std::borrow::Borrow;

use crate::cell::{
    Cell, CellBuilder, CellContainer, CellFamily, DefaultFinalizer, Finalizer, Load, Store,
};

/// BOC decoder implementation.
pub mod de;
/// BOC encoder implementation.
pub mod ser;

/// BOC file magic number.
#[derive(Default, Copy, Clone, Eq, PartialEq)]
pub enum BocTag {
    /// Single root, cells index, no CRC32.
    Indexed,
    /// Single root, cells index, with CRC32.
    IndexedCrc32,
    /// Multiple roots, optional cells index, optional CRC32.
    #[default]
    Generic,
}

impl BocTag {
    const INDEXED: [u8; 4] = [0x68, 0xff, 0x65, 0xf3];
    const INDEXED_CRC32: [u8; 4] = [0xac, 0xc3, 0xa7, 0x28];
    const GENERIC: [u8; 4] = [0xb5, 0xee, 0x9c, 0x72];

    /// Tries to match bytes with BOC tag.
    pub const fn from_bytes(data: [u8; 4]) -> Option<Self> {
        match data {
            Self::GENERIC => Some(Self::Generic),
            Self::INDEXED_CRC32 => Some(Self::IndexedCrc32),
            Self::INDEXED => Some(Self::Indexed),
            _ => None,
        }
    }

    /// Converts BOC tag to bytes.
    pub const fn to_bytes(self) -> [u8; 4] {
        match self {
            Self::Indexed => Self::INDEXED,
            Self::IndexedCrc32 => Self::INDEXED_CRC32,
            Self::Generic => Self::GENERIC,
        }
    }
}

/// BOC (Bag Of Cells) helper.
pub struct Boc<C> {
    _cell_type: std::marker::PhantomData<C>,
}

impl<C: CellFamily> Boc<C> {
    /// Encodes the specified cell tree as BOC and
    /// returns the `base64` encoded bytes as a string.
    #[cfg(any(feature = "base64", test))]
    pub fn encode_base64<'a, T>(cell: T) -> String
    where
        T: Borrow<dyn Cell<C> + 'a>,
    {
        crate::util::encode_base64(Self::encode(cell))
    }

    /// Encodes the specified cell tree as BOC.
    pub fn encode<'a, T>(cell: T) -> Vec<u8>
    where
        T: Borrow<dyn Cell<C> + 'a>,
    {
        fn encode_impl<C: CellFamily>(cell: &dyn Cell<C>) -> Vec<u8> {
            let mut result = Vec::new();
            ser::BocHeader::<C>::new(cell).encode(&mut result);
            result
        }
        encode_impl(cell.borrow())
    }

    /// Encodes a pair of cell trees as BOC.
    pub fn encode_pair<'a, T1, T2>((cell1, cell2): (T1, T2)) -> Vec<u8>
    where
        T1: Borrow<dyn Cell<C> + 'a>,
        T2: Borrow<dyn Cell<C> + 'a>,
    {
        fn encode_pair_impl<C: CellFamily>(cell1: &dyn Cell<C>, cell2: &dyn Cell<C>) -> Vec<u8> {
            let mut result = Vec::new();
            let mut encoder = ser::BocHeader::<C>::new(cell1);
            encoder.add_root(cell2);
            encoder.encode(&mut result);
            result
        }
        encode_pair_impl(cell1.borrow(), cell2.borrow())
    }
}

impl<C: DefaultFinalizer> Boc<C> {
    /// Decodes a `base64` encoded BOC into a cell tree
    /// using the default Cell family finalizer.
    #[cfg(any(feature = "base64", test))]
    #[inline]
    pub fn decode_base64<T: AsRef<[u8]>>(data: T) -> Result<CellContainer<C>, de::Error> {
        fn decode_base64_impl<C: DefaultFinalizer>(
            data: &[u8],
        ) -> Result<CellContainer<C>, de::Error> {
            match crate::util::decode_base64(data) {
                Ok(data) => Boc::<C>::decode_ext(data.as_slice(), &mut C::default_finalizer()),
                Err(_) => Err(de::Error::UnknownBocTag),
            }
        }
        decode_base64_impl::<C>(data.as_ref())
    }

    /// Decodes a cell tree using the default Cell family finalizer.
    #[inline]
    pub fn decode<T>(data: T) -> Result<CellContainer<C>, de::Error>
    where
        T: AsRef<[u8]>,
    {
        fn decode_impl<C: DefaultFinalizer>(data: &[u8]) -> Result<CellContainer<C>, de::Error> {
            Boc::<C>::decode_ext(data, &mut C::default_finalizer())
        }
        decode_impl::<C>(data.as_ref())
    }

    /// Decodes a pair of cell trees using the default Cell family finalizer.
    #[inline]
    pub fn decode_pair<T>(data: T) -> Result<CellContainerPair<C>, de::Error>
    where
        T: AsRef<[u8]>,
    {
        fn decode_pair_impl<C: DefaultFinalizer>(
            data: &[u8],
        ) -> Result<CellContainerPair<C>, de::Error> {
            Boc::<C>::decode_pair_ext(data, &mut C::default_finalizer())
        }
        decode_pair_impl::<C>(data.as_ref())
    }
}

impl<C: CellFamily> Boc<C> {
    /// Decodes a cell tree using the specified finalizer.
    pub fn decode_ext(
        data: &[u8],
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<CellContainer<C>, de::Error> {
        use self::de::*;

        let header = ok!(de::BocHeader::decode(
            data,
            &Options {
                max_roots: Some(1),
                min_roots: Some(1),
            },
        ));

        if let Some(&root) = header.roots().first() {
            let cells = ok!(header.finalize(finalizer));
            if let Some(root) = cells.get(root) {
                return Ok(root);
            }
        }

        Err(de::Error::RootCellNotFound)
    }

    /// Decodes a pair of cell trees using the specified finalizer.
    pub fn decode_pair_ext(
        data: &[u8],
        finalizer: &mut dyn Finalizer<C>,
    ) -> Result<CellContainerPair<C>, de::Error> {
        use self::de::*;

        let header = ok!(de::BocHeader::decode(
            data,
            &Options {
                max_roots: Some(2),
                min_roots: Some(2),
            },
        ));

        let mut roots = header.roots().iter();
        if let (Some(&root1), Some(&root2)) = (roots.next(), roots.next()) {
            let cells = ok!(header.finalize(finalizer));
            if let (Some(root1), Some(root2)) = (cells.get(root1), cells.get(root2)) {
                return Ok((root1, root2));
            }
        }

        Err(de::Error::RootCellNotFound)
    }
}

/// BOC representation helper.
pub struct BocRepr<C> {
    _cell_type: std::marker::PhantomData<C>,
}

impl<C: DefaultFinalizer> BocRepr<C> {
    /// Encodes the specified cell tree as BOC using default finalizer and
    /// returns the `base64` encoded bytes as a string.
    #[cfg(any(feature = "base64", test))]
    pub fn encode_base64<T>(data: T) -> Option<String>
    where
        T: Store<C>,
    {
        let boc = Self::encode_ext(data, &mut C::default_finalizer())?;
        Some(crate::util::encode_base64(boc))
    }

    /// Encodes the specified cell tree as BOC using default finalizer.
    pub fn encode<T>(data: T) -> Option<Vec<u8>>
    where
        T: Store<C>,
    {
        Self::encode_ext(data, &mut C::default_finalizer())
    }

    /// Decodes a `base64` encoded BOC into an object
    /// using the default Cell family finalizer.
    #[cfg(any(feature = "base64", test))]
    #[inline]
    pub fn decode_base64<T, D>(data: D) -> Result<T, BocReprError>
    where
        for<'a> T: Load<'a, C>,
        D: AsRef<[u8]>,
    {
        fn decode_base64_impl<C, T>(data: &[u8]) -> Result<T, BocReprError>
        where
            C: DefaultFinalizer,
            for<'a> T: Load<'a, C>,
        {
            match crate::util::decode_base64(data) {
                Ok(data) => BocRepr::<C>::decode_ext(data.as_slice(), &mut C::default_finalizer()),
                Err(_) => Err(BocReprError::InvalidBoc(de::Error::UnknownBocTag)),
            }
        }
        decode_base64_impl::<C, T>(data.as_ref())
    }

    /// Decodes an object using the default Cell family finalizer.
    #[inline]
    pub fn decode<T, D>(data: D) -> Result<T, BocReprError>
    where
        for<'a> T: Load<'a, C>,
        D: AsRef<[u8]>,
    {
        fn decode_impl<C, T>(data: &[u8]) -> Result<T, BocReprError>
        where
            C: DefaultFinalizer,
            for<'a> T: Load<'a, C>,
        {
            BocRepr::<C>::decode_ext(data, &mut C::default_finalizer())
        }
        decode_impl::<C, T>(data.as_ref())
    }
}

impl<C: CellFamily> BocRepr<C> {
    /// Encodes the specified object as BOC.
    pub fn encode_ext<T>(data: T, finalizer: &mut dyn Finalizer<C>) -> Option<Vec<u8>>
    where
        T: Store<C>,
    {
        let mut builder = CellBuilder::<C>::new();
        if !data.store_into(&mut builder, finalizer) {
            return None;
        }

        let cell = builder.build_ext(finalizer)?;
        Some(Boc::<C>::encode(cell))
    }

    /// Decodes object from BOC using the specified finalizer.
    pub fn decode_ext<T>(data: &[u8], finalizer: &mut dyn Finalizer<C>) -> Result<T, BocReprError>
    where
        for<'a> T: Load<'a, C>,
    {
        let cell = match Boc::<C>::decode_ext(data, finalizer) {
            Ok(cell) => cell,
            Err(e) => return Err(BocReprError::InvalidBoc(e)),
        };
        match T::load_from(&mut cell.as_ref().as_slice()) {
            Some(data) => Ok(data),
            None => Err(BocReprError::InvalidData),
        }
    }
}

/// Error type for BOC repr decoding related errors.
#[derive(Debug, thiserror::Error)]
pub enum BocReprError {
    /// Failed to decode BOC.
    #[error("invalid BOC")]
    InvalidBoc(#[source] de::Error),
    /// Failed to decode data from cells.
    #[error("failed to decode object from cells")]
    InvalidData,
}

type CellContainerPair<C> = (CellContainer<C>, CellContainer<C>);