#[cfg(feature = "hashbrown-json")]
use super::{
one_or_many::{EmptyListError, OneOrMany},
zero_one_or_many::ZeroOneOrMany,
};
#[cfg(feature = "hashbrown-json")]
pub auto trait NotString {}
#[cfg(feature = "hashbrown-json")]
impl !NotString for String {}
#[cfg(feature = "hashbrown-json")]
pub trait JsonObjectExtStringString: Sized {
type Error;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<String>,
V: Into<String>;
fn from_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<String>,
{
Self::from_hashmap(f())
}
}
#[cfg(feature = "hashbrown-json")]
pub trait JsonObjectExtStringV<V1: NotString>: Sized {
type Error;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<String>,
V: Into<V1>;
fn from_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<V1>,
{
Self::from_hashmap(f())
}
}
#[cfg(feature = "hashbrown-json")]
pub trait JsonObjectExtKString<K1: NotString>: Sized {
type Error;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<K1>,
V: Into<String>;
fn from_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<String>,
{
Self::from_hashmap(f())
}
}
#[cfg(feature = "hashbrown-json")]
pub trait JsonObjectExtKV<K1: NotString, V1: NotString>: Sized {
type Error;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<K1>,
V: Into<V1>;
fn from_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<V1>,
{
Self::from_hashmap(f())
}
}
#[cfg(feature = "hashbrown-json")]
impl JsonObjectExtStringString for Vec<(String, String)> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<String>,
V: Into<String>,
{
Ok(map.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
}
}
#[cfg(feature = "hashbrown-json")]
impl<V1: NotString> JsonObjectExtStringV<V1> for Vec<(String, V1)> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<String>,
V: Into<V1>,
{
Ok(map.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1: NotString> JsonObjectExtKString<K1> for Vec<(K1, String)> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<K1>,
V: Into<String>,
{
Ok(map.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1: NotString, V1: NotString> JsonObjectExtKV<K1, V1> for Vec<(K1, V1)> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<K1>,
V: Into<V1>,
{
Ok(map.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
}
}
#[cfg(feature = "hashbrown-json")]
impl JsonObjectExtStringString for Option<Vec<(String, String)>> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<String>,
V: Into<String>,
{
let items: Vec<(String, String)> =
map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(if items.is_empty() { None } else { Some(items) })
}
}
#[cfg(feature = "hashbrown-json")]
impl<V1: NotString> JsonObjectExtStringV<V1> for Option<Vec<(String, V1)>> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<String>,
V: Into<V1>,
{
let items: Vec<(String, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(if items.is_empty() { None } else { Some(items) })
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1: NotString> JsonObjectExtKString<K1> for Option<Vec<(K1, String)>> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<K1>,
V: Into<String>,
{
let items: Vec<(K1, String)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(if items.is_empty() { None } else { Some(items) })
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1: NotString, V1: NotString> JsonObjectExtKV<K1, V1> for Option<Vec<(K1, V1)>> {
type Error = std::convert::Infallible;
fn from_hashmap<K, V>(map: ::hashbrown::HashMap<K, V>) -> Result<Self, Self::Error>
where
K: Into<K1>,
V: Into<V1>,
{
let items: Vec<(K1, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(if items.is_empty() { None } else { Some(items) })
}
}
#[cfg(feature = "hashbrown-json")]
pub trait CollectionJsonExtStringString {
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<String>;
}
#[cfg(feature = "hashbrown-json")]
pub trait CollectionJsonExtStringV<V1: NotString> {
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<V1>;
}
#[cfg(feature = "hashbrown-json")]
pub trait CollectionJsonExtKString<K1: NotString> {
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<String>;
}
#[cfg(feature = "hashbrown-json")]
pub trait CollectionJsonExtKV<K1: NotString, V1: NotString> {
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<V1>;
}
#[cfg(feature = "hashbrown-json")]
pub trait TryCollectionJsonExtStringString {
type Error;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<String>,
Self: Sized;
}
#[cfg(feature = "hashbrown-json")]
pub trait TryCollectionJsonExtStringV<V1: NotString> {
type Error;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<V1>,
Self: Sized;
}
#[cfg(feature = "hashbrown-json")]
pub trait TryCollectionJsonExtKString<K1: NotString> {
type Error;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<String>,
Self: Sized;
}
#[cfg(feature = "hashbrown-json")]
pub trait TryCollectionJsonExtKV<K1: NotString, V1: NotString> {
type Error;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<V1>,
Self: Sized;
}
#[cfg(feature = "hashbrown-json")]
impl TryCollectionJsonExtStringString for OneOrMany<(String, String)> {
type Error = EmptyListError;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<String>,
{
let map = f();
let items: Vec<(String, String)> =
map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
OneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl<V1> TryCollectionJsonExtStringV<V1> for OneOrMany<(String, V1)>
where
V1: NotString,
{
type Error = EmptyListError;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<V1>,
{
let map = f();
let items: Vec<(String, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
OneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1> TryCollectionJsonExtKString<K1> for OneOrMany<(K1, String)>
where
K1: NotString,
{
type Error = EmptyListError;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<String>,
{
let map = f();
let items: Vec<(K1, String)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
OneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1, V1> TryCollectionJsonExtKV<K1, V1> for OneOrMany<(K1, V1)>
where
K1: NotString,
V1: NotString,
{
type Error = EmptyListError;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<V1>,
{
let map = f();
let items: Vec<(K1, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
OneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl CollectionJsonExtStringString for Vec<(String, String)> {
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<String>,
{
f().into_iter().map(|(k, v)| (k.into(), v.into())).collect()
}
}
#[cfg(feature = "hashbrown-json")]
impl<V1> CollectionJsonExtStringV<V1> for Vec<(String, V1)>
where
V1: NotString,
{
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<V1>,
{
f().into_iter().map(|(k, v)| (k.into(), v.into())).collect()
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1> CollectionJsonExtKString<K1> for Vec<(K1, String)>
where
K1: NotString,
{
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<String>,
{
f().into_iter().map(|(k, v)| (k.into(), v.into())).collect()
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1, V1> CollectionJsonExtKV<K1, V1> for Vec<(K1, V1)>
where
K1: NotString,
V1: NotString,
{
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<V1>,
{
f().into_iter().map(|(k, v)| (k.into(), v.into())).collect()
}
}
#[cfg(feature = "hashbrown-json")]
impl CollectionJsonExtStringString for ZeroOneOrMany<(String, String)> {
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<String>,
{
let map = f();
let items: Vec<(String, String)> =
map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
ZeroOneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl<V1> CollectionJsonExtStringV<V1> for ZeroOneOrMany<(String, V1)>
where
V1: NotString,
{
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<V1>,
{
let map = f();
let items: Vec<(String, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
ZeroOneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1> CollectionJsonExtKString<K1> for ZeroOneOrMany<(K1, String)>
where
K1: NotString,
{
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<String>,
{
let map = f();
let items: Vec<(K1, String)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
ZeroOneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1, V1> CollectionJsonExtKV<K1, V1> for ZeroOneOrMany<(K1, V1)>
where
K1: NotString,
V1: NotString,
{
fn json<K, V, F>(f: F) -> Self
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<V1>,
{
let map = f();
let items: Vec<(K1, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
ZeroOneOrMany::many(items)
}
}
#[cfg(feature = "hashbrown-json")]
impl TryCollectionJsonExtStringString for ZeroOneOrMany<(String, String)> {
type Error = std::convert::Infallible;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<String>,
{
let map = f();
let items: Vec<(String, String)> =
map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(ZeroOneOrMany::many(items))
}
}
#[cfg(feature = "hashbrown-json")]
impl<V1> TryCollectionJsonExtStringV<V1> for ZeroOneOrMany<(String, V1)>
where
V1: NotString,
{
type Error = std::convert::Infallible;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<String>,
V: Into<V1>,
{
let map = f();
let items: Vec<(String, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(ZeroOneOrMany::many(items))
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1> TryCollectionJsonExtKString<K1> for ZeroOneOrMany<(K1, String)>
where
K1: NotString,
{
type Error = std::convert::Infallible;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<String>,
{
let map = f();
let items: Vec<(K1, String)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(ZeroOneOrMany::many(items))
}
}
#[cfg(feature = "hashbrown-json")]
impl<K1, V1> TryCollectionJsonExtKV<K1, V1> for ZeroOneOrMany<(K1, V1)>
where
K1: NotString,
V1: NotString,
{
type Error = std::convert::Infallible;
fn try_json<K, V, F>(f: F) -> Result<Self, Self::Error>
where
F: FnOnce() -> ::hashbrown::HashMap<K, V>,
K: Into<K1>,
V: Into<V1>,
{
let map = f();
let items: Vec<(K1, V1)> = map.into_iter().map(|(k, v)| (k.into(), v.into())).collect();
Ok(ZeroOneOrMany::many(items))
}
}