rustic_core/backend/
stdin.rs

1use std::{
2    io::{Stdin, stdin},
3    iter::{Once, once},
4    path::PathBuf,
5};
6
7use crate::{
8    backend::{ReadSource, ReadSourceEntry},
9    error::{ErrorKind, RusticError, RusticResult},
10};
11
12/// The `StdinSource` is a `ReadSource` for stdin.
13#[derive(Debug, Clone)]
14pub struct StdinSource {
15    /// The path of the stdin entry.
16    path: PathBuf,
17}
18
19impl StdinSource {
20    /// Creates a new `StdinSource`.
21    pub const fn new(path: PathBuf) -> Self {
22        Self { path }
23    }
24}
25
26impl ReadSource for StdinSource {
27    /// The open type.
28    type Open = Stdin;
29    /// The iterator type.
30    type Iter = Once<RusticResult<ReadSourceEntry<Stdin>>>;
31
32    /// Returns the size of the source.
33    fn size(&self) -> RusticResult<Option<u64>> {
34        Ok(None)
35    }
36
37    /// Returns an iterator over the source.
38    fn entries(&self) -> Self::Iter {
39        let open = Some(stdin());
40        once(
41            ReadSourceEntry::from_path(self.path.clone(), open).map_err(|err| {
42                RusticError::with_source(
43                    ErrorKind::Backend,
44                    "Failed to create ReadSourceEntry from Stdin",
45                    err,
46                )
47            }),
48        )
49    }
50}