-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Impl
Ed25519VerificationKey2020
and Multikey
.
- Loading branch information
1 parent
3cbe443
commit 551370e
Showing
17 changed files
with
918 additions
and
116 deletions.
There are no files selected for viewing
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
src/table.rs |
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,16 @@ | ||
[package] | ||
name = "ssi-multicodec" | ||
version = "0.1.0" | ||
edition = "2021" | ||
authors = ["Spruce Systems, Inc."] | ||
license = "Apache-2.0" | ||
description = "Implementation of the Multicodec specification for the ssi library" | ||
repository = "https://github.com/spruceid/ssi/" | ||
documentation = "https://docs.rs/ssi-multicodec/" | ||
|
||
[dependencies] | ||
unsigned-varint = "0.7.1" | ||
|
||
[build-dependencies] | ||
csv = "1.2.2" | ||
thiserror.workspace = true |
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,105 @@ | ||
use std::{fs, io, io::Write, path::Path}; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=src/table.csv"); | ||
if let Err(e) = generate_codecs("src/table.csv", "src/table.rs") { | ||
eprintln!("unable to generate codecs: {e}"); | ||
std::process::exit(1); | ||
} | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
enum Error { | ||
#[error(transparent)] | ||
IO(#[from] io::Error), | ||
|
||
#[error(transparent)] | ||
Csv(#[from] csv::Error), | ||
|
||
#[error("missing name field")] | ||
MissingNameField, | ||
|
||
#[error("missing tag field")] | ||
MissingTagField, | ||
|
||
#[error("missing code field")] | ||
MissingCodeField, | ||
|
||
#[error("invalid code `{0}`")] | ||
InvalidCode(String), | ||
|
||
#[error("missing status field")] | ||
MissingStatusField, | ||
|
||
#[error("invalid status `{0}`")] | ||
InvalidStatus(String), | ||
|
||
#[error("missing description field")] | ||
MissingDescriptionField, | ||
} | ||
|
||
fn generate_codecs(input: impl AsRef<Path>, output: impl AsRef<Path>) -> Result<(), Error> { | ||
let mut reader = csv::Reader::from_path(input)?; | ||
let mut f = io::BufWriter::new(fs::File::create(output)?); | ||
|
||
for record in reader.records() { | ||
let record = record?; | ||
let mut fields = record.iter(); | ||
|
||
let name = fields.next().ok_or(Error::MissingNameField)?.trim(); | ||
let _tag = fields.next().ok_or(Error::MissingTagField)?.trim(); | ||
let code = parse_code(fields.next().ok_or(Error::MissingCodeField)?.trim())?; | ||
let status = parse_status(fields.next().ok_or(Error::MissingStatusField)?.trim())?; | ||
let description = fields.next().ok_or(Error::MissingDescriptionField)?.trim(); | ||
|
||
let const_name = all_caps(name); | ||
|
||
writeln!(f, "#[doc = \"{description}\"]")?; | ||
|
||
if matches!(status, Status::Deprecated) { | ||
writeln!(f, "#[deprecated]")? | ||
} | ||
|
||
writeln!(f, "pub const {const_name}: u64 = {code:#x}u64;")?; | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn parse_code(s: &str) -> Result<u64, Error> { | ||
u64::from_str_radix( | ||
s.strip_prefix("0x") | ||
.ok_or_else(|| Error::InvalidCode(s.to_owned()))?, | ||
16, | ||
) | ||
.map_err(|_| Error::InvalidCode(s.to_owned())) | ||
} | ||
|
||
fn parse_status(s: &str) -> Result<Status, Error> { | ||
match s { | ||
"draft" => Ok(Status::Draft), | ||
"permanent" => Ok(Status::Permanent), | ||
"deprecated" => Ok(Status::Deprecated), | ||
_ => Err(Error::InvalidStatus(s.to_owned())), | ||
} | ||
} | ||
|
||
enum Status { | ||
Draft, | ||
Permanent, | ||
Deprecated, | ||
} | ||
|
||
fn all_caps(s: &str) -> String { | ||
let mut result = String::new(); | ||
|
||
for c in s.chars() { | ||
if c == '-' { | ||
result.push('_'); | ||
} else { | ||
result.push(c.to_uppercase().next().unwrap()); | ||
} | ||
} | ||
|
||
result | ||
} |
Oops, something went wrong.