use std::fmt::{Debug, Display};
use crate::{rule::string::StringRule, Rule, Value};
use super::Message;
#[derive(Clone)]
pub struct Contains<T>(pub T);
impl<T: Debug> Debug for Contains<T> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("Contains").field(&self.0).finish()
}
}
crate::__impl_copy!(Contains);
crate::__impl_deref!(Contains);
const NAME: &str = "contains";
impl<T> Contains<T> {
pub const fn as_ref(&self) -> Contains<&T> {
let Contains(ref t) = self;
Contains(t)
}
pub fn as_mut(&mut self) -> Contains<&mut T> {
let Contains(ref mut t) = self;
Contains(t)
}
}
impl<T> Contains<T>
where
T: Display,
{
fn message_in(&self) -> Message {
Message::new(super::MessageKind::Contains(self.0.to_string()))
}
}
impl Rule for Contains<&str> {
type Message = Message;
const NAME: &'static str = NAME;
fn message(&self) -> Self::Message {
self.message_in()
}
fn call(&mut self, value: &mut Value) -> bool {
match value {
Value::String(s) => s.contains(self.0),
_ => false,
}
}
}
impl Rule for Contains<String> {
type Message = Message;
const NAME: &'static str = NAME;
fn message(&self) -> Self::Message {
self.message_in()
}
fn call(&mut self, value: &mut Value) -> bool {
match value {
Value::String(s) => s.contains(&self.0),
_ => false,
}
}
}
impl Rule for Contains<char> {
type Message = Message;
const NAME: &'static str = NAME;
fn message(&self) -> Self::Message {
self.message_in()
}
fn call(&mut self, value: &mut Value) -> bool {
match value {
Value::String(s) => s.contains(self.0),
_ => false,
}
}
}
impl StringRule for Contains<&'static str> {
type Message = Message;
const NAME: &'static str = NAME;
fn message(&self) -> Self::Message {
self.message_in()
}
fn call(&mut self, data: &mut String) -> bool {
data.contains(self.0)
}
}
impl StringRule for Contains<String> {
type Message = Message;
const NAME: &'static str = NAME;
fn message(&self) -> Self::Message {
self.message_in()
}
fn call(&mut self, data: &mut String) -> bool {
data.contains(&self.0)
}
}
impl StringRule for Contains<char> {
type Message = Message;
const NAME: &'static str = NAME;
fn message(&self) -> Self::Message {
self.message_in()
}
fn call(&mut self, data: &mut String) -> bool {
data.contains(self.0)
}
}
impl<T> Contains<&T> {
pub const fn copied(self) -> Contains<T>
where
T: Copy,
{
Contains(*self.0)
}
pub fn cloned(self) -> Contains<T>
where
T: Clone,
{
Contains(self.0.clone())
}
}
impl<T> Contains<&mut T> {
pub fn copied(self) -> Contains<T>
where
T: Copy,
{
Contains(*self.0)
}
pub fn cloned(self) -> Contains<T>
where
T: Clone,
{
Contains(self.0.clone())
}
}
impl<T: PartialEq> PartialEq for Contains<T> {
#[inline]
fn eq(&self, other: &Self) -> bool {
self.0 == other.0
}
}