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
use crate::rust::prelude::*;
use crate::traversal::*;
use crate::*;

/// A wrapper for a full encoded SBOR payload, including the prefix byte.
///
/// Encode is implemented, but Decode is not - because the payload needs to be checked to be valid.
/// Instead, you can use the typed or untyped traverser to validate the payload.
///
/// The payload is assumed to be valid, and for performance, the payload is
/// encoded directly without checking if it is valid.
///
/// If you need to check the validity of a payload first:
/// * If you have a schema - use the typed traverser
/// * If it is schemaless - use the untyped traverser
pub struct RawPayload<'a, E: CustomExtension> {
    full_payload: Cow<'a, [u8]>,
    root_value_kind: ValueKind<E::CustomValueKind>,
    custom_extension: PhantomData<E>,
}

impl<'a, E: CustomExtension> RawPayload<'a, E> {
    /// The bytes should include the prefix byte (eg 0x5b for basic SBOR).
    ///
    /// It is the caller's responsibility to ensure that a valid SBOR payload for extension E is
    /// passed to the caller.
    ///
    /// This constructor does not check the prefix byte, and panics if the root value kind is invalid.
    pub fn new_from_valid_slice(payload_bytes: &'a [u8]) -> Self {
        Self {
            full_payload: Cow::Borrowed(payload_bytes),
            root_value_kind: ValueKind::<E::CustomValueKind>::from_u8(payload_bytes[1]).unwrap(),
            custom_extension: PhantomData,
        }
    }

    /// The bytes should include the prefix byte (eg 0x5b for basic SBOR).
    ///
    /// It is the caller's responsibility to ensure that a valid SBOR payload for extension E is
    /// passed to the caller.
    ///
    /// Unlike `new_from_valid_payload_bytes`, the constructor includes a prefix byte check to hopefully
    /// catch some errors - but it is not guaranteed to catch all errors.
    pub fn new_from_valid_slice_with_checks(payload_bytes: &'a [u8]) -> Option<Self> {
        if payload_bytes.len() < 2 || payload_bytes[0] != E::PAYLOAD_PREFIX {
            return None;
        }
        let Some(value_kind) = ValueKind::<E::CustomValueKind>::from_u8(payload_bytes[1]) else {
            return None;
        };
        Some(Self {
            full_payload: Cow::Borrowed(payload_bytes),
            root_value_kind: value_kind,
            custom_extension: PhantomData,
        })
    }

    /// The bytes should include the prefix byte (eg 0x5b for basic SBOR).
    ///
    /// It is the caller's responsibility to ensure that a valid SBOR payload for extension E is
    /// passed to the caller.
    ///
    /// This constructor does not check the prefix byte, and panics if the root value kind is invalid.
    pub fn new_from_valid_owned(payload_bytes: Vec<u8>) -> Self {
        let root_value_kind = ValueKind::<E::CustomValueKind>::from_u8(payload_bytes[1]).unwrap();
        Self {
            full_payload: Cow::Owned(payload_bytes),
            root_value_kind,
            custom_extension: PhantomData,
        }
    }

    /// The bytes should include the prefix byte (eg 0x5b for basic SBOR).
    ///
    /// It is the caller's responsibility to ensure that a valid SBOR payload for extension E is
    /// passed to the caller.
    ///
    /// Unlike `new_from_valid_payload_bytes`, the constructor includes a prefix byte check to hopefully
    /// catch some errors - but it is not guaranteed to catch all errors.
    pub fn new_from_valid_owned_with_checks(payload_bytes: Vec<u8>) -> Option<Self> {
        if payload_bytes.len() < 2 || payload_bytes[0] != E::PAYLOAD_PREFIX {
            return None;
        }
        let Some(value_kind) = ValueKind::<E::CustomValueKind>::from_u8(payload_bytes[1]) else {
            return None;
        };
        Some(Self {
            full_payload: Cow::Owned(payload_bytes),
            root_value_kind: value_kind,
            custom_extension: PhantomData,
        })
    }

    pub fn as_encoded_value<'b>(&'b self) -> RawValue<'b, E> {
        RawValue::new_from_valid_value_body_slice(
            self.root_value_kind,
            self.encoded_root_body_bytes(),
        )
    }

    pub fn decode_into<
        T: for<'b> Decode<E::CustomValueKind, VecDecoder<'b, E::CustomValueKind>>,
    >(
        &self,
        depth_limit: usize,
    ) -> Result<T, DecodeError> {
        let mut decoder = VecDecoder::new(self.encoded_root_body_bytes(), depth_limit);
        T::decode_body_with_value_kind(&mut decoder, self.root_value_kind)
    }

    pub fn root_value_kind(&self) -> ValueKind<E::CustomValueKind> {
        self.root_value_kind
    }

    pub fn payload_bytes(&self) -> &[u8] {
        &self.full_payload
    }

    pub fn encoded_root_value_bytes(&self) -> &[u8] {
        &self.full_payload[1..]
    }

    pub fn encoded_root_body_bytes(&self) -> &[u8] {
        &self.full_payload[2..]
    }
}

impl<'a, E: CustomExtension> TryFrom<&'a [u8]> for RawPayload<'a, E> {
    type Error = ();

    fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
        RawPayload::new_from_valid_slice_with_checks(value).ok_or(())
    }
}

impl<Ext: CustomExtension, E: Encoder<Ext::CustomValueKind>> Encode<Ext::CustomValueKind, E>
    for RawPayload<'_, Ext>
{
    fn encode_value_kind(&self, encoder: &mut E) -> Result<(), EncodeError> {
        encoder.write_value_kind(self.root_value_kind)
    }

    fn encode_body(&self, encoder: &mut E) -> Result<(), EncodeError> {
        encoder.write_slice(self.encoded_root_body_bytes())
    }
}

