-
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.
- Loading branch information
Showing
7 changed files
with
107 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,59 @@ | ||
use super::Release; | ||
use serde_json::Value; | ||
use crate::meta::v1 as dist; | ||
use serde_json::{json, Value}; | ||
use std::error::Error; | ||
|
||
pub fn from_value(_: Value) -> Result<Release, Box<dyn Error>> { | ||
Err("TODO".into()) | ||
/// 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}; | ||
if let Some(Value::String(user)) = v1.get("user") { | ||
if let Some(Value::String(date)) = v1.get("date") { | ||
if let Some(Value::String(sha1)) = v1.get("sha1") { | ||
if let Some(Value::String(name)) = v1.get("name") { | ||
if let Some(Value::String(version)) = v1.get("version") { | ||
let uri = format!( | ||
"/dist/semver/{}/{}-{}.zip", | ||
version.as_str(), | ||
name.as_str(), | ||
version.as_str() | ||
); | ||
let mut rng = rand::thread_rng(); | ||
return Ok(json!({ | ||
"headers": [format!("eyJ{}", Alphanumeric.sample_string(&mut rng, 12))], | ||
"signatures": [Alphanumeric.sample_string(&mut rng, 32)], | ||
"payload": { | ||
"user": user, | ||
"date": date, | ||
"uri": uri, | ||
"digests": {"sha1": sha1}, | ||
} | ||
})); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
Err(Box::from("release metadata missing")) | ||
} |