Skip to content

Commit bde6f34

Browse files
committed
refactor(core): yet another io refactor
I think we got it this time, though.
1 parent a3284f5 commit bde6f34

File tree

31 files changed

+1726
-893
lines changed

31 files changed

+1726
-893
lines changed

.github/workflows/core.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ jobs:
4848
- name: Check w/ reqwest
4949
run: cargo check -F reqwest
5050
- name: Test
51-
run: cargo test -F geo -F geoparquet-compression -F reqwest -F object-store-full
51+
run: cargo test -F geo -F geoparquet-compression -F reqwest -F object-store-all
5252
test-core-with-gdal:
5353
runs-on: ubuntu-latest
5454
steps:

cli/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ serde = "1"
3131
serde_json = "1"
3232
stac = { version = "0.9.0", path = "../core", features = [
3333
"reqwest",
34-
"object-store-full",
34+
"object-store-all",
3535
] }
3636
stac-api = { version = "0.5.0", path = "../api", features = ["client"] }
3737
stac-duckdb = { version = "0.0.1", path = "../duckdb", optional = true }

cli/src/args/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ mod validate;
1212

1313
use crate::{input::Input, options::KeyValue, output::Output, Result, Value};
1414
use clap::Parser;
15-
use stac::io::Format;
15+
use stac::Format;
1616
use tokio::{sync::mpsc::Sender, task::JoinHandle};
1717
use tracing::metadata::Level;
1818

cli/src/args/search.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ async fn search_api(
132132
}
133133
}
134134

