use core::fmt::{self, Write};
use crate::{
path::Path,
value::{StringOrRef, ValueOrRef},
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BeginsWith {
pub(crate) path: Path,
pub(crate) substr: ValueOrRef,
}
impl BeginsWith {
pub fn new<P, S>(path: P, substr: S) -> Self
where
P: Into<Path>,
S: Into<StringOrRef>,
{
Self {
path: path.into(),
substr: substr.into().into(),
}
}
}
impl fmt::Display for BeginsWith {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("begins_with(")?;
self.path.fmt(f)?;
f.write_str(", ")?;
self.substr.fmt(f)?;
f.write_char(')')
}
}
#[cfg(test)]
mod test {
use std::error::Error;
use pretty_assertions::assert_eq;
use crate::{value::Ref, Path};
use super::BeginsWith;
#[test]
fn string() -> Result<(), Box<dyn Error>> {
let begins_with = BeginsWith::new("foo[3]".parse::<Path>()?, "foo");
assert_eq!(r#"begins_with(foo[3], "foo")"#, begins_with.to_string());
let begins_with = BeginsWith::new("foo[3]".parse::<Path>()?, String::from("foo"));
assert_eq!(r#"begins_with(foo[3], "foo")"#, begins_with.to_string());
#[allow(clippy::needless_borrows_for_generic_args)]
let begins_with = BeginsWith::new("foo[3]".parse::<Path>()?, &String::from("foo"));
assert_eq!(r#"begins_with(foo[3], "foo")"#, begins_with.to_string());
#[allow(clippy::needless_borrows_for_generic_args)]
let begins_with = BeginsWith::new("foo[3]".parse::<Path>()?, &"foo");
assert_eq!(r#"begins_with(foo[3], "foo")"#, begins_with.to_string());
Ok(())
}
#[test]
fn value_ref() {
let begins_with = BeginsWith::new("foo[3]".parse::<Path>().unwrap(), Ref::from("prefix"));
assert_eq!("begins_with(foo[3], :prefix)", begins_with.to_string());
}
}