use codec::MaxEncodedLen;
use frame_support::Parameter;
pub trait RuntimeParameterStore {
type AggregatedKeyValue: AggregatedKeyValue;
fn get<KV, K>(key: K) -> Option<K::Value>
where
KV: AggregatedKeyValue,
K: Key + Into<<KV as AggregatedKeyValue>::Key>,
<KV as AggregatedKeyValue>::Key: IntoKey<
<<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key,
>,
<<Self as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value:
TryIntoKey<<KV as AggregatedKeyValue>::Value>,
<KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
}
pub trait ParameterStore<KV: AggregatedKeyValue> {
fn get<K>(key: K) -> Option<K::Value>
where
K: Key + Into<<KV as AggregatedKeyValue>::Key>,
<KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>;
}
pub trait Key {
type Value;
type WrappedValue: Into<Self::Value>;
}
pub trait AggregatedKeyValue: Parameter {
type Key: Parameter + MaxEncodedLen;
type Value: Parameter + MaxEncodedLen;
fn into_parts(self) -> (Self::Key, Option<Self::Value>);
}
impl AggregatedKeyValue for () {
type Key = ();
type Value = ();
fn into_parts(self) -> (Self::Key, Option<Self::Value>) {
((), None)
}
}
pub struct ParameterStoreAdapter<PS, KV>(core::marker::PhantomData<(PS, KV)>);
impl<PS, KV> ParameterStore<KV> for ParameterStoreAdapter<PS, KV>
where
PS: RuntimeParameterStore,
KV: AggregatedKeyValue,
<KV as AggregatedKeyValue>::Key:
IntoKey<<<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Key>,
<KV as AggregatedKeyValue>::Value: TryFromKey<
<<PS as RuntimeParameterStore>::AggregatedKeyValue as AggregatedKeyValue>::Value,
>,
{
fn get<K>(key: K) -> Option<K::Value>
where
K: Key + Into<<KV as AggregatedKeyValue>::Key>,
<KV as AggregatedKeyValue>::Value: TryInto<K::WrappedValue>,
{
PS::get::<KV, K>(key)
}
}
mod workaround {
pub trait FromKey<T>: Sized {
#[must_use]
fn from_key(value: T) -> Self;
}
pub trait IntoKey<T>: Sized {
#[must_use]
fn into_key(self) -> T;
}
impl<T, U> IntoKey<U> for T
where
U: FromKey<T>,
{
fn into_key(self) -> U {
U::from_key(self)
}
}
pub trait TryIntoKey<T>: Sized {
type Error;
fn try_into_key(self) -> Result<T, Self::Error>;
}
pub trait TryFromKey<T>: Sized {
type Error;
fn try_from_key(value: T) -> Result<Self, Self::Error>;
}
impl<T, U> TryIntoKey<U> for T
where
U: TryFromKey<T>,
{
type Error = U::Error;
fn try_into_key(self) -> Result<U, U::Error> {
U::try_from_key(self)
}
}
}
pub use workaround::*;