staging_xcm_builder/
matches_location.rs1use core::marker::PhantomData;
21use frame_support::traits::{Contains, Get};
22use sp_runtime::traits::MaybeEquivalence;
23use xcm::latest::{InteriorLocation, Location, NetworkId};
24
25pub struct StartsWith<T, L = Location>(core::marker::PhantomData<(T, L)>);
28impl<T: Get<L>, L: TryInto<Location> + Clone> Contains<L> for StartsWith<T, L> {
29 fn contains(location: &L) -> bool {
30 let latest_location: Location =
31 if let Ok(location) = (*location).clone().try_into() { location } else { return false };
32 let latest_t = if let Ok(location) = T::get().try_into() { location } else { return false };
33 latest_location.starts_with(&latest_t)
34 }
35}
36impl<T: Get<InteriorLocation>> Contains<InteriorLocation> for StartsWith<T> {
37 fn contains(t: &InteriorLocation) -> bool {
38 t.starts_with(&T::get())
39 }
40}
41
42pub struct StartsWithExplicitGlobalConsensus<T>(core::marker::PhantomData<T>);
46impl<T: Get<NetworkId>> Contains<Location> for StartsWithExplicitGlobalConsensus<T> {
47 fn contains(location: &Location) -> bool {
48 matches!(location.interior().global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
49 }
50}
51impl<T: Get<NetworkId>> Contains<InteriorLocation> for StartsWithExplicitGlobalConsensus<T> {
52 fn contains(location: &InteriorLocation) -> bool {
53 matches!(location.global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
54 }
55}
56
57pub struct WithLatestLocationConverter<Target>(PhantomData<Target>);
60impl<Target: TryInto<Location> + TryFrom<Location> + Clone> MaybeEquivalence<Location, Target>
61 for WithLatestLocationConverter<Target>
62{
63 fn convert(old: &Location) -> Option<Target> {
64 (*old).clone().try_into().ok()
65 }
66
67 fn convert_back(new: &Target) -> Option<Location> {
68 new.clone().try_into().ok()
69 }
70}