use std::{
io::{Stdin, stdin},
iter::{Once, once},
path::PathBuf,
};
use crate::{
backend::{ReadSource, ReadSourceEntry},
error::{ErrorKind, RusticError, RusticResult},
};
#[derive(Debug, Clone)]
pub struct StdinSource {
path: PathBuf,
}
impl StdinSource {
pub const fn new(path: PathBuf) -> Self {
Self { path }
}
}
impl ReadSource for StdinSource {
type Open = Stdin;
type Iter = Once<RusticResult<ReadSourceEntry<Stdin>>>;
fn size(&self) -> RusticResult<Option<u64>> {
Ok(None)
}
fn entries(&self) -> Self::Iter {
let open = Some(stdin());
once(
ReadSourceEntry::from_path(self.path.clone(), open).map_err(|err| {
RusticError::with_source(
ErrorKind::Backend,
"Failed to create ReadSourceEntry from Stdin",
err,
)
}),
)
}
}