Skip to content

Commit

Permalink
add cmp mod
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed Sep 3, 2023
1 parent 2e230d7 commit 1a38924
Show file tree
Hide file tree
Showing 3 changed files with 165 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cmp/mock_value.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#[derive(Debug, PartialEq, Eq, Clone, Ord, PartialOrd)]
pub enum Value {
Int8(i8),
Uint8(u8),
String(String),
Str(&'static str),
Unit,
}
156 changes: 156 additions & 0 deletions src/cmp/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
mod mock_value;

use std::{cmp::Ordering, ops::RangeBounds, string::Drain};

use mock_value::Value;

impl PartialEq<i8> for Value {
fn eq(&self, other: &i8) -> bool {
if let Value::Int8(n) = self {
n.eq(other)
} else {
false
}
}
}

impl PartialOrd<i8> for Value {
fn partial_cmp(&self, other: &i8) -> Option<Ordering> {
if let Value::Int8(n) = self {
n.partial_cmp(other)
} else {
None
}
}
}

macro_rules! tyerr {
() => {
panic!("type mismatch")
};
}

impl Value {
pub fn push_str(&mut self, string: &str) {
if let Value::String(s) = self {
s.push_str(string)
} else {
tyerr!()
}
}
pub fn push(&mut self, ch: char) {
if let Value::String(s) = self {
s.push(ch)
} else {
tyerr!()
}
}
pub fn truncate(&mut self, new_len: usize) {
if let Value::String(s) = self {
s.truncate(new_len)
} else {
tyerr!()
}
}
pub fn pop(&mut self) -> Option<char> {
if let Value::String(s) = self {
s.pop()
} else {
tyerr!()
}
}
pub fn remove(&mut self, idx: usize) -> char {
if let Value::String(s) = self {
s.remove(idx)
} else {
tyerr!()
}
}
pub fn insert(&mut self, idx: usize, ch: char) {
if let Value::String(s) = self {
s.insert(idx, ch)
} else {
tyerr!()
}
}
pub fn insert_str(&mut self, idx: usize, string: &str) {
if let Value::String(s) = self {
s.insert_str(idx, string)
} else {
tyerr!()
}
}

/// TODO other type
pub fn len(&self) -> usize {
if let Value::String(s) = self {
s.len()
} else {
todo!()
}
}
pub fn is_empty(&self) -> bool {
if let Value::String(s) = self {
s.is_empty()
} else {
false
}
}
pub fn drain<R>(&mut self, range: R) -> Drain<'_>
where
R: RangeBounds<usize>,
{
if let Value::String(s) = self {
s.drain(range)
} else {
tyerr!()
}
}
pub fn replace_range<R>(&mut self, range: R, replace_with: &str)
where
R: RangeBounds<usize>,
{
if let Value::String(s) = self {
s.replace_range(range, replace_with)
} else {
tyerr!()
}
}

//----------------------------- u8 -----------------------------------
pub const fn is_ascii(&self) -> bool {
if let Value::Uint8(s) = self {
s.is_ascii()
} else {
false
}
}
pub const fn to_ascii_uppercase(&self) -> u8 {
if let Value::Uint8(s) = self {
s.to_ascii_uppercase()
} else {
tyerr!()
}
}
pub const fn to_ascii_lowercase(&self) -> u8 {
if let Value::Uint8(s) = self {
s.to_ascii_lowercase()
} else {
tyerr!()
}
}
pub fn make_ascii_uppercase(&mut self) {
if let Value::Uint8(s) = self {
s.make_ascii_uppercase()
} else {
tyerr!()
}
}
pub fn make_ascii_lowercase(&mut self) {
if let Value::Uint8(s) = self {
s.make_ascii_lowercase()
} else {
tyerr!()
}
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod cmp;
mod de;
pub mod register;
pub mod rule;
Expand Down

0 comments on commit 1a38924

Please sign in to comment.