Skip to main content

ic_wire/
lib.rs

1//! Bounded Postcard contracts for controlled IC endpoints.
2//!
3//! Transport, authentication, semantic validation and replay policy belong to
4//! consumers. See the runtime guide for maintained wire rules.
5#![no_std]
6#![doc = include_str!("../README.md")]
7extern crate alloc;
8mod bounded;
9mod contract;
10mod float;
11mod packed;
12#[cfg(all(feature = "platform-types", not(target_family = "wasm")))]
13mod platform_types;
14pub use packed::{Packed, PackedElement};
15/// Bounded Serde/Postcard encoding, decoding and request/response framing.
16pub mod native;
17/// Optional TypeScript generation on native hosts.
18/// Forwarding the feature on Wasm retains only the runtime codec.
19#[cfg(all(feature = "typescript", not(target_family = "wasm")))]
20pub mod typescript;
21pub use bounded::{Bytes, BytesRef, List, PackedI16, Text, TextRef};
22pub use float::{FiniteF32, FiniteF64};
23#[cfg(feature = "typescript")]
24pub use ic_wire_derive::Wire;
25
26/// Owned binary data using Serde's bulk byte-buffer representation.
27///
28/// This is the same type as `serde_bytes::ByteBuf`, available without a separate
29/// consumer dependency. Use [`Bytes<N>`] when the field needs a byte limit.
30/// Native [`Limits`] still apply; TypeScript metadata projects to `Uint8Array`.
31/// Compared with `Vec<u8>`, bytes are identical but admission is bulk rather
32/// than per-element. Existing ByteBuf bytes, limits and primitive checks are
33/// unchanged by this re-export; no migration is needed from serde_bytes::ByteBuf.
34pub use serde_bytes::ByteBuf;
35
36/// Fixed-width binary data using Serde's bulk byte representation.
37///
38/// This re-exports `serde_bytes::ByteArray<N>`; no consumer dependency is needed.
39/// TypeScript projects it to `Uint8Array` with length exactly `N`.
40/// Unlike `[u8; N]`, its Postcard bytes include a length prefix: `[0, 255]`
41/// is `00 ff` as an array and `02 00 ff` as `ByteArray<2>`. Adopting this type
42/// changes the contract and bulk admission; coordinate consumers, regenerate
43/// clients and qualify limits. Existing array layouts remain unchanged.
44pub use serde_bytes::ByteArray;
45
46/// Implementation dependencies used by generated contracts.
47#[doc(hidden)]
48pub mod __private {
49    pub use serde;
50    #[cfg(all(feature = "typescript", not(target_family = "wasm")))]
51    pub use serde_json;
52}
53
54/// Typed codec/admission failure, distinct from transport and application errors.
55#[derive(Clone, Copy, Debug, Eq, PartialEq)]
56pub enum Error {
57    /// Wire, collection, text or caller-output allowance exceeded.
58    Limit,
59    /// Allocation failed within an admitted allowance.
60    Allocation,
61    /// Invalid or truncated Postcard representation.
62    Malformed,
63    /// Bytes remain after the expected value.
64    Trailing,
65    /// Envelope marker, method identity or direction differs.
66    Identity,
67}
68impl core::fmt::Display for Error {
69    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
70        // Display must survive builds that strip derived Debug implementations.
71        // Text changes from e.g. "wire Malformed" to "wire malformed"; enum
72        // meanings, protocol bytes and admission rules remain unchanged.
73        f.write_str(match self {
74            Self::Limit => "wire limit",
75            Self::Allocation => "wire allocation",
76            Self::Malformed => "wire malformed",
77            Self::Trailing => "wire trailing",
78            Self::Identity => "wire identity",
79        })
80    }
81}
82impl core::error::Error for Error {}
83
84/// Consumer-selected allowances for Serde traversal.
85#[derive(Clone, Copy, Debug)]
86pub struct Limits {
87    /// Maximum encoding bytes; framed APIs include the 11-byte header.
88    pub bytes: usize,
89    /// Conservative traversal allowance in bytes, not measured heap usage.
90    /// Reserves the entire [`Self::bytes`] allowance, then charges eight times
91    /// visited value sizes and text/byte lengths. Excludes caller input.
92    /// Encoding and decoding can require different allowances; see the crate guide.
93    pub allocation: usize,
94    /// Maximum visited value nodes, including unit elements.
95    pub nodes: usize,
96    /// Maximum nesting depth.
97    pub depth: usize,
98}