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 28, 2020
1 parent a3c0032 commit 164a7b8
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/bin/ssi-vc-test/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ssi::jwk::JWTKeys;
use ssi::one_or_many::OneOrMany;
use ssi::vc::Context;
use ssi::vc::Credential;
use ssi::vc::OneOrMany;
use ssi::vc::Presentation;

fn usage() {
Expand Down
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 mod one_or_many;
pub mod rdf;
pub mod vc;

Expand Down
89 changes: 89 additions & 0 deletions src/one_or_many.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
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 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
}
}
}
}
}

// 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(),
}
}
}
79 changes: 1 addition & 78 deletions src/vc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::convert::TryInto;

use crate::error::Error;
use crate::jwk::{Header, JWTKeys, JWK};
use crate::one_or_many::OneOrMany;
use crate::rdf::{
BlankNodeLabel, DataSet, IRIRef, Literal, Object, Predicate, Statement, StringLiteral, Subject,
};
Expand Down Expand Up @@ -69,13 +70,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 @@ -277,77 +271,6 @@ pub struct JWTClaims {
pub verifiable_presentation: Option<Presentation>,
}

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(),
}
}
}

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 164a7b8

Please sign in to comment.