irox_tools/codec/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// SPDX-License-Identifier: MIT
// Copyright 2025 IROX Contributors
//

//!
//! A [`Codec`] is a trait that provides `encode` and `decode` to convert to/from different byte
//! encoding formats
//!

mod vbyte;
pub use vbyte::*;
mod varint;
pub use varint::*;

crate::cfg_feature_std! {
    mod code_dictionary;
    pub use code_dictionary::*;
}

crate::cfg_feature_alloc! {
    extern crate alloc;
    use alloc::string::{String, ToString};
    use alloc::vec::Vec;
}
use irox_bits::{Bits, Error, MutBits};

/// Something that can encode or decode bytes to bytes
pub trait Codec {
    /// Encodes the input, writing to output.  Returns the total number of bytes written.
    fn encode<I: Bits, O: MutBits>(&self, input: I, output: &mut O) -> Result<usize, Error>;
    /// Decodes the input, writing to output.  Returns the total number of bytes written.
    fn decode<I: Bits, O: MutBits>(&self, input: I, output: &mut O) -> Result<usize, Error>;

    crate::cfg_feature_alloc! {
        /// Reads the entirety of the input in the raw format, and produces the encoded UTF-8 results
        /// in an owned string.
        fn encode_to_str<I: Bits>(&self, input: I) -> Result<String, Error> {
            let vec = self.encode_to_vec(input)?;
            Ok(String::from_utf8_lossy(vec.as_slice()).to_string())
        }
    }
    crate::cfg_feature_alloc! {
        /// Reads the entirety of the input in the raw format, and produces the encoded results in an
        /// owned Vec
        fn encode_to_vec<I: Bits>(&self, input: I) -> Result<Vec<u8>, Error> {
            let mut vec = Vec::new();
            self.encode(input, &mut vec)?;
            Ok(vec)
        }
    }
    crate::cfg_feature_alloc! {
        /// Reads the entirety of input in coded format, and produces the results in an owned UTF-8 encoded
        /// string, dropping any non-UTF-8 characters.
        fn decode_to_str_lossy<I: Bits>(&self, input: I) -> Result<String, Error> {
            let vec = self.decode_to_vec(input)?;
            Ok(String::from_utf8_lossy(&vec).to_string())
        }
    }
    crate::cfg_feature_alloc! {
        /// Reads the entirety of input in coded format, and produces the results in an owned Vec
        fn decode_to_vec<I: Bits>(&self, input: I) -> Result<Vec<u8>, Error> {
            let mut vec = Vec::new();
            self.decode(input, &mut vec)?;
            Ok(vec)
        }
    }
}