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