-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dupe the pattern from the `meta` package by adding `v1` and `v2` sub-packages that know how to parse into the canonical form. Make `meta::v1` public to the crate so that `release::v1` can use it to migrate everything but the release metadata, then add `v1_to_v2_release` to handle that bit. The v2 `release` object takes the form of a JWS-JS data structure, but since we're not signing anything yet, and this is just to get things to load properly, for now just generate random values for the JWS header and signature. While at it, change the `uri` property to start with `dist` instead of `/dist`, because the latter implies relative to the root of a domain, and mirrors may not serve from a domain root.
- Loading branch information
Showing
13 changed files
with
269 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use super::Release; | ||
use crate::meta::v1 as dist; | ||
use serde_json::{json, Value}; | ||
use std::error::Error; | ||
|
||
/// to_v2 parses v1, which contains PGXN v1 release metadata, into a JSON | ||
/// object containing valid PGXN v2 release metadata. | ||
pub fn to_v2(v1: &Value) -> Result<Value, Box<dyn Error>> { | ||
let mut v2_val = dist::to_v2(v1)?; | ||
let v2 = v2_val | ||
.as_object_mut() | ||
.ok_or("Date returned from v1::to_v2 is not an object")?; | ||
|
||
// Convert release. | ||
v2.insert("release".to_string(), v1_to_v2_release(v1)?); | ||
|
||
Ok(v2_val) | ||
} | ||
|
||
/// from_value parses v1, which contains PGXN v1 metadata, into a | ||
/// [`Release`] object containing valid PGXN v2 metadata. | ||
pub fn from_value(v1: Value) -> Result<Release, Box<dyn Error>> { | ||
to_v2(&v1)?.try_into() | ||
} | ||
|
||
/// v1_to_v2_release clones release metadata from v1 to the v2 format. The | ||
/// included signature information will be random and un-verifiable, but | ||
/// adequate for v2 JSON Schema validation. | ||
fn v1_to_v2_release(v1: &Value) -> Result<Value, Box<dyn Error>> { | ||
use rand::distributions::{Alphanumeric, DistString}; | ||
let mut field = "user"; | ||
if let Some(Value::String(user)) = v1.get(field) { | ||
field = "date"; | ||
if let Some(Value::String(date)) = v1.get(field) { | ||
field = "sha1"; | ||
if let Some(Value::String(sha1)) = v1.get(field) { | ||
field = "name"; | ||
if let Some(Value::String(name)) = v1.get(field) { | ||
field = "version"; | ||
if let Some(Value::String(version)) = v1.get(field) { | ||
let uri = | ||
format!("dist/{1}/{0}/{1}-{0}.zip", version.as_str(), name.as_str()); | ||
// Generate random base64-ish values to mock headers | ||
// and signatures. | ||
let mut rng = rand::thread_rng(); | ||
return Ok(json!({ | ||
"headers": [format!("eyJ{}", Alphanumeric.sample_string(&mut rng, 13))], | ||
"signatures": [Alphanumeric.sample_string(&mut rng, 32)], | ||
"payload": { | ||
"user": user, | ||
"date": date, | ||
"uri": uri, | ||
"digests": {"sha1": sha1}, | ||
} | ||
})); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Err(Box::from(format!("missing release property {:?}", field))) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests; |
Oops, something went wrong.