use super::{SharedSource, Tags};
use crate::string::Arg;
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
pub struct TargetedMsg<'a, T> {
pub tags: Tags<'a>,
pub source: Option<SharedSource<'a>>,
pub target: Arg<'a>,
pub value: T,
}
impl<'a, T: Default> TargetedMsg<'a, T> {
pub fn new_default(target: Arg<'a>) -> Self {
Self::new(target, T::default())
}
}
impl<'a, T> TargetedMsg<'a, T> {
pub const fn new(target: Arg<'a>, value: T) -> Self {
TargetedMsg { tags: Tags::new(), source: None, target, value }
}
pub fn map<U>(self, f: impl FnOnce(T) -> U) -> TargetedMsg<'a, U> {
let TargetedMsg { tags, source, target, value } = self;
let value = f(value);
TargetedMsg { tags, source, target, value }
}
pub fn map_result<U, E>(
self,
f: impl FnOnce(T) -> Result<U, E>,
) -> Result<TargetedMsg<'a, U>, E> {
let TargetedMsg { tags, source, target, value } = self;
let value = f(value)?;
Ok(TargetedMsg { tags, source, target, value })
}
pub fn map_iter<U, I: IntoIterator<Item = U>>(
self,
f: impl FnOnce(T) -> I,
) -> impl Iterator<Item = TargetedMsg<'a, U>> {
let TargetedMsg { tags, source, target, value } = self;
f(value).into_iter().map(move |value| TargetedMsg {
tags: tags.clone(),
source: source.clone(),
target: target.clone(),
value,
})
}
}