fidius_guest/wire.rs
1// Copyright 2026 Colliery, Inc.
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//! Wire format serialization for Fidius plugin FFI boundary.
16//!
17//! Fidius uses bincode as the single wire format for all FFI data. Prior to
18//! 0.1.0 the format varied by build profile (JSON in debug, bincode in
19//! release) — that was removed because profile-mixed host/plugin load
20//! rejections caused repeated dev-loop friction with no real inspection
21//! benefit to offset them.
22
23use serde::de::DeserializeOwned;
24use serde::Serialize;
25
26/// Errors that can occur during wire serialization or deserialization.
27#[derive(Debug, thiserror::Error)]
28pub enum WireError {
29 /// Bincode serialization/deserialization error.
30 #[error("bincode wire error: {0}")]
31 Bincode(#[from] bincode::Error),
32}
33
34/// Serialize a value as bincode for transport across the FFI boundary.
35pub fn serialize<T: Serialize>(val: &T) -> Result<Vec<u8>, WireError> {
36 bincode::serialize(val).map_err(WireError::Bincode)
37}
38
39/// Deserialize a value from bincode bytes received across the FFI boundary.
40pub fn deserialize<T: DeserializeOwned>(bytes: &[u8]) -> Result<T, WireError> {
41 bincode::deserialize(bytes).map_err(WireError::Bincode)
42}
43
44/// The exact serialized size of `val` in bytes, without allocating. Matches
45/// [`serialize`]'s output length. Used to size/validate a buffer before
46/// [`serialize_into`] (e.g. the cdylib streaming arena path, FIDIUS-T-0138).
47pub fn serialized_size<T: Serialize>(val: &T) -> Result<u64, WireError> {
48 bincode::serialized_size(val).map_err(WireError::Bincode)
49}
50
51/// Serialize `val` directly into a caller-provided buffer — no intermediate
52/// `Vec` allocation. `buf` must be at least [`serialized_size`] bytes (bincode
53/// errors otherwise). Byte-identical to [`serialize`].
54pub fn serialize_into<T: Serialize>(buf: &mut [u8], val: &T) -> Result<(), WireError> {
55 bincode::serialize_into(buf, val).map_err(WireError::Bincode)
56}