Skip to content

Commit

Permalink
feat: fixitup
Browse files Browse the repository at this point in the history
No canoclaize
  • Loading branch information
gadomski committed Oct 31, 2024
1 parent d5c10cc commit 88a0d46
Show file tree
Hide file tree
Showing 9 changed files with 16 additions and 46 deletions.
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ object_store = "0.11.0"
openssl = { version = "0.10.68", features = ["vendored"] }
openssl-src = "=300.3.1" # joinked from https://github.com/iopsystems/rpc-perf/commit/705b290d2105af6f33150da04b217422c6d68701#diff-2e9d962a08321605940b5a657135052fbcef87b5e360662bb527c96d9a615542R41 to cross-compile Python
parquet = { version = "52.2", default-features = false }
path-slash = "0.2.1"
pgstac = { version = "0.2.1", path = "crates/pgstac" }
pyo3 = "0.22.3"
pythonize = "0.22.0"
Expand Down
1 change: 0 additions & 1 deletion crates/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@ jsonschema = { workspace = true, optional = true }
log.workspace = true
object_store = { workspace = true, optional = true }
parquet = { workspace = true, optional = true }
path-slash.workspace = true
reqwest = { workspace = true, features = ["json", "blocking"], optional = true }
serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
Expand Down
2 changes: 1 addition & 1 deletion crates/core/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ mod tests {
.unwrap()
);
let link = collection.link("item").unwrap();
assert_eq!(link.href, "examples/simple-item.json");
assert!(link.href.ends_with("simple-item.json"));
}
}

Expand Down
7 changes: 4 additions & 3 deletions crates/core/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,21 +50,22 @@ impl Format {
href: impl ToString,
) -> Result<T> {
let href = href.to_string();
let mut value: T = if let Some(url) = Url::parse(&href)
let (mut value, href): (T, String) = if let Some(url) = Url::parse(&href)
.ok()
.filter(|url| url.scheme().starts_with("http"))
{
#[cfg(feature = "reqwest")]
{
let bytes = reqwest::blocking::get(url)?.bytes()?;
self.from_bytes(bytes)?
(self.from_bytes(bytes)?, href)
}
#[cfg(not(feature = "reqwest"))]
{
return Err(Error::FeatureNotEnabled("reqwest"));
}
} else {
self.from_path(&href)?
let path = Path::new(&href).canonicalize()?;
(self.from_path(&path)?, path.to_string_lossy().into_owned())
};
value.set_href(href);
Ok(value)
Expand Down
5 changes: 2 additions & 3 deletions crates/core/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
use crate::{Asset, Assets, Bbox, Error, Fields, Link, Result, Version, STAC_VERSION};
use chrono::{DateTime, FixedOffset, Utc};
use geojson::{feature::Id, Feature, Geometry};
use path_slash::PathBufExt;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use stac_derive::{Href, Links, Migrate};
use std::{collections::HashMap, path::PathBuf};
use std::{collections::HashMap, path::Path};
use url::Url;

const TOP_LEVEL_ATTRIBUTES: [&str; 8] = [
Expand Down Expand Up @@ -286,7 +285,7 @@ impl Builder {
let mut item = Item::new(self.id);
for (key, mut asset) in self.assets {
if Url::parse(&asset.href).is_err() && self.canonicalize_paths {
asset.href = PathBuf::from_slash(&asset.href)
asset.href = Path::new(&asset.href)
.canonicalize()?
.to_string_lossy()
.into_owned();
Expand Down
1 change: 0 additions & 1 deletion crates/core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,6 @@ mod tests {
}

#[test]
#[ignore = "skipping while we debug paths"]
fn resolve() {
let mut node: Node = crate::read::<Catalog>("examples/catalog.json")
.unwrap()
Expand Down
1 change: 0 additions & 1 deletion crates/types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ rust-version.workspace = true

[dependencies]
mime.workspace = true
path-slash.workspace = true
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
thiserror.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/types/src/href.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub trait Href {
/// use stac::{Href, Item};
///
/// let item: Item = stac::read("examples/simple-item.json").unwrap();
/// assert_eq!(item.href(), Some("examples/simple-item.json"));
/// assert!(item.href().unwrap().ends_with("simple-item.json"));
/// ```
fn href(&self) -> Option<&str>;

Expand Down
42 changes: 8 additions & 34 deletions crates/types/src/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

use crate::{mime::APPLICATION_GEOJSON, Error, Href, Result};
use mime::APPLICATION_JSON;
use path_slash::PathBufExt;
use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use std::path::PathBuf;
use url::Url;

/// Child links.
Expand Down Expand Up @@ -735,25 +733,7 @@ fn make_absolute(href: impl ToString, base: Option<&str>) -> Result<String> {
}
}
} else {
println!(
"from slash: {}",
PathBuf::from_slash(&href).to_string_lossy()
);
println!(
"canonicalize: {}",
std::fs::canonicalize(PathBuf::from_slash(&href))
.unwrap()
.to_string_lossy()
);
println!(
"back: {}",
std::fs::canonicalize(PathBuf::from_slash(&href))
.map(|p| p.to_slash_lossy().into_owned())
.unwrap()
);
std::fs::canonicalize(PathBuf::from_slash(&href))
.map(|p| p.to_slash_lossy().into_owned())
.map_err(Error::from)
Ok(href)
}
}

Expand Down Expand Up @@ -824,7 +804,7 @@ fn normalize_path(path: &str) -> String {
let mut parts = if path.starts_with('/') {
Vec::new()
} else {
vec![""]
vec!["."]
};
for part in path.split('/') {
match part {
Expand Down Expand Up @@ -859,17 +839,6 @@ mod tests {
assert!(value.get("title").is_none());
}

#[test]
fn absolute() {
let mut link = Link::new("examples/simple-item.json", "rel");
link.make_absolute(None).unwrap();
assert!(
link.href.ends_with("should fail"),
"the absolute href failed, here's the output: {}",
link.href
);
}

mod links {
use stac::{Catalog, Href, Item, Link, Links};

Expand Down Expand Up @@ -899,7 +868,12 @@ mod tests {

#[test]
fn make_relative_links_absolute_path() {
let mut catalog: Catalog = stac::read("examples/catalog.json").unwrap();
let mut catalog: Catalog = stac::read(
std::fs::canonicalize("examples/catalog.json")
.unwrap()
.to_string_lossy(),
)
.unwrap();
catalog.make_links_absolute().unwrap();
for link in catalog.links() {
assert!(link.is_absolute());
Expand Down

0 comments on commit 88a0d46

Please sign in to comment.