|
1 | | -use crate::{config::Config, Error, Result}; |
2 | | -use object_store::{local::LocalFileSystem, path::Path, ObjectStore}; |
3 | | -use stac::{Format, Item, ItemCollection, Value}; |
4 | | -use std::io::BufReader; |
5 | | -use url::Url; |
| 1 | +use crate::{options::Options, Error, Result}; |
| 2 | +use stac::{io::Config, Format, Value}; |
6 | 3 |
|
7 | 4 | /// The input to a CLI run. |
8 | 5 | #[derive(Debug, Default)] |
9 | 6 | pub(crate) struct Input { |
10 | | - format: Format, |
11 | | - reader: Reader, |
12 | 7 | config: Config, |
13 | | -} |
14 | | - |
15 | | -#[derive(Debug, Default)] |
16 | | -enum Reader { |
17 | | - ObjectStore { |
18 | | - object_store: Box<dyn ObjectStore>, |
19 | | - path: Path, |
20 | | - }, |
21 | | - #[default] |
22 | | - Stdin, |
| 8 | + href: Option<String>, |
23 | 9 | } |
24 | 10 |
|
25 | 11 | impl Input { |
26 | 12 | /// Creates a new input. |
27 | 13 | pub(crate) fn new( |
28 | | - infile: impl Into<Option<String>>, |
| 14 | + href: impl Into<Option<String>>, |
29 | 15 | format: impl Into<Option<Format>>, |
30 | | - config: impl Into<Config>, |
31 | | - ) -> Result<Input> { |
32 | | - let infile = infile |
| 16 | + options: impl Into<Options>, |
| 17 | + ) -> Input { |
| 18 | + let href = href |
33 | 19 | .into() |
34 | | - .and_then(|infile| if infile == "-" { None } else { Some(infile) }); |
35 | | - let format = format |
36 | | - .into() |
37 | | - .or_else(|| infile.as_deref().and_then(Format::infer_from_href)) |
38 | | - .unwrap_or_default(); |
39 | | - let config = config.into(); |
40 | | - let reader = if let Some(infile) = infile { |
41 | | - let (object_store, path) = parse_href_opts(&infile, config.iter())?; |
42 | | - Reader::ObjectStore { object_store, path } |
43 | | - } else { |
44 | | - Reader::Stdin |
45 | | - }; |
46 | | - Ok(Input { |
47 | | - format, |
48 | | - reader, |
49 | | - config, |
50 | | - }) |
| 20 | + .and_then(|href| if href == "-" { None } else { Some(href) }); |
| 21 | + let config = Config::new().format(format).options(options.into()); |
| 22 | + Input { config, href } |
51 | 23 | } |
52 | 24 |
|
53 | 25 | /// Creates a new input with the given href. |
54 | | - pub(crate) fn with_href(&self, href: &str) -> Result<Input> { |
55 | | - let (object_store, path) = parse_href_opts(href, self.config.iter())?; |
56 | | - let reader = Reader::ObjectStore { object_store, path }; |
57 | | - Ok(Input { |
58 | | - format: self.format, |
59 | | - reader, |
| 26 | + pub(crate) fn with_href(&self, href: impl Into<Option<String>>) -> Input { |
| 27 | + Input { |
60 | 28 | config: self.config.clone(), |
61 | | - }) |
| 29 | + href: href.into(), |
| 30 | + } |
62 | 31 | } |
63 | 32 |
|
64 | 33 | /// Gets a STAC value from the input. |
65 | 34 | pub(crate) async fn get(&self) -> Result<Value> { |
66 | | - tracing::debug!("getting {}", self.format); |
67 | | - match &self.reader { |
68 | | - Reader::ObjectStore { object_store, path } => { |
69 | | - let bytes = object_store.get(path).await?.bytes().await?; |
70 | | - match self.format { |
71 | | - Format::Json => serde_json::from_slice(&bytes).map_err(Error::from), |
72 | | - Format::NdJson => bytes |
73 | | - .split(|c| *c == b'\n') |
74 | | - .map(|line| serde_json::from_slice::<Item>(line).map_err(Error::from)) |
75 | | - .collect::<Result<Vec<_>>>() |
76 | | - .map(ItemCollection::from) |
77 | | - .map(Value::from), |
78 | | - #[cfg(feature = "geoparquet")] |
79 | | - Format::Geoparquet => stac::geoparquet::from_reader(bytes) |
80 | | - .map(Value::from) |
81 | | - .map_err(Error::from), |
82 | | - } |
83 | | - } |
84 | | - Reader::Stdin => match self.format { |
85 | | - Format::Json => serde_json::from_reader(std::io::stdin()).map_err(Error::from), |
86 | | - Format::NdJson => stac::ndjson::from_buf_reader(BufReader::new(std::io::stdin())) |
87 | | - .map(Value::from) |
88 | | - .map_err(Error::from), |
89 | | - #[cfg(feature = "geoparquet")] |
90 | | - Format::Geoparquet => { |
91 | | - use std::io::Read; |
92 | | - |
93 | | - let mut buf = Vec::new(); |
94 | | - let _ = std::io::stdin().read_to_end(&mut buf)?; |
95 | | - stac::geoparquet::from_reader(bytes::Bytes::from(buf)) |
96 | | - .map(Value::from) |
97 | | - .map_err(Error::from) |
98 | | - } |
99 | | - }, |
| 35 | + if let Some(href) = self.href.as_ref() { |
| 36 | + self.config.get(href.clone()).await.map_err(Error::from) |
| 37 | + } else { |
| 38 | + self.config |
| 39 | + .from_reader(std::io::stdin()) |
| 40 | + .map_err(Error::from) |
100 | 41 | } |
101 | 42 | } |
102 | 43 | } |
103 | | - |
104 | | -pub(crate) fn parse_href_opts<I, K, V>( |
105 | | - href: &str, |
106 | | - options: I, |
107 | | -) -> Result<(Box<dyn ObjectStore>, Path)> |
108 | | -where |
109 | | - I: IntoIterator<Item = (K, V)>, |
110 | | - K: AsRef<str>, |
111 | | - V: Into<String>, |
112 | | -{ |
113 | | - if let Ok(url) = Url::parse(href) { |
114 | | - object_store::parse_url_opts(&url, options).map_err(Error::from) |
115 | | - } else { |
116 | | - let path = Path::from_filesystem_path(href)?; |
117 | | - let object_store = LocalFileSystem::new(); |
118 | | - Ok((Box::new(object_store), path)) |
119 | | - } |
120 | | -} |
0 commit comments