use crate::{Command, Merge};
use std::error::Error;
use std::fmt;
use std::marker::PhantomData;
#[macro_export]
macro_rules! merge {
($cmd1:expr, $cmd2:expr) => (
$crate::Merged::new($cmd1, $cmd2)
);
($cmd1:expr, $cmd2:expr, $($tail:expr),+) => (
merge![$crate::Merged::new($cmd1, $cmd2), $($tail),*]
);
}
pub struct Merged<R, C1: Command<R> + 'static, C2: Command<R> + 'static> {
cmd1: C1,
cmd2: C2,
#[cfg(feature = "display")]
summary: Option<String>,
_marker: PhantomData<Box<dyn Command<R> + 'static>>,
}
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> Merged<R, C1, C2> {
#[inline]
pub fn new(cmd1: C1, cmd2: C2) -> Merged<R, C1, C2> {
Merged {
cmd1,
cmd2,
#[cfg(feature = "display")]
summary: None,
_marker: PhantomData,
}
}
#[inline]
#[cfg(feature = "display")]
pub fn set_summary(&mut self, summary: impl Into<String>) {
self.summary = Some(summary.into());
}
#[inline]
pub fn into_commands(self) -> (C1, C2) {
(self.cmd1, self.cmd2)
}
}
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> Command<R> for Merged<R, C1, C2> {
#[inline]
fn apply(&mut self, receiver: &mut R) -> Result<(), Box<dyn Error + Send + Sync>> {
self.cmd1.apply(receiver)?;
self.cmd2.apply(receiver)
}
#[inline]
fn undo(&mut self, receiver: &mut R) -> Result<(), Box<dyn Error + Send + Sync>> {
self.cmd2.undo(receiver)?;
self.cmd1.undo(receiver)
}
#[inline]
fn redo(&mut self, receiver: &mut R) -> Result<(), Box<dyn Error + Send + Sync>> {
self.cmd1.redo(receiver)?;
self.cmd2.redo(receiver)
}
#[inline]
fn merge(&self) -> Merge {
self.cmd1.merge()
}
}
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> fmt::Debug for Merged<R, C1, C2> {
#[inline]
#[cfg(not(feature = "display"))]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Merged")
.field("cmd1", &self.cmd1)
.field("cmd2", &self.cmd2)
.finish()
}
#[inline]
#[cfg(feature = "display")]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("Merged")
.field("cmd1", &self.cmd1)
.field("cmd2", &self.cmd2)
.field("summary", &self.summary)
.finish()
}
}
#[cfg(feature = "display")]
impl<R, C1: Command<R> + 'static, C2: Command<R> + 'static> fmt::Display for Merged<R, C1, C2> {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match &self.summary {
Some(summary) => f.write_str(summary),
None => write!(f, "{cmd1} + {cmd2}", cmd1 = self.cmd1, cmd2 = self.cmd2),
}
}
}