/// A wrapper for a reference to a valid partial SBOR payload representing a single value.
///
/// Encode, Decode and Describe are implemented:
/// * For performance, the payload is encoded directly without checking if it is valid.
/// * Decoding goes through a traverser, which calculates the length of the bytes and ensures that the bytes are actually valid.
/// * Describe is implemented as Any.
///
/// Categorize can't be implemented, because we can't guarantee the value kind is constant.
/// This means RawValue can't be put as an immediate child to a Vec or Map.
#[derive(Debug, Clone)]
pub struct RawValue<'a, E: CustomExtension> {
    value_kind: ValueKind<E::CustomValueKind>,
    value_body: Cow<'a, [u8]>,
    custom_extension: PhantomData<E>,
}

impl<'a, E: CustomExtension> RawValue<'a, E> {
    /// The bytes should include the value kind byte, but not the payload prefix byte.
    ///
    /// The bytes must be at least 1 byte long, else this will panic.
    pub fn new_from_valid_full_value_slice(encoded_full_value: &'a [u8]) -> Self {
        let value_kind = ValueKind::from_u8(encoded_full_value[0]).unwrap();
        Self {
            value_kind,
            value_body: Cow::Borrowed(&encoded_full_value[1..]),
            custom_extension: PhantomData,
        }
    }

    /// The bytes should include the value, not the value kind or the prefix byte
    pub fn new_from_valid_value_body_slice(
        value_kind: ValueKind<E::CustomValueKind>,
        encoded_value_body: &'a [u8],
    ) -> Self {
        Self {
            value_kind,
            value_body: Cow::Borrowed(encoded_value_body),
            custom_extension: PhantomData,
        }
    }

    /// The bytes should include the value, not the value kind or the prefix byte
    pub fn new_from_valid_owned_value_body(
        value_kind: ValueKind<E::CustomValueKind>,
        encoded_value_body: Vec<u8>,
    ) -> Self {
        Self {
            value_kind,
            value_body: Cow::Owned(encoded_value_body),
            custom_extension: PhantomData,
        }
    }

    pub fn value_kind(&self) -> ValueKind<E::CustomValueKind> {
        self.value_kind
    }

    pub fn value_body_bytes(&self) -> &[u8] {
        &self.value_body
    }
}

impl<Ext: CustomExtension, E: Encoder<Ext::CustomValueKind>> Encode<Ext::CustomValueKind, E>
    for RawValue<'_, Ext>
{
    fn encode_value_kind(&self, encoder: &mut E) -> Result<(), EncodeError> {
        encoder.write_value_kind(self.value_kind)
    }

    fn encode_body(&self, encoder: &mut E) -> Result<(), EncodeError> {
        encoder.write_slice(self.value_body.as_ref())
    }
}

impl<Ext: CustomExtension, D: Decoder<Ext::CustomValueKind>> Decode<Ext::CustomValueKind, D>
    for RawValue<'_, Ext>
{
    fn decode_body_with_value_kind(
        decoder: &mut D,
        value_kind: ValueKind<Ext::CustomValueKind>,
    ) -> Result<Self, DecodeError> {
        // Because SBOR isn't a length-first decoding, you don't know how long a value "tree" is until you've decoded it.
        // So we use a traverser to calculate the length of the subpayload, and then read that many bytes.
        let length = calculate_value_tree_body_byte_length::<Ext>(
            decoder.peek_remaining(),
            value_kind,
            decoder.get_stack_depth(),
            decoder.get_depth_limit(),
        )?;
        // Because Decode doesn't (currently) allow borrowing from the decoder, we have to to_vec here
        Ok(Self::new_from_valid_owned_value_body(
            value_kind,
            decoder.read_slice(length)?.to_vec(),
        ))
    }
}

impl<Ext: CustomExtension, C: CustomTypeKind<RustTypeId>> Describe<C> for RawValue<'_, Ext> {
    const TYPE_ID: RustTypeId = RustTypeId::WellKnown(basic_well_known_types::ANY_TYPE);

    fn type_data() -> TypeData<C, RustTypeId> {
        basic_well_known_types::any_type_data()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[derive(BasicSbor)]
    struct RawValueStruct {
        field1: BasicOwnedRawValue,
        field2: (BasicOwnedRawValue, BasicOwnedRawValue),
    }

    #[test]
    pub fn can_encode_and_decode_raw_value() {
        let encoded = basic_encode(&BasicValue::Tuple {
            fields: vec![
                // Field1
                BasicValue::Enum {
                    discriminator: 1,
                    fields: vec![],
                },
                // Field2
                BasicValue::Tuple {
                    fields: vec![BasicValue::U8 { value: 1 }, BasicValue::U16 { value: 5125 }],
                },
            ],
        })
        .unwrap();
        let decoded: RawValueStruct = basic_decode(&encoded).unwrap();
        // Check that the content of the raw value makes sense
        assert_eq!(decoded.field2.1.value_kind, ValueKind::U16);
        assert_eq!(
            decoded.field2.1.value_body_bytes(),
            // Extract the value body (ie remove the payload prefix byte and the value kind byte)
            &basic_encode(&5125u16).unwrap()[2..],
        );
        // Check that it can be encoded back to the original value
        let encoded2 = basic_encode(&decoded).unwrap();
        assert_eq!(encoded, encoded2);
    }
}