From 1a38924be99ba3f8564062687573ba847b0389fa Mon Sep 17 00:00:00 2001 From: tu6ge Date: Sun, 3 Sep 2023 16:34:36 +0800 Subject: [PATCH] add cmp mod --- src/cmp/mock_value.rs | 8 +++ src/cmp/mod.rs | 156 ++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 1 + 3 files changed, 165 insertions(+) create mode 100644 src/cmp/mock_value.rs create mode 100644 src/cmp/mod.rs diff --git a/src/cmp/mock_value.rs b/src/cmp/mock_value.rs new file mode 100644 index 0000000..02e0e04 --- /dev/null +++ b/src/cmp/mock_value.rs @@ -0,0 +1,8 @@ +#[derive(Debug, PartialEq, Eq, Clone, Ord, PartialOrd)] +pub enum Value { + Int8(i8), + Uint8(u8), + String(String), + Str(&'static str), + Unit, +} diff --git a/src/cmp/mod.rs b/src/cmp/mod.rs new file mode 100644 index 0000000..d27a1df --- /dev/null +++ b/src/cmp/mod.rs @@ -0,0 +1,156 @@ +mod mock_value; + +use std::{cmp::Ordering, ops::RangeBounds, string::Drain}; + +use mock_value::Value; + +impl PartialEq for Value { + fn eq(&self, other: &i8) -> bool { + if let Value::Int8(n) = self { + n.eq(other) + } else { + false + } + } +} + +impl PartialOrd for Value { + fn partial_cmp(&self, other: &i8) -> Option { + 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 { + 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(&mut self, range: R) -> Drain<'_> + where + R: RangeBounds, + { + if let Value::String(s) = self { + s.drain(range) + } else { + tyerr!() + } + } + pub fn replace_range(&mut self, range: R, replace_with: &str) + where + R: RangeBounds, + { + 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!() + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 7f1fb80..37330cc 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +pub mod cmp; mod de; pub mod register; pub mod rule;