pub mod feed;
pub mod html;
pub mod json;
pub mod print;
pub mod use_raw_contents;
use self::feed::Feed;
use self::html::Html;
use self::json::Json;
use self::use_raw_contents::UseRawContents;
use super::result::TransformedEntry;
use crate::error::transform::Error as TransformError;
use crate::source::http::{self, Http};
use crate::{entry::Entry, error::transform::Kind as TransformErrorKind};
use derive_more::From;
pub trait TransformEntry {
type Error: Into<TransformErrorKind>;
#[allow(clippy::missing_errors_doc)]
fn transform_entry(&self, entry: &Entry) -> Result<Vec<TransformedEntry>, Self::Error>;
}
#[allow(missing_docs, clippy::large_enum_variant)]
#[derive(From, Debug)]
pub enum Kind {
Http,
Html(Html),
Json(Json),
Feed(Feed),
UseRawContents(UseRawContents),
Print,
}
impl Kind {
pub async fn transform(
&self,
entry: Entry,
output: &mut Vec<Entry>,
) -> Result<(), TransformError> {
macro_rules! delegate {
($($t:tt),+ custom => { $($custom_t:pat => $custom_impl:expr),+ }) => {
match self {
$(Self::$t(x) => x.transform_entry(&entry).map_err(Into::into),)+
$($custom_t => $custom_impl,)+
}
};
}
let v = delegate!(
Html, Json, Feed, UseRawContents
custom => {
Self::Http => {
Http::transform(&entry, http::TransformFromField::MessageLink) .await
.map(|x| vec![x])
.map_err(Into::into)
},
Self::Print => {
print::print(&entry).await;
Ok(vec![TransformedEntry::default()])
}
}
)
.map_err(|kind| TransformError {
kind,
original_entry: entry.clone(),
})?;
output.extend(
v.into_iter()
.map(|new_entry| new_entry.into_entry(entry.clone())),
);
Ok(())
}
}