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.
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/// Fixed-width binary data using Serde's bulk byte representation.
36///
37/// This re-exports `serde_bytes::ByteArray<N>`; no consumer dependency is needed.
38/// TypeScript projects it to `Uint8Array` with length exactly `N`.
39/// Unlike `[u8; N]`, its Postcard bytes include a length prefix: `[0, 255]`
40/// is `00 ff` as an array and `02 00 ff` as `ByteArray<2>`. Adopting this type
41/// changes the contract and bulk admission; coordinate consumers, regenerate
42/// clients and qualify limits. Existing array layouts remain unchanged.
43pub use serde_bytes::ByteArray;
44
45/// Implementation dependencies used by generated contracts.
46#[doc(hidden)]
47pub mod __private {
48    pub use serde;
49    #[cfg(all(feature = "typescript", not(target_family = "wasm")))]
50    pub use serde_json;
51}
52
53/// Typed codec/admission failure, distinct from transport and application errors.
54#[derive(Clone, Copy, Debug, Eq, PartialEq)]
55pub enum Error {
56    /// Wire, collection, text or caller-output allowance exceeded.
57    Limit,
58    /// Allocation failed within an admitted allowance.
59    Allocation,
60    /// Invalid or truncated Postcard representation.
61    Malformed,
62    /// Bytes remain after the expected value.
63    Trailing,
64    /// Envelope marker, method identity or direction differs.
65    Identity,
66}
67impl core::fmt::Display for Error {
68    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
69        write!(f, "wire {self:?}")
70    }
71}
72impl core::error::Error for Error {}
73
74/// Consumer-selected allowances for Serde traversal.
75#[derive(Clone, Copy, Debug)]
76pub struct Limits {
77    /// Maximum encoding bytes; framed APIs include the 11-byte header.
78    pub bytes: usize,
79    /// Conservative traversal allowance in bytes, not measured heap usage.
80    /// Reserves the entire [`Self::bytes`] allowance, then charges eight times
81    /// visited value sizes and text/byte lengths. Excludes caller input.
82    /// Encoding and decoding can require different allowances; see the crate guide.
83    pub allocation: usize,
84    /// Maximum visited value nodes, including unit elements.
85    pub nodes: usize,
86    /// Maximum nesting depth.
87    pub depth: usize,
88}