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 = if let Ok(location) = (*location).clone().try_into() {
31 location
32 } else {
33 return false;
34 };
35 let latest_t =
36 if let Ok(location) = T::get().try_into() { location } else { return false };
37 latest_location.starts_with(&latest_t)
38 }
39}
40impl<T: Get<InteriorLocation>> Contains<InteriorLocation> for StartsWith<T> {
41 fn contains(t: &InteriorLocation) -> bool {
42 t.starts_with(&T::get())
43 }
44}
45
46pub struct StartsWithExplicitGlobalConsensus<T>(core::marker::PhantomData<T>);
50impl<T: Get<NetworkId>> Contains<Location> for StartsWithExplicitGlobalConsensus<T> {
51 fn contains(location: &Location) -> bool {
52 matches!(location.interior().global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
53 }
54}
55impl<T: Get<NetworkId>> Contains<InteriorLocation> for StartsWithExplicitGlobalConsensus<T> {
56 fn contains(location: &InteriorLocation) -> bool {
57 matches!(location.global_consensus(), Ok(requested_network) if requested_network.eq(&T::get()))
58 }
59}
60
61pub struct WithLatestLocationConverter<Target>(PhantomData<Target>);
64impl<Target: TryInto<Location> + TryFrom<Location> + Clone> MaybeEquivalence<Location, Target>
65 for WithLatestLocationConverter<Target>
66{
67 fn convert(old: &Location) -> Option<Target> {
68 (*old).clone().try_into().ok()
69 }
70
71 fn convert_back(new: &Target) -> Option<Location> {
72 new.clone().try_into().ok()
73 }
74}