Skip to main content

consortium_codec/
lib.rs

1// Copyright 2026 Ethan Wu
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16#![cfg_attr(not(feature = "std"), no_std)]
17
18#[cfg(feature = "postcard")]
19pub(crate) mod postcard;
20
21#[cfg(feature = "prost")]
22pub(crate) mod prost;
23
24#[cfg(feature = "rkyv")]
25pub(crate) mod rkyv;
26
27#[cfg(feature = "postcard")]
28pub use postcard::PostcardCodec;
29
30#[cfg(feature = "prost")]
31pub use prost::ProstCodec;
32
33#[cfg(feature = "rkyv")]
34pub use rkyv::RkyvCodec;
35
36pub trait Codec: core::marker::Sized {
37    type Error: core::fmt::Debug;
38}
39
40pub trait CodecFor<T>: Codec {
41    type Decoded<'buf>: 'buf
42    where
43        T: 'buf;
44
45    fn encode(msg: &T, buf: &mut [u8]) -> core::result::Result<usize, Self::Error>;
46
47    fn decode<'buf>(buf: &'buf [u8]) -> core::result::Result<Self::Decoded<'buf>, Self::Error>
48    where
49        T: 'buf;
50}
51
52#[cfg(feature = "rkyv")]
53pub use consortium_codec_macros::TrustedArchive;
54
55#[cfg(feature = "rkyv")]
56pub use crate::rkyv::TrustedArchive;