Skip to content

Commit

Permalink
simplify entry tag
Browse files Browse the repository at this point in the history
Signed-off-by: Ondrej Prazak <[email protected]>
  • Loading branch information
Ondrej Prazak committed Jan 31, 2024
1 parent 16b4feb commit 562537a
Show file tree
Hide file tree
Showing 12 changed files with 78 additions and 125 deletions.
2 changes: 1 addition & 1 deletion aries/aries_vcx_core/src/anoncreds/credx_anoncreds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::{
},
wallet::{
base_wallet::{record::Record, search_filter::SearchFilter, BaseWallet, RecordWallet},
entry_tag::EntryTags,
entry_tags::EntryTags,
},
};

Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx_core/src/wallet/agency_client_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use super::{
};
use crate::{
errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult},
wallet::entry_tag::EntryTags,
wallet::entry_tags::EntryTags,
};

#[derive(Debug)]
Expand Down
12 changes: 6 additions & 6 deletions aries/aries_vcx_core/src/wallet/base_wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use async_trait::async_trait;
use public_key::Key;

use super::entry_tag::EntryTags;
use super::entry_tags::EntryTags;
use crate::{
errors::error::VcxCoreResult,
wallet::{
Expand Down Expand Up @@ -82,7 +82,7 @@ mod tests {
errors::error::AriesVcxCoreErrorKind,
wallet::{
base_wallet::{DidWallet, Record, RecordWallet},
entry_tag::{EntryTag, EntryTags},
entry_tags::EntryTags,
},
};

Expand Down Expand Up @@ -264,7 +264,7 @@ mod tests {
let category = "my";
let value1 = "xxx";
let value2 = "yyy";
let tags1: EntryTags = vec![EntryTag::Plaintext("a".into(), "b".into())].into();
let tags1: EntryTags = vec![("a".into(), "b".into())].into();
let tags2 = EntryTags::default();

let record = Record::builder()
Expand Down Expand Up @@ -297,7 +297,7 @@ mod tests {
let category = "my";
let value1 = "xxx";
let value2 = "yyy";
let tags: EntryTags = vec![EntryTag::Plaintext("a".into(), "b".into())].into();
let tags: EntryTags = vec![("a".into(), "b".into())].into();

let record = Record::builder()
.name(name.into())
Expand All @@ -324,8 +324,8 @@ mod tests {
let name = "foo";
let category = "my";
let value = "xxx";
let tags1: EntryTags = vec![EntryTag::Plaintext("a".into(), "b".into())].into();
let tags2: EntryTags = vec![EntryTag::Plaintext("c".into(), "d".into())].into();
let tags1: EntryTags = vec![("a".into(), "b".into())].into();
let tags2: EntryTags = vec![("c".into(), "d".into())].into();

let record = Record::builder()
.name(name.into())
Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx_core/src/wallet/base_wallet/record.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use typed_builder::TypedBuilder;

use crate::wallet::entry_tag::EntryTags;
use crate::wallet::entry_tags::EntryTags;

#[derive(Debug, Default, Clone, TypedBuilder)]
pub struct Record {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,26 @@ use std::fmt;

use serde::{de::Visitor, ser::SerializeMap, Deserialize, Serialize};

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

impl EntryTag {
pub fn from_pair(pair: (String, String)) -> Self {
if pair.0.starts_with('~') {
EntryTag::Plaintext(pair.0.trim_start_matches('~').into(), pair.1)
} else {
EntryTag::Encrypted(pair.0, pair.1)
}
}

pub fn key(&self) -> &str {
match self {
Self::Encrypted(key, _) => key,
Self::Plaintext(key, _) => key,
}
}
}
// #[derive(Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
// pub enum EntryTag {
// Tag(String, String),
// }

// impl EntryTag {
// pub fn from_pair(pair: (String, String)) -> Self {
// Self::Tag(pair.0, pair.1)
// }

// pub fn key(&self) -> &str {
// match self {
// Self::Tag(key, _) => key,
// }
// }
// }

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

impl Serialize for EntryTags {
Expand All @@ -37,10 +31,7 @@ impl Serialize for EntryTags {
{
let mut map = serializer.serialize_map(Some(self.inner.len()))?;
for tag in self.inner.iter() {
match tag {
EntryTag::Encrypted(key, val) => map.serialize_entry(&key, &val)?,
EntryTag::Plaintext(key, val) => map.serialize_entry(&format!("~{}", key), &val)?,
}
map.serialize_entry(&tag.0, &tag.1)?
}
map.end()
}
Expand All @@ -62,7 +53,7 @@ impl<'de> Visitor<'de> for EntryTagsVisitor {
let mut tags = EntryTags::new(vec![]);

while let Some(pair) = map.next_entry()? {
tags.add(EntryTag::from_pair(pair));
tags.add(pair);
}

Ok(tags)
Expand All @@ -79,14 +70,14 @@ impl<'de> Deserialize<'de> for EntryTags {
}

impl EntryTags {
pub fn new(inner: Vec<EntryTag>) -> Self {
pub fn new(inner: Vec<(String, String)>) -> Self {
let mut items = inner;
items.sort();

Self { inner: items }
}

pub fn add(&mut self, tag: EntryTag) {
pub fn add(&mut self, tag: (String, String)) {
self.inner.push(tag);
self.inner.sort();
}
Expand All @@ -95,7 +86,7 @@ impl EntryTags {
self.inner.is_empty()
}

pub fn into_inner(self) -> Vec<EntryTag> {
pub fn into_inner(self) -> Vec<(String, String)> {
self.inner
}

Expand All @@ -104,15 +95,14 @@ impl EntryTags {
self.inner.sort();
}

pub fn remove(&mut self, tag: EntryTag) {
self.inner
.retain(|existing_tag| existing_tag.key() != tag.key());
pub fn remove(&mut self, tag: (String, String)) {
self.inner.retain(|existing_tag| existing_tag.0 != tag.0);
self.inner.sort();
}
}

impl IntoIterator for EntryTags {
type Item = EntryTag;
type Item = (String, String);

type IntoIter = std::vec::IntoIter<Self::Item>;

Expand All @@ -121,8 +111,8 @@ impl IntoIterator for EntryTags {
}
}

impl FromIterator<EntryTag> for EntryTags {
fn from_iter<T: IntoIterator<Item = EntryTag>>(iter: T) -> Self {
impl FromIterator<(String, String)> for EntryTags {
fn from_iter<T: IntoIterator<Item = (String, String)>>(iter: T) -> Self {
let mut tags = Self::default();

for item in iter {
Expand All @@ -132,16 +122,16 @@ impl FromIterator<EntryTag> for EntryTags {
}
}

impl From<Vec<EntryTag>> for EntryTags {
fn from(value: Vec<EntryTag>) -> Self {
impl From<Vec<(String, String)>> for EntryTags {
fn from(value: Vec<(String, String)>) -> Self {
value.into_iter().fold(Self::default(), |mut memo, item| {
memo.add(item);
memo
})
}
}

impl From<EntryTags> for Vec<EntryTag> {
impl From<EntryTags> for Vec<(String, String)> {
fn from(value: EntryTags) -> Self {
value.inner
}
Expand All @@ -151,14 +141,11 @@ impl From<EntryTags> for Vec<EntryTag> {
mod tests {
use serde_json::json;

use crate::wallet::entry_tag::{EntryTag, EntryTags};
use crate::wallet::entry_tags::EntryTags;

#[test]
fn test_entry_tags_serialize() {
let tags = EntryTags::new(vec![
EntryTag::Plaintext("a".into(), "b".into()),
EntryTag::Encrypted("c".into(), "d".into()),
]);
let tags = EntryTags::new(vec![("~a".into(), "b".into()), ("c".into(), "d".into())]);

let res = serde_json::to_string(&tags).unwrap();

Expand All @@ -169,10 +156,7 @@ mod tests {
fn test_entry_tags_deserialize() {
let json = json!({"a":"b", "~c":"d"});

let tags = EntryTags::new(vec![
EntryTag::Encrypted("a".into(), "b".into()),
EntryTag::Plaintext("c".into(), "d".into()),
]);
let tags = EntryTags::new(vec![("a".into(), "b".into()), ("~c".into(), "d".into())]);

let res = serde_json::from_str(&json.to_string()).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions aries/aries_vcx_core/src/wallet/indy/indy_record_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ use serde::Deserialize;
use serde_json::Value;
use vdrtools::Locator;

use super::{indy_tag::IndyTags, SEARCH_OPTIONS, WALLET_OPTIONS};
use super::{indy_tags::IndyTags, SEARCH_OPTIONS, WALLET_OPTIONS};
use crate::{
errors::error::{AriesVcxCoreError, VcxCoreResult},
wallet::{
base_wallet::{record::Record, search_filter::SearchFilter, RecordWallet},
entry_tag::EntryTags,
entry_tags::EntryTags,
indy::IndySdkWallet,
},
};
Expand Down
60 changes: 0 additions & 60 deletions aries/aries_vcx_core/src/wallet/indy/indy_tag.rs

This file was deleted.

29 changes: 29 additions & 0 deletions aries/aries_vcx_core/src/wallet/indy/indy_tags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use std::collections::HashMap;

use crate::wallet::entry_tags::EntryTags;

pub(crate) struct IndyTags(HashMap<String, String>);

impl IndyTags {
pub fn new(map: HashMap<String, String>) -> Self {
Self(map)
}

pub fn into_inner(self) -> HashMap<String, String> {
self.0
}

pub fn from_entry_tags(tags: EntryTags) -> Self {
let mut map = HashMap::new();
let tags_vec: Vec<_> = tags.into_iter().collect();
map.extend(tags_vec);
Self(map)
}

pub fn into_entry_tags(self) -> EntryTags {
let mut items: Vec<(String, String)> = self.0.into_iter().collect();
items.sort();

EntryTags::new(items)
}
}
4 changes: 2 additions & 2 deletions aries/aries_vcx_core/src/wallet/indy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use indy_api_types::domain::wallet::IndyRecord;
use serde::{Deserialize, Serialize};
use typed_builder::TypedBuilder;

use self::indy_tag::IndyTags;
use self::indy_tags::IndyTags;
use super::base_wallet::{record::Record, BaseWallet};
use crate::{errors::error::VcxCoreResult, WalletHandle};

mod indy_did_wallet;
mod indy_record_wallet;
mod indy_tag;
mod indy_tags;
pub mod internal;
pub mod wallet;

Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx_core/src/wallet/mock_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use super::{
use crate::{
errors::error::{AriesVcxCoreError, AriesVcxCoreErrorKind, VcxCoreResult},
utils::{self},
wallet::entry_tag::EntryTags,
wallet::entry_tags::EntryTags,
};

#[derive(Debug)]
Expand Down
2 changes: 1 addition & 1 deletion aries/aries_vcx_core/src/wallet/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod agency_client_wallet;
pub mod base_wallet;
pub mod entry_tag;
pub mod entry_tags;
#[cfg(feature = "vdrtools_wallet")]
pub mod indy;
pub mod mock_wallet;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use aries_vcx::{
};
use aries_vcx_core::wallet::{
base_wallet::{record::Record, DidWallet, RecordWallet},
entry_tag::EntryTags,
entry_tags::EntryTags,
indy::IndyWalletRecord,
};
use futures::FutureExt;
Expand Down

0 comments on commit 562537a

Please sign in to comment.