use std::collections::HashMap;
use std::io::Cursor;
use crate::Transport;
use crate::transports::{BoxedHandler, Stream};
pub struct InMemory {
input: String,
}
impl InMemory {
pub fn new(input: &str) -> Self {
Self {
input: input.to_string(),
}
}
}
impl<C> Transport<C> for InMemory
where
C: Send + Sync,
{
type Handler = BoxedHandler<C>;
type Output = String;
type Error = std::io::Error;
fn serve(
self,
ctx: C,
handlers: HashMap<String, Self::Handler>,
) -> Result<Self::Output, Self::Error> {
let reader = Cursor::new(self.input);
let mut writer = Vec::new();
Stream::new(reader, &mut writer).serve(ctx, handlers)?;
let output = String::from_utf8(writer)
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e))?;
Ok(output)
}
}