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};
13pub mod native;
14/// Optional host-side TypeScript generation; never required by a canister.
15#[cfg(feature = "typescript")]
16pub mod typescript;
17pub use bounded::{Bytes, BytesRef, List, PackedI16, Text, TextRef};
18pub use float::{FiniteF32, FiniteF64};
19pub use ic_wire_derive::Wire;
20
21/// Implementation dependencies used by generated contracts.
22#[doc(hidden)]
23pub mod __private {
24    pub use serde;
25    #[cfg(feature = "typescript")]
26    pub use serde_json;
27}
28
29/// Typed codec/admission failure, distinct from transport and application errors.
30#[derive(Clone, Copy, Debug, Eq, PartialEq)]
31pub enum Error {
32    /// Wire, collection or text allowance exceeded.
33    Limit,
34    /// Allocation failed within an admitted allowance.
35    Allocation,
36    /// Invalid or truncated Postcard representation.
37    Malformed,
38    /// Bytes remain after the expected value.
39    Trailing,
40    /// A valid value used a noncanonical representation.
41    Noncanonical,
42    /// Envelope marker, method identity or direction differs.
43    Identity,
44}
45impl core::fmt::Display for Error {
46    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47        write!(f, "wire {self:?}")
48    }
49}
50impl core::error::Error for Error {}
51
52/// Consumer-selected allowances for Serde traversal.
53#[derive(Clone, Copy, Debug)]
54pub struct Limits {
55    /// Maximum encoding bytes; framed APIs include the 11-byte header.
56    pub bytes: usize,
57    /// Maximum value heap plus codec scratch (excludes caller input).
58    pub allocation: usize,
59    /// Maximum visited value nodes, including unit elements.
60    pub nodes: usize,
61    /// Maximum nesting depth.
62    pub depth: usize,
63}