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
use crate::cell::finalizer::Finalizer;
use crate::cell::{Cell, CellContainer, CellFamily};

/// BOC decoder implementation.
pub mod de;

/// BOC file magic number.
#[derive(Default, Copy, Clone, Eq, PartialEq)]
pub enum BocTag {
    Indexed,
    IndexedCrc32,
    #[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];

    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,
        }
    }

    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>
where
    CellContainer<C>: AsRef<dyn Cell<C>>,
{
    /// 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: CellFamily>(data: &[u8]) -> Result<CellContainer<C>, de::Error>
        where
            CellContainer<C>: AsRef<dyn Cell<C>>,
        {
            Boc::<C>::decode_ext(data, &mut C::default_finalizer())
        }
        decode_impl::<C>(data.as_ref())
    }

    /// 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 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: CellFamily>(data: &[u8]) -> Result<CellContainerPair<C>, de::Error>
        where
            CellContainer<C>: AsRef<dyn Cell<C>>,
        {
            Boc::<C>::decode_pair_ext(data, &mut C::default_finalizer())
        }
        decode_pair_impl::<C>(data.as_ref())
    }

    /// 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)
    }
}

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