use core::fmt;
use crate::path::Path;
use super::Update;
#[must_use = "Use in an update expression with `Update::from(remove)`"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Remove {
pub(crate) paths: Vec<Path>,
}
impl Remove {
pub fn and<T>(self, other: T) -> Update
where
T: Into<Update>,
{
Update::from(self).and(other)
}
}
impl fmt::Display for Remove {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("REMOVE ")?;
let mut first = true;
self.paths.iter().try_for_each(|name| {
if first {
first = false;
} else {
f.write_str(", ")?;
}
name.fmt(f)
})
}
}
impl<T> From<T> for Remove
where
T: Into<Path>,
{
fn from(path: T) -> Self {
Self {
paths: vec![path.into()],
}
}
}
impl<T> FromIterator<T> for Remove
where
T: Into<Path>,
{
fn from_iter<I>(paths: I) -> Self
where
I: IntoIterator<Item = T>,
{
Self {
paths: paths.into_iter().map(Into::into).collect(),
}
}
}
#[cfg(test)]
mod test {
use pretty_assertions::assert_eq;
use crate::Path;
#[test]
fn sub_attributes() {
assert_eq!(
"REMOVE foo.bar",
"foo.bar".parse::<Path>().unwrap().remove().to_string()
);
assert_eq!(
"REMOVE foo[3].bar",
"foo[3].bar".parse::<Path>().unwrap().remove().to_string()
);
assert_eq!(
"REMOVE foo[3][7]",
"foo[3][7]".parse::<Path>().unwrap().remove().to_string()
);
}
#[test]
fn and() {
let remove = "foo"
.parse::<Path>()
.unwrap()
.remove()
.and("bar".parse::<Path>().unwrap().remove());
assert_eq!("REMOVE foo, bar", remove.to_string());
}
}