Skip to content

Commit

Permalink
Move OneOrMany into own file
Browse files Browse the repository at this point in the history
  • Loading branch information
clehner committed Sep 21, 2020
1 parent 15fa92b commit 5117509
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 62 deletions.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ pub mod der;
pub mod did;
pub mod error;
pub mod jwk;
pub(crate) mod one_or_many;
pub mod vc;

extern crate pest;
Expand Down
79 changes: 79 additions & 0 deletions src/one_or_many.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum OneOrMany<T> {
One(T),
Many(Vec<T>),
}

impl<T> OneOrMany<T> {
pub fn len(&self) -> usize {
match self {
Self::One(_) => 1,
Self::Many(values) => values.len(),
}
}

pub fn contains(&self, x: &T) -> bool
where
T: PartialEq<T>,
{
match self {
Self::One(value) => x == value,
Self::Many(values) => values.contains(x),
}
}

pub fn first(&self) -> Option<&T> {
match self {
Self::One(value) => Some(&value),
Self::Many(values) => {
if values.len() > 0 {
Some(&values[0])
} else {
None
}
}
}
}

pub fn to_single(&self) -> Option<&T> {
match self {
Self::One(value) => Some(&value),
Self::Many(values) => {
if values.len() == 1 {
Some(&values[0])
} else {
None
}
}
}
}
}

// consuming iterator
impl<T> IntoIterator for OneOrMany<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
match self {
Self::One(value) => vec![value].into_iter(),
Self::Many(values) => values.into_iter(),
}
}
}

// non-consuming iterator
impl<'a, T> IntoIterator for &'a OneOrMany<T> {
type Item = &'a T;
type IntoIter = std::vec::IntoIter<Self::Item>;

fn into_iter(self) -> Self::IntoIter {
match self {
OneOrMany::One(value) => vec![value].into_iter(),
OneOrMany::Many(values) => values.into_iter().collect::<Vec<Self::Item>>().into_iter(),
}
}
}
63 changes: 1 addition & 62 deletions src/vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::convert::TryFrom;

use crate::error::Error;
use crate::jwk::{JWTKeys, Params};
use crate::one_or_many::OneOrMany;

use chrono::prelude::*;
use jsonwebtoken::{Algorithm, DecodingKey, EncodingKey, Header, Validation};
Expand Down Expand Up @@ -63,13 +64,6 @@ pub struct Credential {
pub refresh_service: Option<OneOrMany<RefreshService>>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
pub enum OneOrMany<T> {
One(T),
Many(Vec<T>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(untagged)]
#[serde(try_from = "OneOrMany<Context>")]
Expand Down Expand Up @@ -231,61 +225,6 @@ pub struct JWTClaims {
pub verifiable_presentation: Option<Presentation>,
}

impl<T> OneOrMany<T> {
pub fn any<F>(&self, f: F) -> bool
where
F: Fn(&T) -> bool,
{
match self {
Self::One(value) => f(value),
Self::Many(values) => values.iter().any(f),
}
}

pub fn len(&self) -> usize {
match self {
Self::One(_) => 1,
Self::Many(values) => values.len(),
}
}

pub fn contains(&self, x: &T) -> bool
where
T: PartialEq<T>,
{
match self {
Self::One(value) => x == value,
Self::Many(values) => values.contains(x),
}
}

pub fn first(&self) -> Option<&T> {
match self {
Self::One(value) => Some(&value),
Self::Many(values) => {
if values.len() > 0 {
Some(&values[0])
} else {
None
}
}
}
}

pub fn to_single(&self) -> Option<&T> {
match self {
Self::One(value) => Some(&value),
Self::Many(values) => {
if values.len() == 1 {
Some(&values[0])
} else {
None
}
}
}
}
}

impl TryFrom<OneOrMany<Context>> for Contexts {
type Error = Error;
fn try_from(context: OneOrMany<Context>) -> Result<Self, Self::Error> {
Expand Down

0 comments on commit 5117509

Please sign in to comment.