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