dec_sixbit/struct_api.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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
//! Provides the `DecSixbit` struct for encapsulated handling of SIXBIT-encoded data.
//!
//! The `DecSixbit` struct offers a more feature-rich and structured API for encoding and decoding operations,
//! leveraging the underlying encoding and decoding functions.
//!
//! ## Features
//! - Encapsulates SIXBIT-encoded data and its metadata.
//! - Implements common traits for ease of use.
//! - Provides both encoding and decoding functionalities.
use crate::{encode::encode, decode::decode, Error};
use std::fmt;
/// The `DecSixbit` struct stores the encoded bytes and provides methods
/// for accessing the encoded data and retrieving the original string.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
pub struct DecSixbit {
/// Original string length
pub(crate) len: usize,
/// Packed bytes where every 3 bytes contain 4 characters (6 bits each)
pub(crate) bytes: Vec<u8>,
}
impl DecSixbit {
/// The marker byte for trailing spaces in the last block is added when the length is a multiple of 4, and the last 6 bits are all zero.
const TRAILING_SPACE_MARKER: u8 = 0b11;
/// Creates a new DecSixbit instance by encoding the input string.
/// Only accepts ASCII characters in the range 32-95 (space through underscore).
/// Creates a new `DecSixbit` instance by encoding the input string.
///
/// # Parameters
/// - `str`: The input string to encode. Must contain only ASCII characters in the range 32-95.
///
/// # Errors
/// Returns an [`Error::InvalidCharacter`] if the input contains invalid characters.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("HELLO").unwrap();
/// ```
pub fn new(str: &str) -> Result<Self, Error> {
let (mut bytes, len) = encode(str)?;
// Check if TRAILING_SPACE_MARKER needs to be added
if len % 4 == 0 && len != 0 && (bytes.last().unwrap() & 0b111111) == 0 {
bytes.push(Self::TRAILING_SPACE_MARKER);
}
Ok(Self { bytes, len })
}
/// Returns a reference to the encoded SIXBIT bytes.
///
/// # Returns
/// A slice of bytes containing the SIXBIT-encoded data.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("HELLO").unwrap();
/// let encoded = sixbit.as_bytes();
/// ```
#[inline]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
/// Returns the length of the original input string.
///
/// # Returns
/// The number of characters in the original string before encoding.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("HELLO").unwrap();
/// assert_eq!(sixbit.len(), 5);
/// ```
#[inline]
pub fn len(&self) -> usize {
self.len
}
/// Checks if the encoded SIXBIT data is empty.
///
/// # Returns
/// `true` if the original input string was empty, otherwise `false`.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("").unwrap();
/// assert!(sixbit.is_empty());
/// ```
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn try_from_slice(bytes: &[u8]) -> Result<Self, Error> {
let num_full_blocks = bytes.len() / 3;
let num_remain_bytes = bytes.len() % 3;
let len = match num_remain_bytes {
0 => num_full_blocks * 4,
1 => {
if bytes.last().unwrap() == &Self::TRAILING_SPACE_MARKER {
num_full_blocks * 4
} else {
num_full_blocks * 4 + 1
}
},
2 => num_full_blocks * 4 + 2,
_ => unreachable!(),
};
Ok(Self {
len,
bytes: bytes.to_vec(),
})
}
pub fn from_slice(bytes: &[u8]) -> Self {
Self::try_from_slice(bytes).unwrap()
}
}
impl fmt::Display for DecSixbit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let decoded = decode(&self.bytes, self.len)
.expect("invalid SIXBIT data in DecSixbit");
write!(f, "{}", decoded)
}
}
impl std::str::FromStr for DecSixbit {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Self::new(s)
}
}
impl TryFrom<&str> for DecSixbit {
type Error = Error;
fn try_from(s: &str) -> Result<Self, Self::Error> {
Self::new(s)
}
}
impl TryFrom<&[u8]> for DecSixbit {
type Error = Error;
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
Self::try_from_slice(bytes)
}
}
impl TryFrom<Vec<u8>> for DecSixbit {
type Error = Error;
fn try_from(bytes: Vec<u8>) -> Result<Self, Self::Error> {
Self::try_from(bytes.as_slice())
}
}
impl TryFrom<&Vec<u8>> for DecSixbit {
type Error = Error;
fn try_from(bytes: &Vec<u8>) -> Result<Self, Self::Error> {
Self::try_from(bytes.as_slice())
}
}
impl AsRef<[u8]> for DecSixbit {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl serde::Serialize for DecSixbit {
fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
if serializer.is_human_readable() {
self.to_string().serialize(serializer)
} else {
(&self.len, &self.bytes).serialize(serializer)
}
}
}
mod deserialize {
use super::DecSixbit;
pub(super) struct DecSixbitVisitor;
#[allow(clippy::needless_lifetimes)]
impl<'de> serde::de::Visitor<'de> for DecSixbitVisitor {
type Value = DecSixbit;
fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
formatter.write_str("bytes or string")
}
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
DecSixbit::new(v).map_err(E::custom)
}
}
}
impl<'de> serde::Deserialize<'de> for DecSixbit {
fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<DecSixbit, D::Error> {
use serde::de::Error;
if deserializer.is_human_readable() {
deserializer
.deserialize_str(deserialize::DecSixbitVisitor)
.map_err(D::Error::custom)
} else {
let (len, bytes) = <(usize, Vec<u8>)>::deserialize(deserializer)?;
Ok(DecSixbit { len, bytes })
}
}
}