Skip to content

Commit

Permalink
Change merge try_from to take &[Value]
Browse files Browse the repository at this point in the history
Instead of `&[&Value]`. The double-reference is unnecessary and required
contortions to use.
  • Loading branch information
theory committed Nov 19, 2024
1 parent 06f37e6 commit 75ae03e
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 37 deletions.
12 changes: 6 additions & 6 deletions src/dist/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -775,7 +775,7 @@ should never need to be modified; hence the read-only accessors to its
contents.
For cases where PGXN `META.json` data does need to be modified, use the
[`TryFrom<&[&Value]>`](#impl-TryFrom%3C%26%5B%26Value%5D%3E-for-Distribution) trait to
[`TryFrom<&[Value]>`](#impl-TryFrom%3C%26%5B%26Value%5D%3E-for-Distribution) trait to
merge merge one or more [RFC 7396] patches.
[RFC 7396]: https://www.rfc-editor.org/rfc/rfc7396.html
Expand Down Expand Up @@ -964,7 +964,7 @@ impl TryFrom<Value> for Distribution {
}
}

impl TryFrom<&[&Value]> for Distribution {
impl TryFrom<&[Value]> for Distribution {
type Error = Error;
/// Merge multiple PGXN `META.json` data from `meta` into a
/// [`Distribution`]. Returns an error if `meta` is invalid.
Expand Down Expand Up @@ -1000,25 +1000,25 @@ impl TryFrom<&[&Value]> for Distribution {
/// });
///
/// let patch = json!({"license": "MIT"});
/// let all_meta = [&meta_json, &patch];
/// let all_meta = [meta_json, patch];
///
/// let meta = Distribution::try_from(&all_meta[..]);
/// assert!(meta.is_ok());
/// assert_eq!("MIT", meta.unwrap().license());
/// ```
///
/// [RFC 7396]: https:///www.rfc-editor.org/rfc/rfc7396.html
fn try_from(meta: &[&Value]) -> Result<Self, Self::Error> {
fn try_from(meta: &[Value]) -> Result<Self, Self::Error> {
if meta.is_empty() {
return Err(Error::Param("meta contains no values"));
}

// Find the version of the first doc.
let version = util::get_version(meta[0]).ok_or(Error::UnknownSpec)?;
let version = util::get_version(&meta[0]).ok_or(Error::UnknownSpec)?;

// Convert the first doc to v2 if necessary.
let mut v2 = match version {
1 => v1::to_v2(meta[0])?,
1 => v1::to_v2(&meta[0])?,
2 => meta[0].clone(),
_ => unreachable!(),
};
Expand Down
21 changes: 8 additions & 13 deletions src/dist/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ fn test_try_merge_v1() -> Result<(), Error> {
json!({"/dependencies": {"postgres": { "version": "8.0.0" }}}),
),
] {
run_merge_case(name, &contents, patches.as_slice(), &expect)?;
run_merge_case(name, contents.clone(), patches.as_slice(), &expect)?;
}

Ok(())
Expand Down Expand Up @@ -202,21 +202,16 @@ fn test_try_merge_v2() -> Result<(), Error> {
}),
),
] {
run_merge_case(name, &contents, patches.as_slice(), &expect)?;
run_merge_case(name, contents.clone(), patches.as_slice(), &expect)?;
}

Ok(())
}

fn run_merge_case(
name: &str,
orig: &Value,
patches: &[Value],
expect: &Value,
) -> Result<(), Error> {
fn run_merge_case(name: &str, orig: Value, patches: &[Value], expect: &Value) -> Result<(), Error> {
let mut meta = vec![orig];
for p in patches {
meta.push(p);
meta.push(p.clone());
}
match Distribution::try_from(meta.as_slice()) {
Err(e) => panic!("patching {name} failed: {e}"),
Expand Down Expand Up @@ -252,17 +247,17 @@ fn test_try_merge_err() -> Result<(), Error> {
("no meta", vec![], "meta contains no values"),
(
"no version",
vec![&empty],
vec![empty],
"cannot determine meta-spec version",
),
(
"bad version",
vec![&bad_version],
vec![bad_version],
"cannot determine meta-spec version",
),
(
"invalid",
vec![&invalid],
vec![invalid],
"jsonschema validation failed with https://pgxn.org/meta/v2/distribution.schema.json#\n- at '': missing properties 'version'",
),
] {
Expand Down Expand Up @@ -404,7 +399,7 @@ fn test_try_merge_partman() -> Result<(), Error> {
});

// Apply the patch.
let meta = [&original_meta, &patch];
let meta = [original_meta, patch];
match Distribution::try_from(&meta[..]) {
Err(e) => panic!("patching part man failed: {e}"),
Ok(dist) => {
Expand Down
10 changes: 5 additions & 5 deletions src/release/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ impl TryFrom<Value> for Release {
}
}

impl TryFrom<&[&Value]> for Release {
impl TryFrom<&[Value]> for Release {
type Error = Error;
/// Merge multiple PGXN release `META.json` data from `meta` into a
/// [`Release`]. Returns an error if `meta` is invalid.
Expand Down Expand Up @@ -409,25 +409,25 @@ impl TryFrom<&[&Value]> for Release {
/// });
///
/// let patch = json!({"license": "MIT"});
/// let all_meta = [&meta_json, &patch];
/// let all_meta = [meta_json, patch];
///
/// let meta = Release::try_from(&all_meta[..]);
/// assert!(meta.is_ok());
/// assert_eq!("MIT", meta.unwrap().license());
/// ```
///
/// [RFC 7396]: https:///www.rfc-editor.org/rfc/rfc7396.html
fn try_from(meta: &[&Value]) -> Result<Self, Self::Error> {
fn try_from(meta: &[Value]) -> Result<Self, Self::Error> {
if meta.is_empty() {
return Err(Error::Param("meta contains no values"));
}

// Find the version of the first doc.
let version = util::get_version(meta[0]).ok_or_else(|| Error::UnknownSpec)?;
let version = util::get_version(&meta[0]).ok_or_else(|| Error::UnknownSpec)?;

// Convert the first doc to v2 if necessary.
let mut v2 = match version {
1 => v1::to_v2(meta[0])?,
1 => v1::to_v2(&meta[0])?,
2 => meta[0].clone(),
_ => unreachable!(),
};
Expand Down
21 changes: 8 additions & 13 deletions src/release/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ fn test_try_merge_v2() -> Result<(), Error> {
}),
),
] {
run_merge_case(name, &contents, patches.as_slice(), &expect)?;
run_merge_case(name, contents.clone(), patches.as_slice(), &expect)?;
}

Ok(())
Expand Down Expand Up @@ -284,22 +284,17 @@ fn test_try_merge_v1() -> Result<(), Error> {
}),
),
] {
run_merge_case(name, &contents, patches.as_slice(), &expect)?;
run_merge_case(name, contents.clone(), patches.as_slice(), &expect)?;
}

Ok(())
}

fn run_merge_case(
name: &str,
orig: &Value,
patches: &[Value],
expect: &Value,
) -> Result<(), Error> {
fn run_merge_case(name: &str, orig: Value, patches: &[Value], expect: &Value) -> Result<(), Error> {
let patch = certs();
let mut meta = vec![orig, &patch];
let mut meta = vec![orig, patch];
for p in patches {
meta.push(p);
meta.push(p.clone());
}
match Release::try_from(meta.as_slice()) {
Err(e) => panic!("patching {name} failed: {e}"),
Expand Down Expand Up @@ -335,15 +330,15 @@ fn test_try_merge_err() -> Result<(), Error> {
("no meta", vec![], "meta contains no values"),
(
"no version",
vec![&empty],
vec![empty],
"cannot determine meta-spec version",
),
(
"bad version",
vec![&bad_version],
vec![bad_version],
"cannot determine meta-spec version",
),
("invalid", vec![&invalid], "missing properties 'version'"),
("invalid", vec![invalid], "missing properties 'version'"),
] {
match Release::try_from(arg.as_slice()) {
Ok(_) => panic!("patching {name} unexpectedly succeeded"),
Expand Down

0 comments on commit 75ae03e

Please sign in to comment.