Dialect

Struct Dialect 

Source
pub struct Dialect<const N: usize> {
    pub patterns: [VariablePattern; N],
}
Expand description

Represents a dialect with a fixed number of patterns.

§Parameters

  • N - Number of patterns

Fields§

§patterns: [VariablePattern; N]

Implementations§

Source§

impl<const N: usize> Dialect<N>

Source

pub const fn new(patterns: [VariablePattern; N]) -> Self

Source

pub const fn measure_encode_capacity(&self, payload: &[u8]) -> usize

Source

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. Use Dialect::measure_encode_capacity to determine the required size.
§Returns

A Result which is:

  • Ok(usize): The number of bytes written to the buffer.
  • Err(DialectError): If an error occurs during encoding.
§Errors
§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 ");
Source

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 the buffer.
  • Err(DialectError): If an error occurs during decoding.
§Errors
§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]);
Source

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 "
);
Source

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]);

Trait Implementations§

Source§

impl<const N: usize> Debug for Dialect<N>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for Dialect<N>

§

impl<const N: usize> RefUnwindSafe for Dialect<N>

§

impl<const N: usize> Send for Dialect<N>

§

impl<const N: usize> Sync for Dialect<N>

§

impl<const N: usize> Unpin for Dialect<N>

§

impl<const N: usize> UnwindSafe for Dialect<N>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.