use std::convert::Infallible;
use crate::{Context, Frame};
use super::ExtractorError;
pub trait Extractor<State> {
type Error: Into<ExtractorError>;
fn extract(frame: Frame, context: &Context<State>) -> Result<Self, Self::Error>
where
Self: Sized;
fn extract_from_frame_and_state(frame: Frame, context: &Context<State>) -> crate::Result<Self>
where
Self: Sized,
Self::Error: Into<ExtractorError>,
{
match Self::extract(frame, context) {
Ok(value) => Ok(value),
Err(error) => Err(error.into().into()),
}
}
}
impl<State> Extractor<State> for Frame {
type Error = Infallible;
fn extract(
frame: Frame,
_: &Context<State>,
) -> Result<Self, <crate::Frame as Extractor<State>>::Error> {
Ok(frame)
}
}
impl<State> Extractor<State> for String {
type Error = ExtractorError;
fn extract(frame: Frame, _: &Context<State>) -> Result<Self, Self::Error> {
Ok(frame.into())
}
}