Skip to content

Commit

Permalink
add minor improvements for entry tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Ondrej Prazak committed Jan 12, 2024
1 parent c2d7e0e commit f85aedc
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 53 deletions.
55 changes: 5 additions & 50 deletions aries/aries_vcx_core/src/wallet2/entry_tag.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,21 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[derive(Clone, Debug, Serialize, Deserialize, Eq, PartialEq, PartialOrd, Ord)]
pub enum EntryTag {
Encrypted(String, String),
Plaintext(String, String),
}

#[cfg(feature = "vdrtools_wallet")]
impl From<EntryTag> for (String, String) {
fn from(value: EntryTag) -> Self {
match value {
EntryTag::Encrypted(key, val) => (key, val),
EntryTag::Plaintext(key, val) => (format!("~{}", key), val),
}
}
}

#[cfg(feature = "vdrtools_wallet")]
impl From<(String, String)> for EntryTag {
fn from(value: (String, String)) -> Self {
if value.0.starts_with('~') {
EntryTag::Plaintext(value.0.trim_start_matches('~').into(), value.1)
} else {
EntryTag::Encrypted(value.0, value.1)
}
}
}

#[derive(Debug, Default, Clone, PartialEq)]
pub struct EntryTags {
inner: Vec<EntryTag>,
}

impl EntryTags {
pub fn new(inner: Vec<EntryTag>) -> Self {
Self { inner }
}

pub fn add(&mut self, tag: EntryTag) {
self.inner.push(tag)
}
Expand Down Expand Up @@ -79,29 +60,3 @@ impl From<EntryTags> for Vec<EntryTag> {
value.inner
}
}

#[cfg(feature = "vdrtools_wallet")]
impl From<EntryTags> for HashMap<String, String> {
fn from(value: EntryTags) -> Self {
let tags: Vec<EntryTag> = value.into();
tags.into_iter().fold(Self::new(), |mut memo, item| {
let (key, value) = item.into();

memo.insert(key, value);
memo
})
}
}

#[cfg(feature = "vdrtools_wallet")]
impl From<HashMap<String, String>> for EntryTags {
fn from(value: HashMap<String, String>) -> Self {
Self {
inner: value
.into_iter()
.map(|(key, value)| (key, value))
.map(From::from)
.collect(),
}
}
}
52 changes: 51 additions & 1 deletion aries/aries_vcx_core/src/wallet2/indy_wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
use super::BaseWallet2;
use std::collections::HashMap;

use super::{
entry_tag::{EntryTag, EntryTags},
BaseWallet2,
};
use crate::wallet::indy::IndySdkWallet;

pub mod indy_did_wallet;
Expand All @@ -10,3 +15,48 @@ const WALLET_OPTIONS: &str =
const SEARCH_OPTIONS: &str = r#"{"retrieveType": true, "retrieveValue": true, "retrieveTags": true, "retrieveRecords": true}"#;

impl BaseWallet2 for IndySdkWallet {}

impl From<EntryTag> for (String, String) {
fn from(value: EntryTag) -> Self {
match value {
EntryTag::Encrypted(key, val) => (key, val),
EntryTag::Plaintext(key, val) => (format!("~{}", key), val),
}
}
}

impl From<(String, String)> for EntryTag {
fn from(value: (String, String)) -> Self {
if value.0.starts_with('~') {
EntryTag::Plaintext(value.0.trim_start_matches('~').into(), value.1)
} else {
EntryTag::Encrypted(value.0, value.1)
}
}
}

impl From<EntryTags> for HashMap<String, String> {
fn from(value: EntryTags) -> Self {
let tags: Vec<EntryTag> = value.into();
tags.into_iter().fold(Self::new(), |mut memo, item| {
let (key, value) = item.into();

memo.insert(key, value);
memo
})
}
}

impl From<HashMap<String, String>> for EntryTags {
fn from(value: HashMap<String, String>) -> Self {
let mut items: Vec<EntryTag> = value
.into_iter()
.map(|(key, value)| (key, value))
.map(From::from)
.collect();

items.sort();

Self::new(items)
}
}
3 changes: 1 addition & 2 deletions aries/misc/test_utils/src/devsetup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,7 @@ pub async fn dev_build_featured_indy_ledger(
};
}

#[allow(unreachable_code)]
#[allow(unused_variables)]
#[allow(clippy::needless_return)]
pub async fn dev_build_featured_anoncreds() -> impl BaseAnonCreds {
#[cfg(feature = "credx")]
{
Expand Down

0 comments on commit f85aedc

Please sign in to comment.