feat(codec): improve KeyValuePairs ergonomics (#515)
This commit is contained in:
parent
615e2d4c29
commit
80d121d13a
1 changed files with 60 additions and 1 deletions
|
|
@ -4,7 +4,7 @@ use minicbor::{
|
||||||
Decode, Encode,
|
Decode, Encode,
|
||||||
};
|
};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::{fmt, hash::Hash as StdHash, ops::Deref};
|
use std::{collections::HashMap, fmt, hash::Hash as StdHash, ops::Deref};
|
||||||
|
|
||||||
static TAG_SET: u64 = 258;
|
static TAG_SET: u64 = 258;
|
||||||
|
|
||||||
|
|
@ -64,6 +64,16 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<K, V> FromIterator<(K, V)> for KeyValuePairs<K, V>
|
||||||
|
where
|
||||||
|
K: Clone,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
fn from_iter<T: IntoIterator<Item = (K, V)>>(iter: T) -> Self {
|
||||||
|
KeyValuePairs::Def(Vec::from_iter(iter))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)>
|
impl<K, V> From<KeyValuePairs<K, V>> for Vec<(K, V)>
|
||||||
where
|
where
|
||||||
K: Clone,
|
K: Clone,
|
||||||
|
|
@ -87,6 +97,28 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<K, V> From<KeyValuePairs<K, V>> for HashMap<K, V>
|
||||||
|
where
|
||||||
|
K: Clone + Eq + std::hash::Hash,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
fn from(other: KeyValuePairs<K, V>) -> Self {
|
||||||
|
match other {
|
||||||
|
KeyValuePairs::Def(x) => x.into_iter().collect(),
|
||||||
|
KeyValuePairs::Indef(x) => x.into_iter().collect(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<K, V> From<HashMap<K, V>> for KeyValuePairs<K, V>
|
||||||
|
where
|
||||||
|
K: Clone,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
fn from(other: HashMap<K, V>) -> Self {
|
||||||
|
KeyValuePairs::Def(other.into_iter().collect())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<K, V> Deref for KeyValuePairs<K, V>
|
impl<K, V> Deref for KeyValuePairs<K, V>
|
||||||
where
|
where
|
||||||
K: Clone,
|
K: Clone,
|
||||||
|
|
@ -230,6 +262,33 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<K, V> TryFrom<KeyValuePairs<K, V>> for NonEmptyKeyValuePairs<K, V>
|
||||||
|
where
|
||||||
|
K: Clone,
|
||||||
|
V: Clone,
|
||||||
|
{
|
||||||
|
type Error = String;
|
||||||
|
|
||||||
|
fn try_from(value: KeyValuePairs<K, V>) -> Result<Self, Self::Error> {
|
||||||
|
match value {
|
||||||
|
KeyValuePairs::Def(x) => {
|
||||||
|
if x.is_empty() {
|
||||||
|
Err("NonEmptyKeyValuePairs must contain at least one element".into())
|
||||||
|
} else {
|
||||||
|
Ok(NonEmptyKeyValuePairs::Def(x))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
KeyValuePairs::Indef(x) => {
|
||||||
|
if x.is_empty() {
|
||||||
|
Err("NonEmptyKeyValuePairs must contain at least one element".into())
|
||||||
|
} else {
|
||||||
|
Ok(NonEmptyKeyValuePairs::Indef(x))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<K, V> Deref for NonEmptyKeyValuePairs<K, V>
|
impl<K, V> Deref for NonEmptyKeyValuePairs<K, V>
|
||||||
where
|
where
|
||||||
K: Clone,
|
K: Clone,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue