use super::TransformEntry;
use crate::{
action::transform::{
error::TransformErrorKind,
field::Field,
result::{TransformResult as TrRes, TransformedEntry},
},
entry::Entry,
error::InvalidUrlError,
utils::OptionExt,
};
use async_trait::async_trait;
use url::Url;
#[derive(Debug)]
pub struct Use {
pub field: Field,
pub as_field: Field,
}
#[async_trait]
impl TransformEntry for Use {
type Err = TransformErrorKind;
async fn transform_entry(&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.map(|s| s.to_string()),
Field::Id => ent.id.map(|id| id.0),
Field::ReplyTo => ent.reply_to.map(|id| id.0),
Field::RawContets => ent.raw_contents,
};
let mut ent = TransformedEntry::default();
match self.as_field {
Field::Title => ent.msg.title = TrRes::New(val),
Field::Body => ent.msg.body = TrRes::New(val),
Field::Link => {
ent.msg.link = TrRes::New(val.try_map(|s| {
Url::try_from(s.as_str()).map_err(|e| {
TransformErrorKind::FieldLinkTransformInvalidUrl(InvalidUrlError(e, s))
})
})?);
}
Field::Id => ent.id = TrRes::New(val.map(Into::into)),
Field::ReplyTo => ent.reply_to = TrRes::New(val.map(Into::into)),
Field::RawContets => ent.raw_contents = TrRes::New(val),
}
Ok(vec![ent])
}
}