use super::Msg;
impl<M> Msg<M> {
#[must_use]
pub fn route<N>(&self, pick: impl FnOnce(&M) -> Option<N>) -> Option<Msg<N>> {
match self {
Self::App(m) => pick(m).map(Msg::App),
other => Some(input(other)),
}
}
#[must_use]
pub fn map<N>(self, wrap: impl FnOnce(M) -> N) -> Msg<N> {
match self {
Self::App(m) => Msg::App(wrap(m)),
other => input(&other),
}
}
}
fn input<M, N>(msg: &Msg<M>) -> Msg<N> {
match msg {
Msg::KeyPress(k) => Msg::KeyPress(k.clone()),
Msg::KeyRelease(k) => Msg::KeyRelease(k.clone()),
Msg::MouseClick(m) => Msg::MouseClick(*m),
Msg::MouseRelease(m) => Msg::MouseRelease(*m),
Msg::MouseWheel(m) => Msg::MouseWheel(*m),
Msg::MouseMotion(m) => Msg::MouseMotion(*m),
Msg::Paste(text) => Msg::Paste(text.clone()),
other => window(other),
}
}
fn window<M, N>(msg: &Msg<M>) -> Msg<N> {
match msg {
Msg::PasteStart => Msg::PasteStart,
Msg::PasteEnd => Msg::PasteEnd,
Msg::Focus => Msg::Focus,
Msg::Blur => Msg::Blur,
Msg::WindowSize(size) => Msg::WindowSize(*size),
Msg::PixelSize(size) => Msg::PixelSize(*size),
Msg::CellSize(size) => Msg::CellSize(*size),
Msg::Resume => Msg::Resume,
other => replies(other),
}
}
fn replies<M, N>(msg: &Msg<M>) -> Msg<N> {
match msg {
Msg::CursorPosition { x, y } => Msg::CursorPosition { x: *x, y: *y },
Msg::KeyboardEnhancements(report) => Msg::KeyboardEnhancements(*report),
Msg::ModeReport { mode, value } => Msg::ModeReport {
mode: *mode,
value: *value,
},
Msg::TerminalVersion(v) => Msg::TerminalVersion(v.clone()),
Msg::ForegroundColor(c) => Msg::ForegroundColor(*c),
Msg::BackgroundColor(c) => Msg::BackgroundColor(*c),
Msg::CursorColor(c) => Msg::CursorColor(*c),
Msg::ColorProfile(p) => Msg::ColorProfile(*p),
other => attributes(other),
}
}
fn attributes<M, N>(msg: &Msg<M>) -> Msg<N> {
match msg {
Msg::Clipboard { content, selection } => Msg::Clipboard {
content: content.clone(),
selection: *selection,
},
Msg::Capability(c) => Msg::Capability(c.clone()),
Msg::PrimaryDeviceAttributes(a) => Msg::PrimaryDeviceAttributes(a.clone()),
Msg::SecondaryDeviceAttributes(a) => Msg::SecondaryDeviceAttributes(a.clone()),
Msg::TertiaryDeviceAttributes(a) => Msg::TertiaryDeviceAttributes(a.clone()),
Msg::ColorScheme(s) => Msg::ColorScheme(*s),
_ => Msg::Resume,
}
}
#[cfg(test)]
mod tests;