Skip to content

Commit

Permalink
Add release module
Browse files Browse the repository at this point in the history
The new `Release` struct encapsulates `Distribution` and adds a
`release` field that contains JWS-signed release metadata. It currently
only supports v2 metadata, but offers the same traits, constructors, and
accessors as `Distribution`, plus the `release()` accessor and
associated objects.

Add tests for all of these bits, using both the v2 corpus with a release
patch applied and custom JSON objects to test each individual struct and
its accessors.

Writing the tests for the `TryFrom<PathBuf>` when what was on hand was a
`Path` led to a bit of research and the conclusion that one does not
convert a file path into a struct, but loads it into a struct. So create
the `load` function, instead, and have it accept a `AsRef<Path>`
argument, which covers `Path`s, `PathBuf`s, and strings. Back-patch this
change to `Distribution`, as well, and take advantage of `.try_into()`
where possible.
  • Loading branch information
theory committed Sep 17, 2024
1 parent 426128a commit 2923488
Show file tree
Hide file tree
Showing 8 changed files with 1,430 additions and 21 deletions.
439 changes: 434 additions & 5 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ exclude = [ ".github", ".vscode", ".gitignore", ".ci", ".pre-*.yaml"]

[dependencies]
boon = "0.6"
chrono = { version = "0.4.38", features = ["serde"] }
email_address = "0.2.9"
json-patch = "2.0.0"
lexopt = "0.3.0"
relative-path = { version = "1.9", features = ["serde"] }
semver = { version = "1.0", features = ["std", "serde"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1.0"
serde_with = { version = "3.9.0", features = ["hex"] }
spdx = "0.10.6"
wax = "0.6.0"

Expand All @@ -32,3 +34,5 @@ serde_json = "1.0"

[dev-dependencies]
assert-json-diff = "2.0.2"
hex = "0.4.3"
tempfile = "3.12.0"
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ files. It supports both the [v1] and [v2] specs.
*/

pub mod meta;
pub mod release;
mod util; // private utilities
pub mod valid;

Expand Down
23 changes: 10 additions & 13 deletions src/meta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ This module provides interfaces to load, validate, and manipulate PGXN
[v2]: https://github.com/pgxn/rfcs/pull/3
*/
use std::{borrow::Borrow, collections::HashMap, error::Error, fs::File, path::PathBuf};
use std::{borrow::Borrow, collections::HashMap, error::Error, fs::File, path::Path};

use crate::util;
use relative_path::{RelativePath, RelativePathBuf};
Expand Down Expand Up @@ -836,6 +836,14 @@ impl Distribution {
}
}

/// Loads the release `META.json` data from `file` then converts into a
/// [`Distribution`]. Returns an error on file error or if the content of
/// `file` is not valid PGXN `META.json` data.
pub fn load<P: AsRef<Path>>(file: P) -> Result<Self, Box<dyn Error>> {
let meta: Value = serde_json::from_reader(File::open(file)?)?;
meta.try_into()
}

/// Borrows the Distribution name.
pub fn name(&self) -> &str {
self.name.as_str()
Expand Down Expand Up @@ -1073,24 +1081,13 @@ impl TryFrom<Distribution> for Value {
}
}

impl TryFrom<&PathBuf> for Distribution {
type Error = Box<dyn Error>;
/// Reads the `META.json` data from `file` then converts into a
/// [`Distribution`]. Returns an error on file error or if the content of
/// `file` is not valid PGXN `META.json` data.
fn try_from(file: &PathBuf) -> Result<Self, Self::Error> {
let meta: Value = serde_json::from_reader(File::open(file)?)?;
Distribution::try_from(meta)
}
}

impl TryFrom<&String> for Distribution {
type Error = Box<dyn Error>;
/// Converts `str` into JSON and then into a [`Distribution`]. Returns an
/// error if the content of `str` is not valid PGXN `META.json` data.
fn try_from(str: &String) -> Result<Self, Self::Error> {
let meta: Value = serde_json::from_str(str)?;
Distribution::try_from(meta)
meta.try_into()
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/meta/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ fn test_corpus() -> Result<(), Box<dyn Error>> {
let path = path?.into_path();
let contents: Value = serde_json::from_reader(File::open(&path)?)?;

// Test try_from path.
if let Err(e) = Distribution::try_from(&path) {
// Test load path.
if let Err(e) = Distribution::load(&path) {
panic!("{v_dir}/{:?} failed: {e}", path.file_name().unwrap());
}

Expand Down Expand Up @@ -1347,7 +1347,7 @@ fn test_distribution() -> Result<(), Box<dyn Error>> {
let name = path.as_path().to_str().unwrap();
let contents: Value = serde_json::from_reader(File::open(&path)?)?;

match Distribution::try_from(&path) {
match Distribution::load(&path) {
Err(e) => panic!("{name} failed: {e}"),
Ok(dist) => {
// Required fields.
Expand Down
1 change: 1 addition & 0 deletions src/meta/v1/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::*;
use std::path::PathBuf;

#[test]
fn test_v1_v2_common() {
Expand Down
Loading

0 comments on commit 2923488

Please sign in to comment.