From 5667c8109526cf0e5349ebd4a666bfc4f7b9d217 Mon Sep 17 00:00:00 2001 From: Andrew Golovashevich Date: Tue, 18 Nov 2025 13:05:11 +0300 Subject: [PATCH] ASCII lib --- ascii/Cargo.toml | 9 +++++ ascii/src/char.rs | 28 ++++++++++++++++ ascii/src/converters.rs | 74 +++++++++++++++++++++++++++++++++++++++++ ascii/src/lib.rs | 5 +++ 4 files changed, 116 insertions(+) create mode 100644 ascii/Cargo.toml create mode 100644 ascii/src/char.rs create mode 100644 ascii/src/converters.rs create mode 100644 ascii/src/lib.rs diff --git a/ascii/Cargo.toml b/ascii/Cargo.toml new file mode 100644 index 0000000..b8cccaf --- /dev/null +++ b/ascii/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "source-stream-0-ascii-0" +edition = "2024" + +[lib] + +[dependencies] +source-stream-0 = { path = ".." } +source-stream-0-wrapper-0 = { path = "../wrapper" } diff --git a/ascii/src/char.rs b/ascii/src/char.rs new file mode 100644 index 0000000..886abad --- /dev/null +++ b/ascii/src/char.rs @@ -0,0 +1,28 @@ +pub enum AsciiChar { + NOT_ASCII, + ASCII(u8), +} + +impl Clone for AsciiChar { + fn clone(&self) -> Self { + match self { + AsciiChar::NOT_ASCII => return AsciiChar::NOT_ASCII, + AsciiChar::ASCII(c) => return AsciiChar::ASCII(*c), + } + } +} + +impl Copy for AsciiChar {} + +impl PartialEq for AsciiChar { + fn eq(&self, other: &Self) -> bool { + match self { + AsciiChar::NOT_ASCII => return false, + AsciiChar::ASCII(c1) => match other { + AsciiChar::NOT_ASCII => return false, + AsciiChar::ASCII(c2) => return c1 == c2, + }, + } + } +} + diff --git a/ascii/src/converters.rs b/ascii/src/converters.rs new file mode 100644 index 0000000..bdff5b8 --- /dev/null +++ b/ascii/src/converters.rs @@ -0,0 +1,74 @@ +use crate::AsciiChar; + +pub trait AsciiCharConvertable { + fn asAsciiChar(&self) -> AsciiChar; +} + +impl AsciiCharConvertable for char { + fn asAsciiChar(&self) -> AsciiChar { + let raw = *self as u32; + if raw < 128 { + return AsciiChar::ASCII(raw as u8); + } else { + return AsciiChar::NOT_ASCII; + } + } +} + +impl AsciiCharConvertable for u8 { + fn asAsciiChar(&self) -> AsciiChar { + if *self < 128 { + return AsciiChar::ASCII(*self); + } else { + return AsciiChar::NOT_ASCII; + } + } +} + +macro_rules! convert_unsigned { + () => {}; + ($t0:ty $(, $tN:ty)* $(,)?) => { + impl AsciiCharConvertable for $t0 { + fn asAsciiChar(&self) -> AsciiChar { + if *self < 128 { + return AsciiChar::ASCII(*self as u8); + } else { + return AsciiChar::NOT_ASCII; + } + } + } + + convert_unsigned!($($tN ,)*); + } +} + +convert_unsigned!(u16, u32, u64); + +impl AsciiCharConvertable for i8 { + fn asAsciiChar(&self) -> AsciiChar { + if 0 <= *self { + return AsciiChar::ASCII(*self as u8); + } else { + return AsciiChar::NOT_ASCII; + } + } +} + +macro_rules! convert_signed { + () => {}; + ($t0:ty $(, $tN:ty)* $(,)?) => { + impl AsciiCharConvertable for $t0 { + fn asAsciiChar(&self) -> AsciiChar { + if 0 <= *self && *self < 128 { + return AsciiChar::ASCII(*self as u8); + } else { + return AsciiChar::NOT_ASCII; + } + } + } + + convert_unsigned!($($tN ,)*); + } +} + +convert_signed!(i16, i32, i64); diff --git a/ascii/src/lib.rs b/ascii/src/lib.rs new file mode 100644 index 0000000..075b769 --- /dev/null +++ b/ascii/src/lib.rs @@ -0,0 +1,5 @@ +mod char; +mod converters; + +pub use crate::char::AsciiChar; +pub use crate::converters::AsciiCharConvertable;