Skip to main content

fastbase91_core/
lib.rs

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