referrerpolicy=no-referrer-when-downgrade

assets_common/
local_and_foreign_assets.rs

1// Copyright (C) Parity Technologies (UK) Ltd.
2// SPDX-License-Identifier: Apache-2.0
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// 	http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16use core::marker::PhantomData;
17use frame_support::traits::Get;
18use sp_runtime::{
19	traits::{Convert, MaybeEquivalence},
20	Either,
21	Either::{Left, Right},
22};
23use xcm::latest::Location;
24
25/// Converts a given [`Location`] to [`Either::Left`] when equal to `Target`, or
26/// [`Either::Right`] otherwise.
27///
28/// Suitable for use as a `Criterion` with [`frame_support::traits::tokens::fungible::UnionOf`].
29pub struct TargetFromLeft<Target, L = Location>(PhantomData<(Target, L)>);
30impl<Target: Get<L>, L: PartialEq + Eq> Convert<L, Either<(), L>> for TargetFromLeft<Target, L> {
31	fn convert(l: L) -> Either<(), L> {
32		Target::get().eq(&l).then(|| Left(())).map_or(Right(l), |n| n)
33	}
34}
35
36/// Converts a given [`Location`] to [`Either::Left`] based on the `Equivalence` criteria.
37/// Returns [`Either::Right`] if not equivalent.
38///
39/// Suitable for use as a `Criterion` with [`frame_support::traits::tokens::fungibles::UnionOf`].
40pub struct LocalFromLeft<Equivalence, AssetId, L = Location>(
41	PhantomData<(Equivalence, AssetId, L)>,
42);
43impl<Equivalence, AssetId, L> Convert<L, Either<AssetId, L>>
44	for LocalFromLeft<Equivalence, AssetId, L>
45where
46	Equivalence: MaybeEquivalence<L, AssetId>,
47{
48	fn convert(l: L) -> Either<AssetId, L> {
49		match Equivalence::convert(&l) {
50			Some(id) => Left(id),
51			None => Right(l),
52		}
53	}
54}