use crate::{rule::string::StringRule, Rule, Value};
use super::Message;
#[derive(Clone, Copy)]
pub struct Trim;
const NAME: &str = "trim";
impl Rule for Trim {
type Message = Message;
const NAME: &'static str = NAME;
fn call(&mut self, data: &mut crate::Value) -> bool {
if let Value::String(s) = data {
*s = s.trim().to_string()
}
true
}
fn message(&self) -> Self::Message {
Message::new(super::MessageKind::Trim)
}
}
impl StringRule for Trim {
type Message = Message;
const NAME: &'static str = NAME;
fn call(&mut self, data: &mut String) -> bool {
*data = data.trim().to_string();
true
}
fn message(&self) -> Self::Message {
Message::new(super::MessageKind::Trim)
}
}
#[test]
fn test_trim() {
let mut value = Value::String(" hello ".to_string());
let mut trim = Trim {};
let _ = Rule::call(&mut trim, &mut value);
assert!(matches!(value, Value::String(s) if s == "hello".to_string()));
}