Skip to main content

ic_db_cli/
fixture_wire.rs

1//! Bounded sparse frames around the private console's positional Serde contracts.
2use ic_wire::{Encoding, Error, Limits, evolution};
3use serde::{Deserialize, Serialize};
4
5/// Encode the fixture body with an empty root extension block.
6/// # Errors
7/// Returns the Wire encoding or budget refusal.
8pub fn encode<T: Serialize + ?Sized>(
9    value: &T,
10    encoding: Encoding,
11    limits: Limits,
12) -> Result<Vec<u8>, Error> {
13    let mut bytes = Vec::new();
14    evolution::encode_projected_into(&mut bytes, encoding, limits, |_| {
15        Ok(evolution::Projected::empty(value))
16    })?;
17    Ok(bytes)
18}
19
20/// Decode the positional body and validate any unrecognized extension fields.
21/// # Errors
22/// Returns the Wire framing, decoding or budget refusal.
23pub fn decode<'de, T: Deserialize<'de>>(
24    bytes: &'de [u8],
25    encoding: Encoding,
26    limits: Limits,
27) -> Result<T, Error> {
28    evolution::decode_projected(bytes, encoding, limits, |message, context| {
29        message.extensions.visit(context, |_, _| Ok(()))?;
30        Ok(message.core)
31    })
32}
33
34#[cfg(test)]
35mod tests {
36    use super::*;
37
38    #[test]
39    fn fixture_projection_accepts_optional_extensions_and_checks_their_headers() {
40        let limits = Limits {
41            bytes: 64,
42            nodes: 64,
43            depth: 8,
44            allocation: 1024,
45        };
46        let payload = Encoding::Payload;
47        let body = 7_u8;
48        assert_eq!(
49            encode(&body, payload, limits),
50            ic_wire::encode(&ic_wire::Message(body), payload, limits)
51        );
52        assert_eq!(decode::<u8>(&[7, 3, 1, 1, 9], payload, limits), Ok(body));
53        assert!(decode::<u8>(&[7, 3, 0, 1, 9], payload, limits).is_err());
54        assert!(decode::<u8>(&[7], payload, limits).is_err());
55    }
56}