Skip to main content

sim_codec_binary_base64/
lib.rs

1//! Base64 text framing over the SIM binary codec.
2//!
3//! This crate is a thin text wrapper around `sim-codec-binary`: it produces and
4//! consumes the same binary `Expr` frames, but carries them as a base64-encoded
5//! ASCII string so the codec can be used on text-only transports. Encoding
6//! delegates to the binary codec and base64-encodes the resulting frame;
7//! decoding base64-decodes the text and hands the bytes back to the binary
8//! reader.
9//!
10//! The public surface is the [`BinaryBase64Codec`] runtime object, registered
11//! via [`BinaryBase64CodecLib`].
12//!
13//! # Examples
14//!
15//! Register the codec and round-trip an [`Expr`] through base64 text: encoding
16//! produces an ASCII string, and decoding that string recovers the value.
17//!
18//! ```
19//! use std::sync::Arc;
20//! use sim_codec::{Input, decode_with_codec, encode_with_codec};
21//! use sim_codec_binary_base64::BinaryBase64CodecLib;
22//! use sim_kernel::{Cx, DefaultFactory, EagerPolicy, Expr, ReadPolicy, Symbol};
23//!
24//! let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
25//! sim_test_support::register_core_classes(&mut cx);
26//!
27//! let lib = BinaryBase64CodecLib::new(cx.registry_mut().fresh_codec_id());
28//! cx.load_lib(&lib)?;
29//! let codec = Symbol::qualified("codec", "binary-base64");
30//!
31//! let expr = Expr::String("hello".to_owned());
32//! let text = encode_with_codec(&mut cx, &codec, &expr, Default::default())?
33//!     .into_text()?;
34//! assert!(text.is_ascii());
35//!
36//! let back = decode_with_codec(&mut cx, &codec, Input::Text(text), ReadPolicy::default())?;
37//! assert_eq!(back, expr);
38//! # Ok::<(), sim_kernel::Error>(())
39//! ```
40//!
41//! The wrapped text is untrusted: input that is not valid base64 fails closed
42//! rather than being interpreted.
43//!
44//! ```
45//! use std::sync::Arc;
46//! use sim_codec::{Input, decode_with_codec};
47//! use sim_codec_binary_base64::BinaryBase64CodecLib;
48//! use sim_kernel::{Cx, DefaultFactory, EagerPolicy, ReadPolicy, Symbol};
49//!
50//! let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
51//! sim_test_support::register_core_classes(&mut cx);
52//! let lib = BinaryBase64CodecLib::new(cx.registry_mut().fresh_codec_id());
53//! cx.load_lib(&lib)?;
54//! let codec = Symbol::qualified("codec", "binary-base64");
55//!
56//! let result = decode_with_codec(
57//!     &mut cx,
58//!     &codec,
59//!     Input::Text("not base64!".to_owned()),
60//!     ReadPolicy::default(),
61//! );
62//! assert!(result.is_err());
63//! # Ok::<(), sim_kernel::Error>(())
64//! ```
65//!
66//! [`Expr`]: sim_kernel::Expr
67
68#![forbid(unsafe_code)]
69#![deny(missing_docs)]
70
71mod base64;
72mod codec;
73#[cfg(test)]
74mod tests;
75
76pub use base64::{decode_base64, decode_base64_with_limits, encode_base64};
77pub use codec::{BinaryBase64Codec, BinaryBase64CodecLib};