use std::collections::HashMap;
use bc_envelope::prelude::*;
use crate::{
Pattern,
pattern::{Matcher, Path, compile_as_atomic, leaf::LeafPattern, vm::Instr},
};
#[derive(Debug, Clone)]
pub struct TextPattern(dcbor_pattern::TextPattern);
impl TextPattern {
pub fn any() -> Self { Self(dcbor_pattern::TextPattern::any()) }
pub fn value<T: Into<String>>(value: T) -> Self {
Self(dcbor_pattern::TextPattern::value(value))
}
pub fn regex(regex: regex::Regex) -> Self {
Self(dcbor_pattern::TextPattern::regex(regex))
}
pub fn from_dcbor_pattern(
dcbor_pattern: dcbor_pattern::TextPattern,
) -> Self {
Self(dcbor_pattern)
}
}
impl PartialEq for TextPattern {
fn eq(&self, other: &Self) -> bool { self.0 == other.0 }
}
impl Eq for TextPattern {}
impl std::hash::Hash for TextPattern {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) { self.0.hash(state); }
}
impl Matcher for TextPattern {
fn paths_with_captures(
&self,
haystack: &Envelope,
) -> (Vec<Path>, HashMap<String, Vec<Path>>) {
if let Some(cbor) = haystack.subject().as_leaf() {
let dcbor_paths = dcbor_pattern::Matcher::paths(&self.0, &cbor);
if !dcbor_paths.is_empty() {
let envelope_paths = vec![vec![haystack.clone()]];
let envelope_captures = HashMap::new(); (envelope_paths, envelope_captures)
} else {
(vec![], HashMap::new())
}
} else {
(vec![], HashMap::new())
}
}
fn compile(
&self,
code: &mut Vec<Instr>,
literals: &mut Vec<Pattern>,
captures: &mut Vec<String>,
) {
compile_as_atomic(
&Pattern::Leaf(LeafPattern::Text(self.clone())),
code,
literals,
captures,
);
}
}
impl std::fmt::Display for TextPattern {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_text_pattern_display() {
assert_eq!(TextPattern::any().to_string(), "text");
assert_eq!(TextPattern::value("Hello").to_string(), r#""Hello""#);
assert_eq!(
TextPattern::regex(regex::Regex::new(r"^\d+$").unwrap())
.to_string(),
r#"/^\d+$/"#
);
}
#[test]
fn test_text_pattern_dcbor_integration() {
let hello_envelope = Envelope::new("Hello");
let world_envelope = Envelope::new("World");
let number_envelope = Envelope::new(42);
let any_pattern = TextPattern::any();
assert!(any_pattern.matches(&hello_envelope));
assert!(any_pattern.matches(&world_envelope));
assert!(!any_pattern.matches(&number_envelope));
let hello_pattern = TextPattern::value("Hello");
assert!(hello_pattern.matches(&hello_envelope));
assert!(!hello_pattern.matches(&world_envelope));
assert!(!hello_pattern.matches(&number_envelope));
let word_regex = regex::Regex::new(r"^[A-Za-z]+$").unwrap();
let word_pattern = TextPattern::regex(word_regex);
assert!(word_pattern.matches(&hello_envelope));
assert!(word_pattern.matches(&world_envelope));
assert!(!word_pattern.matches(&number_envelope));
let paths = hello_pattern.paths(&hello_envelope);
assert_eq!(paths.len(), 1);
assert_eq!(paths[0], vec![hello_envelope.clone()]);
let no_paths = hello_pattern.paths(&world_envelope);
assert_eq!(no_paths.len(), 0);
}
#[test]
fn test_text_pattern_paths_with_captures() {
let hello_envelope = Envelope::new("Hello");
let pattern = TextPattern::value("Hello");
let (paths, captures) = pattern.paths_with_captures(&hello_envelope);
assert_eq!(paths.len(), 1);
assert_eq!(paths[0], vec![hello_envelope.clone()]);
assert_eq!(captures.len(), 0); }
#[test]
fn test_text_pattern_with_non_text_envelope() {
let envelope = Envelope::new_assertion("key", "value");
let pattern = TextPattern::any();
let paths = pattern.paths(&envelope);
assert_eq!(paths.len(), 0); }
}