use core::marker::PhantomData;
use frame_support::traits::{Contains, Get};
use sp_runtime::traits::MaybeEquivalence;
use xcm::latest::{InteriorLocation, Location, NetworkId};
pub struct StartsWith<T, L = Location>(core::marker::PhantomData<(T, L)>);
impl<T: Get<L>, L: TryInto<Location> + Clone> Contains<L> for StartsWith<T, L> {
fn contains(location: &L) -> bool {
let latest_location: Location =
if let Ok(location) = (*location).clone().try_into() { location } else { return false };
let latest_t = if let Ok(location) = T::get().try_into() { location } else { return false };
latest_location.starts_with(&latest_t)
}
}
impl<T: Get<InteriorLocation>> Contains<InteriorLocation> for StartsWith<T> {
fn contains(t: &InteriorLocation) -> bool {
t.starts_with(&T::get())
}
}
pub struct StartsWithExplicitGlobalConsensus<T>(core::marker::PhantomData<T>);
impl<T: Get<NetworkId>> Contains<Location> for StartsWithExplicitGlobalConsensus<T> {
fn contains(location: &Location) -> bool {
matches!(location.interior().global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
}
}
impl<T: Get<NetworkId>> Contains<InteriorLocation> for StartsWithExplicitGlobalConsensus<T> {
fn contains(location: &InteriorLocation) -> bool {
matches!(location.global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
}
}
pub struct WithLatestLocationConverter<Target>(PhantomData<Target>);
impl<Target: TryInto<Location> + TryFrom<Location> + Clone> MaybeEquivalence<Location, Target>
for WithLatestLocationConverter<Target>
{
fn convert(old: &Location) -> Option<Target> {
(*old).clone().try_into().ok()
}
fn convert_back(new: &Target) -> Option<Location> {
new.clone().try_into().ok()
}
}