use crate::PipexResult;
pub trait PipelineResultHandler<T, E>
where
E: std::fmt::Debug,
{
fn handle_pipeline_results(self) -> Vec<Result<T, E>>;
}
pub trait ExtractSuccessful<T> {
fn extract_successful(self) -> Vec<T>;
}
pub trait IntoResult<T, E> {
fn into_result(self) -> Result<T, E>;
}
pub trait CreateError<E> {
fn create_error(error_msg: E) -> Self;
}
impl<T> ExtractSuccessful<T> for Vec<T> {
fn extract_successful(self) -> Vec<T> {
self
}
}
impl<T, E> ExtractSuccessful<T> for Vec<Result<T, E>> {
fn extract_successful(self) -> Vec<T> {
self.into_iter().filter_map(|r| r.ok()).collect()
}
}
impl<T, E> IntoResult<T, E> for Result<T, E> {
fn into_result(self) -> Result<T, E> {
self
}
}
impl<T, E> CreateError<E> for Result<T, E> {
fn create_error(error_msg: E) -> Self {
Err(error_msg)
}
}
impl<T, E> IntoResult<T, E> for PipexResult<T, E> {
fn into_result(self) -> Result<T, E> {
self.result
}
}
impl<T, E> CreateError<E> for PipexResult<T, E> {
fn create_error(error_msg: E) -> Self {
PipexResult::new(Err(error_msg), "preserve_error")
}
}
impl<T, E> PipelineResultHandler<T, E> for Vec<PipexResult<T, E>>
where
T: 'static + Clone,
E: std::fmt::Debug + 'static + Clone,
{
fn handle_pipeline_results(self) -> Vec<Result<T, E>> {
if let Some(first) = self.first() {
let strategy_name = first.strategy_name;
let inner_results: Vec<Result<T, E>> = self
.into_iter()
.map(|pipex_result| pipex_result.result)
.collect();
#[cfg(test)]
{
crate::tests::apply_strategy(strategy_name, inner_results)
}
#[cfg(not(test))]
{
crate::apply_strategy(strategy_name, inner_results)
}
} else {
vec![]
}
}
}
impl<T, E> PipelineResultHandler<T, E> for Vec<Result<T, E>>
where
E: std::fmt::Debug,
{
fn handle_pipeline_results(self) -> Vec<Result<T, E>> {
self
}
}
#[doc(hidden)]
pub trait IntoPipelineItem {
type OutputValue;
type PipelineItem;
fn into_pipeline_item(self) -> Self::PipelineItem;
}
#[doc(hidden)]
impl<T: 'static, E: std::fmt::Debug> IntoPipelineItem for Result<T, E> {
type OutputValue = T;
type PipelineItem = Result<T, String>;
fn into_pipeline_item(self) -> Result<T, String> {
self.map_err(|e| format!("{:?}", e))
}
}
#[doc(hidden)]
impl<T: 'static, E: std::fmt::Debug> IntoPipelineItem for PipexResult<T, E> {
type OutputValue = T;
type PipelineItem = PipexResult<T, String>;
fn into_pipeline_item(self) -> PipexResult<T, String> {
PipexResult {
result: self.result.map_err(|e| format!("{:?}", e)),
strategy_name: self.strategy_name,
}
}
}
#[doc(hidden)]
pub trait IsPure {
}