Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add release module #28

Merged
merged 1 commit into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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