-
Notifications
You must be signed in to change notification settings - Fork 61
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
feat: add ProblemDetails module for Verifiable Credentials Data Model v2.0 #585
Open
laysakura
wants to merge
1
commit into
spruceid:main
Choose a base branch
from
laysakura:feat/ProblemDetails
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+176
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
//! [Algorithms](https://www.w3.org/TR/vc-data-model-2.0/#algorithms) in Verifiable Credentials Data Model v2.0 | ||
|
||
mod problem_details; | ||
|
||
pub use problem_details::{PredefinedProblemType, ProblemDetails, ProblemType}; |
169 changes: 169 additions & 0 deletions
169
crates/claims/crates/vc/src/v2/algorithm/problem_details.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,169 @@ | ||
use std::fmt; | ||
|
||
use serde::{Serialize, Serializer}; | ||
|
||
/// [Problem Details](https://www.w3.org/TR/vc-data-model-2.0/#problem-details). | ||
#[derive(Debug, Serialize)] | ||
pub struct ProblemDetails { | ||
#[serde(rename = "type", serialize_with = "serialize_problem_type")] | ||
problem_type: Box<dyn ProblemType>, | ||
|
||
#[serde(skip_serializing_if = "Option::is_none")] | ||
code: Option<i32>, | ||
|
||
title: String, | ||
detail: String, | ||
} | ||
|
||
impl ProblemDetails { | ||
pub fn new<T: ProblemType>(problem_type: T, title: String, detail: String) -> Self { | ||
let code = problem_type.code(); | ||
Self { | ||
problem_type: Box::new(problem_type), | ||
code: Some(code), | ||
title, | ||
detail, | ||
} | ||
} | ||
|
||
/// `type` property. | ||
pub fn r#type(&self) -> &str { | ||
self.problem_type.url() | ||
} | ||
|
||
/// `code` property. | ||
pub fn code(&self) -> Option<i32> { | ||
self.code | ||
} | ||
|
||
/// `title` property. | ||
pub fn title(&self) -> &str { | ||
&self.title | ||
} | ||
|
||
/// `detail` property. | ||
pub fn detail(&self) -> &str { | ||
&self.detail | ||
} | ||
} | ||
|
||
fn serialize_problem_type<S>( | ||
problem_type: &Box<dyn ProblemType>, | ||
serializer: S, | ||
) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
serializer.serialize_str(&problem_type.to_string()) | ||
} | ||
|
||
/// Problem `type`. | ||
/// | ||
/// Implementations can define custom problem types. | ||
pub trait ProblemType: fmt::Display + fmt::Debug + Send + Sync + 'static { | ||
fn url(&self) -> &'static str; | ||
fn code(&self) -> i32; | ||
} | ||
|
||
/// Predefined `type`s in <https://www.w3.org/TR/vc-data-model-2.0/#problem-details>. | ||
#[derive(Clone, Debug, Serialize)] | ||
#[serde(rename_all = "SCREAMING_SNAKE_CASE")] | ||
pub enum PredefinedProblemType { | ||
ParsingError, | ||
CryptographicSecurityError, | ||
MalformedValueError, | ||
RangeError, | ||
} | ||
|
||
impl ProblemType for PredefinedProblemType { | ||
fn url(&self) -> &'static str { | ||
match self { | ||
PredefinedProblemType::ParsingError => { | ||
"https://www.w3.org/TR/vc-data-model#PARSING_ERROR" | ||
} | ||
PredefinedProblemType::CryptographicSecurityError => { | ||
"https://www.w3.org/TR/vc-data-model#CRYPTOGRAPHIC_SECURITY_ERROR" | ||
} | ||
PredefinedProblemType::MalformedValueError => { | ||
"https://www.w3.org/TR/vc-data-model#MALFORMED_VALUE_ERROR" | ||
} | ||
PredefinedProblemType::RangeError => "https://www.w3.org/TR/vc-data-model#RANGE_ERROR", | ||
} | ||
} | ||
|
||
fn code(&self) -> i32 { | ||
match self { | ||
PredefinedProblemType::ParsingError => -64, | ||
PredefinedProblemType::CryptographicSecurityError => -65, | ||
PredefinedProblemType::MalformedValueError => -66, | ||
PredefinedProblemType::RangeError => -67, | ||
} | ||
} | ||
} | ||
|
||
impl fmt::Display for PredefinedProblemType { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
write!(f, "{}", self.url()) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_serialize_problem_details_parsing_error() { | ||
let problem = ProblemDetails::new( | ||
PredefinedProblemType::ParsingError, | ||
"Parsing Error".to_string(), | ||
"Failed to parse the request body.".to_string(), | ||
); | ||
let json = serde_json::to_string(&problem).expect("Failed to serialize ProblemDetails"); | ||
assert_eq!( | ||
json, | ||
r#"{"type":"https://www.w3.org/TR/vc-data-model#PARSING_ERROR","code":-64,"title":"Parsing Error","detail":"Failed to parse the request body."}"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_serialize_problem_details_cryptographic_security_error() { | ||
let problem = ProblemDetails::new( | ||
PredefinedProblemType::CryptographicSecurityError, | ||
"Cryptographic Security Error".to_string(), | ||
"Failed to verify the cryptographic proof.".to_string(), | ||
); | ||
let json = serde_json::to_string(&problem).expect("Failed to serialize ProblemDetails"); | ||
assert_eq!( | ||
json, | ||
r#"{"type":"https://www.w3.org/TR/vc-data-model#CRYPTOGRAPHIC_SECURITY_ERROR","code":-65,"title":"Cryptographic Security Error","detail":"Failed to verify the cryptographic proof."}"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_serialize_problem_details_malformed_value_error() { | ||
let problem = ProblemDetails::new( | ||
PredefinedProblemType::MalformedValueError, | ||
"Malformed Value Error".to_string(), | ||
"The request body contains a malformed value.".to_string(), | ||
); | ||
let json = serde_json::to_string(&problem).expect("Failed to serialize ProblemDetails"); | ||
assert_eq!( | ||
json, | ||
r#"{"type":"https://www.w3.org/TR/vc-data-model#MALFORMED_VALUE_ERROR","code":-66,"title":"Malformed Value Error","detail":"The request body contains a malformed value."}"# | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_serialize_problem_details_range_error() { | ||
let problem = ProblemDetails::new( | ||
PredefinedProblemType::RangeError, | ||
"Range Error".to_string(), | ||
"The request body contains a value out of range.".to_string(), | ||
); | ||
let json = serde_json::to_string(&problem).expect("Failed to serialize ProblemDetails"); | ||
assert_eq!( | ||
json, | ||
r#"{"type":"https://www.w3.org/TR/vc-data-model#RANGE_ERROR","code":-67,"title":"Range Error","detail":"The request body contains a value out of range."}"# | ||
); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -5,9 +5,11 @@ use iref::Iri; | |
|
||
use crate::syntax::RequiredContext; | ||
|
||
mod algorithm; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think the pub mod problem;
pub use problem::ProblemDetails; |
||
mod data_model; | ||
pub mod syntax; | ||
|
||
pub use algorithm::*; | ||
pub use data_model::*; | ||
pub use syntax::{Context, JsonCredential, JsonCredentialTypes, SpecializedJsonCredential}; | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you can use
problem_type: impl ProblemType
directly instead of declaring a new type parameter.