Implement NyashBox trait for RegexBox (ID, type, clone, equals, etc.)

This commit is contained in:
moe-charm
2025-08-10 03:04:27 +00:00
parent 6f916855c8
commit 85a8c7f2d7

View File

@ -3,17 +3,65 @@
// 参考: 既存Boxの設計思想
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 regex: Regex,
id: u64,
pattern: String,
}
impl RegexBox {
pub fn new(pattern: &str) -> Result<Self, regex::Error> {
static mut COUNTER: u64 = 0;
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 {
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)
}
}
}