Skip to main content

Crate sim_codec_binary_base64

Crate sim_codec_binary_base64 

Source
Expand description

Base64 text framing over the SIM binary codec.

This crate is a thin text wrapper around sim-codec-binary: it produces and consumes the same binary Expr frames, but carries them as a base64-encoded ASCII string so the codec can be used on text-only transports. Encoding delegates to the binary codec and base64-encodes the resulting frame; decoding base64-decodes the text and hands the bytes back to the binary reader.

The public surface is the BinaryBase64Codec runtime object, registered via BinaryBase64CodecLib.

§Examples

Register the codec and round-trip an Expr through base64 text: encoding produces an ASCII string, and decoding that string recovers the value.

use std::sync::Arc;
use sim_codec::{Input, decode_with_codec, encode_with_codec};
use sim_codec_binary_base64::BinaryBase64CodecLib;
use sim_kernel::{Cx, DefaultFactory, EagerPolicy, Expr, ReadPolicy, Symbol};

let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
sim_test_support::register_core_classes(&mut cx);

let lib = BinaryBase64CodecLib::new(cx.registry_mut().fresh_codec_id());
cx.load_lib(&lib)?;
let codec = Symbol::qualified("codec", "binary-base64");

let expr = Expr::String("hello".to_owned());
let text = encode_with_codec(&mut cx, &codec, &expr, Default::default())?
    .into_text()?;
assert!(text.is_ascii());

let back = decode_with_codec(&mut cx, &codec, Input::Text(text), ReadPolicy::default())?;
assert_eq!(back, expr);

The wrapped text is untrusted: input that is not valid base64 fails closed rather than being interpreted.

use std::sync::Arc;
use sim_codec::{Input, decode_with_codec};
use sim_codec_binary_base64::BinaryBase64CodecLib;
use sim_kernel::{Cx, DefaultFactory, EagerPolicy, ReadPolicy, Symbol};

let mut cx = Cx::new(Arc::new(EagerPolicy), Arc::new(DefaultFactory));
sim_test_support::register_core_classes(&mut cx);
let lib = BinaryBase64CodecLib::new(cx.registry_mut().fresh_codec_id());
cx.load_lib(&lib)?;
let codec = Symbol::qualified("codec", "binary-base64");

let result = decode_with_codec(
    &mut cx,
    &codec,
    Input::Text("not base64!".to_owned()),
    ReadPolicy::default(),
);
assert!(result.is_err());

Structs§

BinaryBase64Codec
Codec runtime object that carries sim-codec-binary frames as base64 text.
BinaryBase64CodecLib
Lib that registers the binary-base64 codec with the runtime.

Functions§

decode_base64
Decodes standard base64 text back to bytes under the default decode limits, failing closed on any invalid character, length, or padding.
decode_base64_with_limits
Decodes standard base64 text back to bytes, enforcing limits on the raw, whitespace-stripped, and decoded lengths BEFORE any bulk allocation.
encode_base64
Encodes bytes as standard base64 text (the +/ alphabet, padded).