use super::Transform;
use crate::{
actions::transforms::{
error::TransformErrorKind,
field::Field,
result::{OptionUnwrapTransformResultExt, TransformedEntry},
},
entry::{Entry, EntryId},
};
#[derive(Debug)]
pub struct Use {
pub field: Field,
pub as_field: Field,
}
impl Transform for Use {
type Err = TransformErrorKind;
async fn transform_entry(&mut self, ent: Entry) -> Result<Vec<TransformedEntry>, Self::Err> {
let val = match self.field {
Field::Title => ent.msg.title,
Field::Body => ent.msg.body,
Field::Link => ent.msg.link,
Field::Id => ent.id.map(|id| id.0.into_string()),
Field::ReplyTo => ent.reply_to.map(|id| id.0.into_string()),
Field::RawContents => ent.raw_contents,
};
let mut ent = TransformedEntry::default();
match self.as_field {
Field::Title => ent.msg.title = val.unwrap_or_empty(),
Field::Body => ent.msg.body = val.unwrap_or_empty(),
Field::Link => ent.msg.link = val.unwrap_or_empty(),
Field::Id => ent.id = val.and_then(EntryId::new).unwrap_or_empty(),
Field::ReplyTo => ent.reply_to = val.and_then(EntryId::new).unwrap_or_empty(),
Field::RawContents => ent.raw_contents = val.unwrap_or_empty(),
}
Ok(vec![ent])
}
}