pub struct Dialect<const N: usize> {
pub patterns: [VariablePattern; N],
}Expand description
Fields§
§patterns: [VariablePattern; N]Implementations§
Source§impl<const N: usize> Dialect<N>
impl<const N: usize> Dialect<N>
pub const fn new(patterns: [VariablePattern; N]) -> Self
pub const fn measure_encode_capacity(&self, payload: &[u8]) -> usize
Sourcepub fn encode_slice(
&self,
payload: &[u8],
buffer: &mut [u8],
) -> Result<usize, DialectError>
pub fn encode_slice( &self, payload: &[u8], buffer: &mut [u8], ) -> Result<usize, DialectError>
Encodes a byte slice into a pre-allocated buffer using the dialect’s patterns.
This is the core, no_std-compatible encoding function. It processes the
input payload in chunks, distributing the bits from each chunk across a
series of words according to the dialect’s patterns. These words are then
written sequentially into the output buffer.
§Parameters
payload: The raw byte slice to be encoded.buffer: A mutable byte slice that will receive the encoded output. Its length must be sufficient to hold the entire encoded message. UseDialect::measure_encode_capacityto determine the required size.
§Returns
A Result which is:
Ok(usize): The number of bytes written to thebuffer.Err(DialectError): If an error occurs during encoding.
§Errors
DialectError::BufferExhausted: If the providedbufferis too small to hold the encoded data.
§Examples
use seqc::{Dialect, Pattern, VariablePattern};
const DIALECT: Dialect<1> = Dialect::new([
VariablePattern::Binary(Pattern::from([b'0', b'1'])),
]);
const DATA: &[u8] = &[0xB4];
let mut buffer = [0u8; DIALECT.measure_encode_capacity(DATA)];
let bytes_written = DIALECT.encode_slice(DATA, &mut buffer).unwrap();
// The output reflects the bit distribution for the single byte.
assert_eq!(bytes_written, 14);
assert_eq!(&buffer[..bytes_written], b"001111 011111 ");Sourcepub fn decode_slice(
&self,
payload: &[u8],
buffer: &mut [u8],
) -> Result<usize, DialectError>
pub fn decode_slice( &self, payload: &[u8], buffer: &mut [u8], ) -> Result<usize, DialectError>
Decodes a slice of space-separated words into a pre-allocated buffer.
This is the core, no_std-compatible decoding function. It interprets the
payload as a UTF-8 string of words separated by spaces. It processes the
words in groups, reconstructing the original byte chunks by collecting bits
from each word according to the dialect’s patterns. The resulting raw bytes
are written to the output buffer.
§Parameters
payload: The encoded byte slice. Must contain valid UTF-8 characters.buffer: A mutable byte slice that will receive the decoded raw bytes. Its length should be sufficient to hold the output.
§Returns
A Result which is:
Ok(usize): The number of decoded bytes written to thebuffer.Err(DialectError): If an error occurs during decoding.
§Errors
DialectError::NonUtf8Payload: If thepayloadis not a valid UTF-8 string.DialectError::UnexpectedRemainder: If thepayloadcontains a number of words that cannot be fully decoded back.
§Examples
use seqc::{Dialect, Pattern, VariablePattern};
let dialect: Dialect<2> = Dialect::new([
VariablePattern::Ternary(Pattern::from([b'a', b'b', b'c'])),
VariablePattern::Quaternary(Pattern::from([b'x', b'y', b'z', b'w'])),
]);
const ENCODED: &str = "aaabbbccc xxxyyyzzww aaabbbbcccc xyyyyzw abbbbcc xyyyyzww ";
let mut buffer = [0u8; ENCODED.len()];
let bytes_written = dialect.decode_slice(ENCODED.as_bytes(), &mut buffer).unwrap();
assert_eq!(bytes_written, 4);
assert_eq!(&buffer[..bytes_written], &[0xAA, 0xBB, 0xCC, 0xDD]);Sourcepub fn encode(&self, payload: impl AsRef<[u8]>) -> Vec<u8> ⓘ
pub fn encode(&self, payload: impl AsRef<[u8]>) -> Vec<u8> ⓘ
Encodes a byte slice into a new Vec<u8> using the dialect’s pattern.
This method provides a convenient, high-level interface for encoding. It first
calculates the exact required buffer size, allocates a Vec<u8>, and then
processes the input payload by distributing bits across chunks (e.g., even or
uneven distribution). This function is only available when the std feature
is enabled.
§Parameters
payload: A type that can be referenced as a byte slice containing the raw data to be encoded.
§Returns
A Vec<u8> containing the encoded, human-readable data.
§Examples
use seqc::{Dialect, Pattern, VariablePattern};
let dialect: Dialect<2> = Dialect::new([
VariablePattern::Ternary(Pattern::from([b'a', b'b', b'c'])),
VariablePattern::Quaternary(Pattern::from([b'x', b'y', b'z', b'w'])),
]);
let data = vec![0xAA, 0xBB, 0xCC, 0xDD];
let encoded = dialect.encode(&data);
assert_eq!(
&*String::from_utf8_lossy(&encoded),
"aaabbbccc xxxyyyzzww aaabbbbcccc xyyyyzw abbbbcc xyyyyzww "
);
Sourcepub fn decode(&self, payload: impl AsRef<[u8]>) -> Result<Vec<u8>, DialectError>
pub fn decode(&self, payload: impl AsRef<[u8]>) -> Result<Vec<u8>, DialectError>
Decodes a slice of space-separated words back into a new Vec<u8>.
This high-level method provides a convenient interface for decoding. It
allocates a Vec<u8> and processes the input payload, which is expected to
be a UTF-8 string of words separated by spaces. It reconstructs the original
raw bytes by interpreting the words according to the dialect’s patterns. This
function is only available when the std feature is enabled.
§Parameters
payload: A type that can be referenced as a byte slice containing the encoded, space-separated words.
§Returns
A Result which is:
Ok(Vec<u8>): A new vector containing the decoded raw data.Err(DialectError): If an error occurs during decoding.
§Errors
This function can fail if the input payload is not valid UTF-8 or if it contains a number of words that cannot be decoded back into bytes using dialect’s patterns.
§Examples
use seqc::{Dialect, Pattern, VariablePattern};
let dialect: Dialect<2> = Dialect::new([
VariablePattern::Ternary(Pattern::from([b'a', b'b', b'c'])),
VariablePattern::Quaternary(Pattern::from([b'x', b'y', b'z', b'w'])),
]);
// Corresponds to the encoded form of `[0xAA, 0xBB, 0xCC, 0xDD]`
let encoded_data = b"aaabbbccc xxxyyyzzww aaabbbbcccc xyyyyzw abbbbcc xyyyyzww ";
let decoded = dialect.decode(encoded_data).unwrap();
assert_eq!(decoded, vec![0xAA, 0xBB, 0xCC, 0xDD]);