use crate::event::EventReceiver;
use std::io::Cursor;
use std::path::Path;
use std::sync::Mutex;
pub enum BodyReader<'x> {
Cursor(Cursor<&'x [u8]>),
EventReceiver(&'x Mutex<EventReceiver>),
File(std::fs::File),
}
impl<'x> BodyReader<'x> {
#[must_use]
pub fn bytes(bytes: &'x [u8]) -> Self {
Self::Cursor(Cursor::new(bytes))
}
pub fn file(path: impl AsRef<Path>) -> Result<Self, std::io::Error> {
Ok(Self::File(std::fs::File::open(path.as_ref())?))
}
}
impl<'x> std::io::Read for BodyReader<'x> {
fn read(&mut self, buf: &mut [u8]) -> Result<usize, std::io::Error> {
match self {
BodyReader::Cursor(cursor) => cursor.read(buf),
BodyReader::EventReceiver(mutex_event_receiver) => {
mutex_event_receiver.lock().unwrap().read(buf)
}
BodyReader::File(file) => file.read(buf),
}
}
}