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