Skip to content

Commit

Permalink
use value_or_array serialization for domain type
Browse files Browse the repository at this point in the history
Signed-off-by: Ryan Tate <ryan.tate@spruceid.com>
  • Loading branch information
Ryanmtate committed Dec 16, 2024
1 parent b8ba3b7 commit e083a31
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/claims/crates/data-integrity/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub mod suite;
pub use decode::*;
use educe::Educe;
pub use options::ProofOptions;
pub use proof::value_or_array;
pub use proof::*;
use serde::Serialize;
use ssi_claims_core::{
Expand Down
7 changes: 6 additions & 1 deletion crates/claims/crates/data-integrity/core/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,12 @@ pub struct ProofOptions<M, T> {
/// Example domain values include: `domain.example`` (DNS domain),
/// `https://domain.example:8443` (Web origin), `mycorp-intranet` (bespoke
/// text string), and `b31d37d4-dd59-47d3-9dd8-c973da43b63a` (UUID).
#[serde(default, skip_serializing_if = "Vec::is_empty", rename = "domain")]
#[serde(
default,
with = "crate::value_or_array",
skip_serializing_if = "Vec::is_empty",
rename = "domain"
)]
pub domains: Vec<String>,

/// Used to mitigate replay attacks.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ pub struct ProofConfiguration<S: CryptographicSuite> {
/// Example domain values include: `domain.example`` (DNS domain),
/// `https://domain.example:8443` (Web origin), `mycorp-intranet` (bespoke
/// text string), and `b31d37d4-dd59-47d3-9dd8-c973da43b63a` (UUID).
#[serde(skip_serializing_if = "Vec::is_empty", rename = "domain")]
#[serde(
with = "crate::value_or_array",
skip_serializing_if = "Vec::is_empty",
rename = "domain"
)]
pub domains: Vec<String>,

/// Used to mitigate replay attacks.
Expand Down
28 changes: 27 additions & 1 deletion crates/claims/crates/data-integrity/core/src/proof/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,11 @@ pub struct Proof<S: CryptographicSuite> {
/// Example domain values include: `domain.example`` (DNS domain),
/// `https://domain.example:8443` (Web origin), `mycorp-intranet` (bespoke
/// text string), and `b31d37d4-dd59-47d3-9dd8-c973da43b63a` (UUID).
#[serde(skip_serializing_if = "Vec::is_empty", rename = "domain")]
#[serde(
with = "crate::value_or_array",
skip_serializing_if = "Vec::is_empty",
rename = "domain"
)]
pub domains: Vec<String>,

/// Used to mitigate replay attacks.
Expand Down Expand Up @@ -441,3 +445,25 @@ impl<'de, S: DeserializeCryptographicSuite<'de>> Deserialize<'de> for Proofs<S>
}
}
}

pub mod value_or_array {
use serde::{Deserialize, Serialize};
use ssi_core::OneOrMany;

pub fn serialize<T: Serialize, S>(value: &[T], serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match value.split_first() {
Some((first, [])) => first.serialize(serializer),
_ => value.serialize(serializer),
}
}

pub fn deserialize<'de, T: Deserialize<'de>, D>(deserializer: D) -> Result<Vec<T>, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(OneOrMany::deserialize(deserializer)?.into_vec())
}
}
2 changes: 1 addition & 1 deletion crates/claims/crates/vc/src/syntax/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ pub struct TypedObject {
pub extra_properties: BTreeMap<String, json_syntax::Value>,
}

pub(crate) mod value_or_array {
pub mod value_or_array {
use serde::{Deserialize, Serialize};
use ssi_core::OneOrMany;

Expand Down

0 comments on commit e083a31

Please sign in to comment.