135-
#[cfg(all(feature = "duckdb", feature = "geoparquet"))]
135+
#[cfg(feature = "duckdb")]
136136
async fn search_geoparquet(
137137
href: String,
138138
mut search: Search,
@@ -185,20 +185,20 @@ impl Run for Args {
185185
} else {
186186
Some(self.collections)
187187
};
188-
#[cfg(all(feature = "duckdb", feature = "geoparquet"))]
188+
#[cfg(feature = "duckdb")]
189189
{
190190
if self.duckdb.unwrap_or_else(|| {
191191
matches!(
192-
stac::io::Format::infer_from_href(&self.href),
193-
Some(stac::io::Format::Geoparquet(_))
192+
stac::Format::infer_from_href(&self.href),
193+
Some(stac::Format::Geoparquet(_))
194194
)
195195
}) {
196196
search_geoparquet(self.href, search, stream, self.max_items).await
197197
} else {
198198
search_api(self.href, search, stream, self.max_items).await
199199
}
200200
}
201-
#[cfg(any(not(feature = "duckdb"), not(feature = "geoparquet")))]
201+
#[cfg(not(feature = "duckdb"))]
202202
{
203203
search_api(self.href, search, stream, self.max_items).await
204204
}

cli/src/input.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::io::Read;
22

33
use crate::{options::Options, Error, Result};
4-
use stac::{io::Format, Value};
4+
use stac::{Format, Value};
55

66
/// The input to a CLI run.
77
#[derive(Debug, Default)]
@@ -40,15 +40,18 @@ impl Input {
4040
/// Gets a STAC value from the input.
4141
pub(crate) async fn get(&self) -> Result<Value> {
4242
if let Some(href) = self.href.as_deref() {
43-
stac::io::get_format_opts(href, self.format, self.options.iter())
43+
self.format
44+
.or_else(|| Format::infer_from_href(href))
45+
.unwrap_or_default()
46+
.get_opts(href, self.options.iter())
4447
.await
4548
.map_err(Error::from)
4649
} else {
4750
let mut buf = Vec::new();
4851
let _ = std::io::stdin().read_to_end(&mut buf);
4952
self.format
5053
.unwrap_or_default()
51-
.from_bytes(buf.into())
54+
.from_bytes(buf)
5255
.map_err(Error::from)
5356
}
5457
}

cli/src/output.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{options::Options, value::Value, Error, Result};
44
use object_store::PutResult;
5-
use stac::io::{Format, IntoFormattedBytes};
5+
use stac::{Format, ToNdjson};
66
use std::{path::Path, pin::Pin};
77
use tokio::{
88
fs::File,
@@ -57,7 +57,7 @@ impl Output {
5757

5858
/// Streams a value to the output
5959
pub(crate) async fn stream(&mut self, value: Value) -> Result<()> {
60-
let bytes = value.into_formatted_bytes(Format::NdJson)?;
60+
let bytes = value.to_ndjson_vec()?;
6161
self.stream.write_all(&bytes).await?;
6262
self.stream.flush().await?;
6363
Ok(())
@@ -66,11 +66,12 @@ impl Output {
6666
/// Puts a value to the output.
6767
pub(crate) async fn put(&mut self, value: Value) -> Result<Option<PutResult>> {
6868
if let Some(href) = self.href.as_deref() {
69-
stac::io::put_format_opts(href, value, self.format, self.options.iter())
69+
self.format
70+
.put_opts(href, value, self.options.iter())
7071
.await
7172
.map_err(Error::from)
7273
} else {
73-
let bytes = value.into_formatted_bytes(self.format)?;
74+
let bytes = self.format.into_vec(value)?;
7475
self.stream.write_all(&bytes).await?;
7576
self.stream.flush().await?;
7677
Ok(None)

cli/src/value.rs

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use crate::{Error, Result};
22
use serde::Serialize;
3-
use stac::io::{Format, IntoFormattedBytes};
3+
use stac::{IntoGeoparquet, ToNdjson};
44

55
/// An output value, which can either be a [serde_json::Value] or a [stac::Value].
66
#[derive(Debug, Serialize)]
@@ -41,11 +41,31 @@ impl TryFrom<Value> for stac::Value {
4141
}
4242
}
4343

44-
impl IntoFormattedBytes for Value {
45-
fn into_formatted_bytes(self, format: Format) -> stac::Result<Vec<u8>> {
44+
impl ToNdjson for Value {
45+
fn to_ndjson_vec(&self) -> stac::Result<Vec<u8>> {
4646
match self {
47-
Self::Json(value) => value.into_formatted_bytes(format),
48-
Self::Stac(value) => value.into_formatted_bytes(format),
47+
Value::Json(json) => json.to_ndjson_vec(),
48+
Value::Stac(stac) => stac.to_ndjson_vec(),
49+
}
50+
}
51+
52+
fn to_ndjson_writer(&self, writer: impl std::io::Write) -> stac::Result<()> {
53+
match self {
54+
Value::Json(json) => json.to_ndjson_writer(writer),
55+
Value::Stac(stac) => stac.to_ndjson_writer(writer),
56+
}
57+
}
58+
}
59+
60+
impl IntoGeoparquet for Value {
61+
fn into_geoparquet_writer(
62+
self,
63+
writer: impl std::io::Write + Send,
64+
compression: Option<stac::geoparquet::Compression>,
65+
) -> stac::Result<()> {
66+
match self {
67+
Value::Json(json) => json.into_geoparquet_writer(writer, compression),
68+
Value::Stac(stac) => stac.into_geoparquet_writer(writer, compression),
4969
}
5070
}
5171
}

core/CHANGELOG.md

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
99
### Added
1010

1111
- Deref `ItemCollection` ([#363](https://github.com/stac-utils/stac-rs/pull/363))
12-
- `io::Format` ([#372](https://github.com/stac-utils/stac-rs/pull/371))
12+
- `Format` ([#371](https://github.com/stac-utils/stac-rs/pull/371), [#396](https://github.com/stac-utils/stac-rs/pull/396))
13+
- `Error::FeatureNotEnabled` and `Error::UnsupportedGeoparquetType` ([#396](https://github.com/stac-utils/stac-rs/pull/396))
1314
- Read unknown versions ([#378](https://github.com/stac-utils/stac-rs/pull/378))
14-
- `io::IntoFormattedBytes` ([#386](https://github.com/stac-utils/stac-rs/pull/386))
15+
- Conversion traits for the three formats ([#396](https://github.com/stac-utils/stac-rs/pull/396))
16+
- `object_store` ([#382](https://github.com/stac-utils/stac-rs/pull/382))
17+
- `stac::geoparquet::Compression`, even if geoparquet is not enabled ([#396](https://github.com/stac-utils/stac-rs/pull/396))
1518

1619
### Changed
1720

1821
- Update **geoarrow** to v0.3.0 ([#367](https://github.com/stac-utils/stac-rs/pull/367))
1922

23+
### Removed
24+
25+
- `Error::ReqwestNotEnabled` and `Error::GdalNotEnabled` ([#396](https://github.com/stac-utils/stac-rs/pull/382))
26+
2027
## [0.9.0] - 2024-09-05
2128

2229
### Added

core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ object-store-aws = ["object-store", "object_store/aws"]
3636
object-store-azure = ["object-store", "object_store/azure"]
3737
object-store-gcp = ["object-store", "object_store/gcp"]
3838
object-store-http = ["object-store", "object_store/http"]
39-
object-store-full = [
39+
object-store-all = [
4040
"object-store-aws",
4141
"object-store-azure",
4242
"object-store-gcp",
@@ -71,6 +71,8 @@ url = "2"
7171
assert-json-diff = "2"
7272
bytes = "1"
7373
rstest = "0.22"
74+
tempdir = "0.3"
75+
tokio = "1"
7476
tokio-test = "0.4"
7577

7678
[package.metadata.docs.rs]

core/data/items.ndjson

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
{"type":"Feature","stac_version":"1.0.0","stac_extensions":["https://stac-extensions.github.io/projection/v1.1.0/schema.json","https://stac-extensions.github.io/raster/v1.1.0/schema.json"],"id":"dataset","geometry":null,"properties":{"datetime":"2024-09-07T14:41:31.917359Z","proj:shape":[2667,2658]},"links":[],"assets":{"data":{"href":"/Users/gadomski/Code/stac-rs/core/assets/dataset.tif","roles":["data"],"raster:bands":[{"data_type":"uint16"}]}}}
2-
{"type":"Feature","stac_version":"1.0.0","stac_extensions":["https://stac-extensions.github.io/projection/v1.1.0/schema.json","https://stac-extensions.github.io/raster/v1.1.0/schema.json"],"id":"dataset_geo","geometry":{"type":"Polygon","coordinates":[[[-61.2876244,72.229798],[-52.3015987,72.229798],[-52.3015987,90.0],[-61.2876244,90.0],[-61.2876244,72.229798]]],"bbox":[-61.2876244,72.229798,-52.3015987,90.0]},"bbox":[-61.2876244,72.229798,-52.3015987,90.0],"properties":{"datetime":"2024-09-07T14:41:31.917358Z","proj:epsg":32621,"proj:bbox":[373185.0,8019284.949381611,639014.9492102272,8286015.0],"proj:centroid":{"lat":73.4675736,"lon":-56.8079473},"proj:shape":[2667,2658],"proj:transform":[100.01126757344893,0.0,373185.0,0.0,-100.01126757344893,8286015.0]},"links":[],"assets":{"data":{"href":"/Users/gadomski/Code/stac-rs/core/assets/dataset_geo.tif","roles":["data"],"raster:bands":[{"data_type":"uint16","spatial_resolution":100.01126757344893}]}}}
2+
{"type":"Feature","stac_version":"1.0.0","stac_extensions":["https://stac-extensions.github.io/projection/v1.1.0/schema.json","https://stac-extensions.github.io/raster/v1.1.0/schema.json"],"id":"dataset_geo","geometry":{"type":"Polygon","coordinates":[[[-61.2876244,72.229798],[-52.3015987,72.229798],[-52.3015987,90.0],[-61.2876244,90.0],[-61.2876244,72.229798]]],"bbox":[-61.2876244,72.229798,-52.3015987,90.0]},"bbox":[-61.2876244,72.229798,-52.3015987,90.0],"properties":{"datetime":"2024-09-07T14:41:31.917358Z","proj:epsg":32621,"proj:bbox":[373185.0,8019284.949381611,639014.9492102272,8286015.0],"proj:centroid":{"lat":73.4675736,"lon":-56.8079473},"proj:shape":[2667,2658],"proj:transform":[100.01126757344893,0.0,373185.0,0.0,-100.01126757344893,8286015.0]},"links":[],"assets":{"data":{"href":"/Users/gadomski/Code/stac-rs/core/assets/dataset_geo.tif","roles":["data"],"raster:bands":[{"data_type":"uint16","spatial_resolution":100.01126757344893}]}}}

0 commit comments

Comments
 (0)