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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
//! 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_unchecked, 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
}
/// Attempts to create a `DecSixbit` instance from a slice of encoded bytes.
///
/// # Parameters
/// - `bytes`: A slice of bytes containing SIXBIT-encoded data.
///
/// # Returns
/// - `Ok(Self)` if the slice is successfully parsed.
/// - `Err(Error)` if the slice has an invalid format or contains invalid data.
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(),
})
}
/// Creates a `DecSixbit` instance from a slice of encoded bytes.
///
/// # Parameters
/// - `bytes`: A slice of bytes containing SIXBIT-encoded data.
///
/// # Panics
/// - Panics if the slice has an invalid format or contains invalid data.
pub fn from_slice(bytes: &[u8]) -> Self {
Self::try_from_slice(bytes).unwrap()
}
/// Gets the character at the specified position.
///
/// # Parameters
/// - `index`: The position of the character to retrieve.
///
/// # Returns
/// An `Option<char>` which is `Some(char)` if the index is valid, or `None` otherwise.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("HELLO").unwrap();
/// assert_eq!(sixbit.get(1), Some('E'));
/// assert_eq!(sixbit.get(5), None);
/// ```
pub fn get(&self, index: usize) -> Option<char> {
self.to_string().chars().nth(index)
}
/// Checks if the string starts with the given prefix.
///
/// # Parameters
/// - `prefix`: The prefix string to check.
///
/// # Returns
/// `true` if the string starts with the given prefix, otherwise `false`.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("HELLO").unwrap();
/// assert!(sixbit.starts_with("HE"));
/// assert!(!sixbit.starts_with("EL"));
/// ```
pub fn starts_with<P: AsRef<str>>(&self, prefix: P) -> bool {
self.to_string().starts_with(prefix.as_ref())
}
/// Checks if the string ends with the given suffix.
///
/// # Parameters
/// - `suffix`: The suffix string to check.
///
/// # Returns
/// `true` if the string ends with the given suffix, otherwise `false`.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("HELLO").unwrap();
/// assert!(sixbit.ends_with("LO"));
/// assert!(!sixbit.ends_with("HE"));
/// ```
pub fn ends_with<P: AsRef<str>>(&self, suffix: P) -> bool {
self.to_string().ends_with(suffix.as_ref())
}
/// Checks if the string contains the given substring.
///
/// # Parameters
/// - `substring`: The substring to search for.
///
/// # Returns
/// `true` if the string contains the given substring, otherwise `false`.
///
/// # Examples
///
/// ```rust
/// use dec_sixbit::DecSixbit;
///
/// let sixbit = DecSixbit::new("HELLO").unwrap();
/// assert!(sixbit.contains("ELL"));
/// assert!(!sixbit.contains("XYZ"));
/// ```
pub fn contains<P: AsRef<str>>(&self, substring: P) -> bool {
self.to_string().contains(substring.as_ref())
}
}
impl fmt::Display for DecSixbit {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// Use decode_unchecked because the TRAILING_SPACE_MARKER byte might have been added at the end
let decoded = decode_unchecked(&self.bytes, self.len);
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 })
}
}
}
#[cfg(test)]
mod tests {
use super::DecSixbit;
use crate::Error;
use std::convert::TryFrom;
#[test]
fn test_new_valid_input() {
let input = "HELLO";
let sixbit = DecSixbit::new(input).unwrap();
assert_eq!(sixbit.len(), input.len());
assert_eq!(sixbit.to_string(), input);
}
#[test]
fn test_new_empty_string() {
let input = "";
let sixbit = DecSixbit::new(input).unwrap();
assert_eq!(sixbit.len(), 0);
assert!(sixbit.is_empty());
assert_eq!(sixbit.to_string(), input);
}
#[test]
fn test_new_invalid_character() {
let input = "HELLO😃";
let result = DecSixbit::new(input);
assert!(result.is_err());
match result {
Err(Error::InvalidCharacter { .. }) => (),
_ => panic!("Expected InvalidCharacter error"),
}
}
#[test]
fn test_as_bytes() {
let input = "TEST";
let sixbit = DecSixbit::new(input).unwrap();
let encoded = sixbit.as_bytes();
// The exact encoding depends on the `encode` implementation.
// Here we check that the bytes are not empty and have the expected length.
assert!(!encoded.is_empty());
assert_eq!(encoded.len(), 3); // 4 characters -> 3 bytes
}
#[test]
fn test_try_from_slice_valid() {
let input = "DATA";
let sixbit = DecSixbit::new(input).unwrap();
let bytes = sixbit.as_bytes();
let decoded = DecSixbit::try_from_slice(bytes).unwrap();
assert_eq!(sixbit, decoded);
}
#[test]
fn test_try_from_slice_with_trailing_marker() {
let input = "FOUR";
let sixbit = DecSixbit::new(input).unwrap();
let mut bytes = sixbit.as_bytes().to_vec();
// Manually add TRAILING_SPACE_MARKER
bytes.push(DecSixbit::TRAILING_SPACE_MARKER);
let decoded = DecSixbit::try_from_slice(&bytes).unwrap();
assert_eq!(decoded.len, sixbit.len);
assert_eq!(decoded.bytes, bytes);
}
#[test]
fn test_get_valid_index() {
let input = "WORLD";
let sixbit = DecSixbit::new(input).unwrap();
assert_eq!(sixbit.get(0), Some('W'));
assert_eq!(sixbit.get(4), Some('D'));
}
#[test]
fn test_get_invalid_index() {
let input = "WORLD";
let sixbit = DecSixbit::new(input).unwrap();
assert_eq!(sixbit.get(5), None);
}
#[test]
fn test_starts_with() {
let sixbit = DecSixbit::new("START").unwrap();
assert!(sixbit.starts_with("ST"));
assert!(!sixbit.starts_with("TA"));
}
#[test]
fn test_ends_with() {
let sixbit = DecSixbit::new("ENDING").unwrap();
assert!(sixbit.ends_with("ING"));
assert!(!sixbit.ends_with("END"));
}
#[test]
fn test_contains() {
let sixbit = DecSixbit::new("CONTAINS").unwrap();
assert!(sixbit.contains("TAI"));
assert!(!sixbit.contains("XYZ"));
}
#[test]
fn test_display_trait() {
let input = "DISPLAY";
let sixbit = DecSixbit::new(input).unwrap();
let displayed = format!("{}", sixbit);
assert_eq!(displayed, input);
}
#[test]
fn test_from_str() {
let input = "FROM_STR";
let sixbit: DecSixbit = input.parse().unwrap();
assert_eq!(sixbit.to_string(), input);
}
#[test]
fn test_try_from_str_valid() {
let input = "TRY_FROM";
let sixbit = DecSixbit::try_from(input).unwrap();
assert_eq!(sixbit.to_string(), input);
}
#[test]
fn test_try_from_str_invalid() {
let input = "INVALID😤";
let result = DecSixbit::try_from(input);
assert!(result.is_err());
}
#[test]
fn test_try_from_bytes_valid() {
let input = "BYTES";
let sixbit = DecSixbit::new(input).unwrap();
let bytes = sixbit.as_bytes();
let decoded = DecSixbit::try_from(bytes).unwrap();
assert_eq!(sixbit, decoded);
}
#[test]
fn test_try_from_vec_bytes() {
let input = "VEC_BYTES";
let sixbit = DecSixbit::new(input).unwrap();
let bytes = sixbit.as_bytes().to_vec();
let decoded = DecSixbit::try_from(bytes).unwrap();
assert_eq!(sixbit, decoded);
}
#[test]
fn test_serde_serialize_deserialize_human_readable() {
use serde_json;
let input = "SERIALIZE";
let sixbit = DecSixbit::new(input).unwrap();
let serialized = serde_json::to_string(&sixbit).unwrap();
assert_eq!(serialized, format!("\"{}\"", input));
let deserialized: DecSixbit = serde_json::from_str(&serialized).unwrap();
assert_eq!(sixbit, deserialized);
}
#[test]
fn test_serde_serialize_deserialize_binary() {
use bincode;
let input = "BINARY";
let sixbit = DecSixbit::new(input).unwrap();
let serialized = bincode::serialize(&sixbit).unwrap();
let deserialized: DecSixbit = bincode::deserialize(&serialized).unwrap();
assert_eq!(sixbit, deserialized);
}
#[test]
fn test_is_empty() {
let sixbit = DecSixbit::new("").unwrap();
assert!(sixbit.is_empty());
let sixbit = DecSixbit::new("NON_EMPTY").unwrap();
assert!(!sixbit.is_empty());
}
#[test]
fn test_len() {
let input = "LENGTH";
let sixbit = DecSixbit::new(input).unwrap();
assert_eq!(sixbit.len(), input.len());
}
#[test]
fn test_equality() {
let input1 = "EQUAL";
let input2 = "EQUAL";
let sixbit1 = DecSixbit::new(input1).unwrap();
let sixbit2 = DecSixbit::new(input2).unwrap();
assert_eq!(sixbit1, sixbit2);
}
#[test]
fn test_ordering() {
let sixbit_a = DecSixbit::new("AAA").unwrap();
let sixbit_b = DecSixbit::new("AAB").unwrap();
assert!(sixbit_a < sixbit_b);
}
#[test]
fn test_hash() {
use std::collections::HashSet;
let input1 = "HASH1";
let input2 = "HASH2";
let sixbit1 = DecSixbit::new(input1).unwrap();
let sixbit2 = DecSixbit::new(input2).unwrap();
let mut set = HashSet::new();
set.insert(sixbit1.clone());
set.insert(sixbit2.clone());
assert!(set.contains(&sixbit1));
assert!(set.contains(&sixbit2));
}
}