Skip to main content

fastbase91_core/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2#![forbid(unsafe_code)]
3
4//! A safe encoder and decoder for the standard basE91 binary-to-text encoding.
5//!
6//! The crate provides allocating one-shot `encode` and `decode` APIs when the
7//! `alloc` feature is enabled, caller-provided-buffer APIs through
8//! [`encode_into`] and [`decode_into`], and streaming APIs through [`Encoder`]
9//! and [`Decoder`]. The default `std` feature includes `alloc`; with both
10//! features disabled, the crate supports `no_std` use without an allocator.
11//! Lenient or strict decoding is selected with [`DecodeOptions`].
12
13#[cfg(feature = "alloc")]
14extern crate alloc;
15
16mod decode;
17mod encode;
18mod tables;
19
20/// The version of this core crate.
21pub const VERSION: &str = env!("CARGO_PKG_VERSION");
22
23pub use decode::{max_decoded_len, DecodeError, DecodeOptions, Decoder};
24pub use encode::{max_encoded_len, EncodeError, Encoder, OutputTooSmall};
25
26/// Encodes into caller-provided storage.
27///
28/// The output is produced by the same state machine as streaming encoding.
29pub fn encode_into(input: &[u8], output: &mut [u8]) -> Result<usize, OutputTooSmall> {
30    Encoder::new().encode_into(input, output)
31}
32
33/// Decodes from caller-provided storage.
34///
35/// Decodes according to `options`, writes decoded bytes to `output`, and
36/// includes the final byte produced from any unpaired input symbol.
37pub fn decode_into(
38    input: &[u8],
39    output: &mut [u8],
40    options: DecodeOptions,
41) -> Result<usize, DecodeError> {
42    Decoder::new(options).decode_into(input, output)
43}
44
45#[cfg(feature = "alloc")]
46pub use encode::encode;
47
48#[cfg(feature = "alloc")]
49pub use decode::decode;
50
51#[cfg(test)]
52mod tests {
53    use super::*;
54
55    #[test]
56    fn slice_apis_compile() {
57        let mut output = [0_u8; 16];
58        assert!(encode_into(b"input", &mut output).is_ok());
59        assert_eq!(
60            decode_into(b"encoded", &mut output, DecodeOptions::new()),
61            Ok(5)
62        );
63        assert_eq!(&output[..5], b"\xfb\x8d\xca\x1d\xab");
64    }
65
66    #[test]
67    fn version_matches_package_metadata() {
68        assert_eq!(VERSION, env!("CARGO_PKG_VERSION"));
69        assert!(!VERSION.is_empty());
70    }
71}