use crate::Result;
use serde::Serialize;
use stac::{FromJson, SelfHref, ToJson};
use std::{fs::File, io::Read, path::Path};
pub trait FromJsonPath: FromJson + SelfHref {
fn from_json_path(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
let mut buf = Vec::new();
let _ = File::open(path)?.read_to_end(&mut buf)?;
let mut value = Self::from_json_slice(&buf)?;
value.set_self_href(path.to_string_lossy());
Ok(value)
}
}
pub trait ToJsonPath: ToJson {
fn to_json_path(&self, path: impl AsRef<Path>, pretty: bool) -> Result<()> {
let file = File::create(path)?;
self.to_json_writer(file, pretty)?;
Ok(())
}
}
impl<T: FromJson + SelfHref> FromJsonPath for T {}
impl<T: Serialize> ToJsonPath for T {}
#[cfg(test)]
mod tests {
use super::FromJsonPath;
use stac::{Item, SelfHref};
#[test]
fn set_href() {
let item = Item::from_json_path("examples/simple-item.json").unwrap();
assert!(
item.self_href()
.unwrap()
.ends_with("examples/simple-item.json")
);
}
}