ASCII lib
This commit is contained in:
parent
e1a5aa8562
commit
5667c81095
9
ascii/Cargo.toml
Normal file
9
ascii/Cargo.toml
Normal file
@ -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" }
|
||||
28
ascii/src/char.rs
Normal file
28
ascii/src/char.rs
Normal file
@ -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<Self> 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,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
74
ascii/src/converters.rs
Normal file
74
ascii/src/converters.rs
Normal file
@ -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);
|
||||
5
ascii/src/lib.rs
Normal file
5
ascii/src/lib.rs
Normal file
@ -0,0 +1,5 @@
|
||||
mod char;
|
||||
mod converters;
|
||||
|
||||
pub use crate::char::AsciiChar;
|
||||
pub use crate::converters::AsciiCharConvertable;
|
||||
Loading…
Reference in New Issue
Block a user