Skip to main content

jetro_core/io/
source.rs

1use std::fmt;
2use std::io::BufRead;
3use std::path::{Path, PathBuf};
4
5/// Input source for NDJSON APIs that can operate on either files or callers'
6/// existing buffered readers.
7pub enum NdjsonSource {
8    File(PathBuf),
9    Reader(Box<dyn BufRead + Send>),
10}
11
12impl NdjsonSource {
13    pub fn file<P: Into<PathBuf>>(path: P) -> Self {
14        Self::File(path.into())
15    }
16
17    pub fn reader<R>(reader: R) -> Self
18    where
19        R: BufRead + Send + 'static,
20    {
21        Self::Reader(Box::new(reader))
22    }
23
24    pub fn as_file_path(&self) -> Option<&Path> {
25        match self {
26            Self::File(path) => Some(path.as_path()),
27            Self::Reader(_) => None,
28        }
29    }
30}
31
32impl fmt::Debug for NdjsonSource {
33    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
34        match self {
35            Self::File(path) => f.debug_tuple("File").field(path).finish(),
36            Self::Reader(_) => f.write_str("Reader(<bufread>)"),
37        }
38    }
39}