use core::fmt::{self, Write};
use crate::{
path::Path,
value::{self, ValueOrRef},
};
use super::Update;
#[must_use = "Use in an update expression with `Update::from(delete)`"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Delete {
pub(crate) actions: Vec<DeleteAction>,
}
impl Delete {
pub fn new<P, S>(path: P, subset: S) -> Self
where
P: Into<Path>,
S: Into<value::Set>,
{
Self {
actions: vec![DeleteAction::new(path, subset)],
}
}
pub fn and<T>(self, other: T) -> Update
where
T: Into<Update>,
{
Update::from(self).and(other)
}
}
impl fmt::Display for Delete {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("DELETE ")?;
let mut first = true;
self.actions.iter().try_for_each(|action| {
if first {
first = false;
} else {
f.write_str(", ")?;
}
action.fmt(f)
})
}
}
impl From<DeleteAction> for Delete {
fn from(action: DeleteAction) -> Self {
Self {
actions: vec![action],
}
}
}
#[must_use = "Use in an update expression with `Update::from(delete)`"]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeleteAction {
pub(crate) path: Path,
pub(crate) subset: ValueOrRef,
}
impl DeleteAction {
pub fn new<P, S>(path: P, subset: S) -> Self
where
P: Into<Path>,
S: Into<value::Set>,
{
Self {
path: path.into(),
subset: subset.into().into(),
}
}
pub fn and<T>(self, other: T) -> Update
where
T: Into<Update>,
{
Update::from(self).and(other)
}
}
impl fmt::Display for DeleteAction {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.path.fmt(f)?;
f.write_char(' ')?;
self.subset.fmt(f)
}
}