Implement NyashBox trait for RegexBox (ID, type, clone, equals, etc.)
This commit is contained in:
@ -3,17 +3,65 @@
|
|||||||
// 参考: 既存Boxの設計思想
|
// 参考: 既存Boxの設計思想
|
||||||
|
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
use crate::box_trait::{NyashBox, StringBox, BoolBox};
|
||||||
|
use std::any::Any;
|
||||||
|
use std::fmt::Debug;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub struct RegexBox {
|
pub struct RegexBox {
|
||||||
pub regex: Regex,
|
pub regex: Regex,
|
||||||
|
id: u64,
|
||||||
|
pattern: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RegexBox {
|
impl RegexBox {
|
||||||
pub fn new(pattern: &str) -> Result<Self, regex::Error> {
|
pub fn new(pattern: &str) -> Result<Self, regex::Error> {
|
||||||
|
static mut COUNTER: u64 = 0;
|
||||||
let regex = Regex::new(pattern)?;
|
let regex = Regex::new(pattern)?;
|
||||||
Ok(RegexBox { regex })
|
let id = unsafe {
|
||||||
|
COUNTER += 1;
|
||||||
|
COUNTER
|
||||||
|
};
|
||||||
|
Ok(RegexBox {
|
||||||
|
regex,
|
||||||
|
id,
|
||||||
|
pattern: pattern.to_string(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
pub fn is_match(&self, text: &str) -> bool {
|
pub fn is_match(&self, text: &str) -> bool {
|
||||||
self.regex.is_match(text)
|
self.regex.is_match(text)
|
||||||
}
|
}
|
||||||
|
pub fn pattern(&self) -> &str {
|
||||||
|
&self.pattern
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl NyashBox for RegexBox {
|
||||||
|
fn clone_box(&self) -> Box<dyn NyashBox> {
|
||||||
|
Box::new(self.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn to_string_box(&self) -> StringBox {
|
||||||
|
StringBox::new(format!("RegexBox({})", self.pattern))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn as_any(&self) -> &dyn Any {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
fn type_name(&self) -> &'static str {
|
||||||
|
"RegexBox"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn box_id(&self) -> u64 {
|
||||||
|
self.id
|
||||||
|
}
|
||||||
|
|
||||||
|
fn equals(&self, other: &dyn NyashBox) -> BoolBox {
|
||||||
|
if let Some(other_regex) = other.as_any().downcast_ref::<RegexBox>() {
|
||||||
|
BoolBox::new(self.pattern == other_regex.pattern)
|
||||||
|
} else {
|
||||||
|
BoolBox::new(false)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user