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;
12pub use packed::{Packed, PackedElement};
13/// Optional typed IC entrypoints using Platform's export generator and executor.
14#[cfg(feature = "endpoint")]
15pub mod endpoint;
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};
23pub use ic_wire_derive::Wire;
24
25/// Owned binary data using Serde's bulk byte-buffer representation.
26///
27/// This is the same type as `serde_bytes::ByteBuf`, available without a separate
28/// consumer dependency. Use [`Bytes<N>`] when the field needs a byte limit.
29/// Native [`Limits`] still apply; TypeScript metadata projects to `Uint8Array`.
30/// Compared with `Vec<u8>`, bytes are identical but admission is bulk rather
31/// than per-element. Existing ByteBuf bytes, limits and primitive checks are
32/// unchanged by this re-export; no migration is needed from serde_bytes::ByteBuf.
33pub use serde_bytes::ByteBuf;
34
35/// Implementation dependencies used by generated contracts.
36#[doc(hidden)]
37pub mod __private {
38    pub use serde;
39    #[cfg(all(feature = "typescript", not(target_family = "wasm")))]
40    pub use serde_json;
41}
42
43/// Typed codec/admission failure, distinct from transport and application errors.
44#[derive(Clone, Copy, Debug, Eq, PartialEq)]
45pub enum Error {
46    /// Wire, collection, text or caller-output allowance exceeded.
47    Limit,
48    /// Allocation failed within an admitted allowance.
49    Allocation,
50    /// Invalid or truncated Postcard representation.
51    Malformed,
52    /// Bytes remain after the expected value.
53    Trailing,
54    /// Envelope marker, method identity or direction differs.
55    Identity,
56}
57impl core::fmt::Display for Error {
58    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
59        write!(f, "wire {self:?}")
60    }
61}
62impl core::error::Error for Error {}
63
64/// Consumer-selected allowances for Serde traversal.
65#[derive(Clone, Copy, Debug)]
66pub struct Limits {
67    /// Maximum encoding bytes; framed APIs include the 11-byte header.
68    pub bytes: usize,
69    /// Conservative traversal allowance in bytes, not measured heap usage.
70    /// Reserves the entire [`Self::bytes`] allowance, then charges eight times
71    /// visited value sizes and text/byte lengths. Excludes caller input.
72    /// Encoding and decoding can require different allowances; see the crate guide.
73    pub allocation: usize,
74    /// Maximum visited value nodes, including unit elements.
75    pub nodes: usize,
76    /// Maximum nesting depth.
77    pub depth: usize,
78}