Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Prev Previous commit
Next Next commit
Add compile test and missing conversion cases
  • Loading branch information
KiChjang committed Aug 13, 2021
commit 4bd110c117187d9f57bd85a7337a28ead60b8614
12 changes: 12 additions & 0 deletions xcm/procedural/src/multilocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ fn generate_conversion_from_tuples() -> TokenStream {
}
}

impl From<(Ancestor, #(#junctions),*)> for MultiLocation {
fn from( ( Ancestor(parents), #(#idents),* ): (Ancestor, #(#junctions),* ) ) -> Self {
MultiLocation { parents, interior: Junctions::#variant( #(#idents),* ) }
}
}

impl From<[Junction; #array_size]> for MultiLocation {
fn from(j: [Junction; #array_size]) -> Self {
let [#(#idents),*] = j;
Expand Down Expand Up @@ -108,6 +114,12 @@ fn generate_conversion_from_tuples() -> TokenStream {
}
}

impl From<(Ancestor, Junctions)> for MultiLocation {
fn from((Ancestor(parents), interior): (Ancestor, Junctions)) -> Self {
MultiLocation { parents, interior }
}
}

impl From<()> for MultiLocation {
fn from(_: ()) -> Self {
MultiLocation { parents: 0, interior: Junctions::Here }
Expand Down
44 changes: 43 additions & 1 deletion xcm/src/v1/multilocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,7 @@ impl TryFrom<MultiLocation> for Junctions {

#[cfg(test)]
mod tests {
use super::{Junctions::*, MultiLocation};
use super::{Ancestor, AncestorThen, Junctions::*, MultiLocation, Parent, ParentThen};
use crate::opaque::v1::{Junction::*, NetworkId::Any};
use parity_scale_codec::{Decode, Encode};

Expand Down Expand Up @@ -864,4 +864,46 @@ mod tests {
assert_eq!(iter.next(), None);
assert_eq!(iter.next_back(), None);
}

#[test]
fn conversion_from_other_types_works() {
use crate::v0;
use core::convert::TryInto;

fn takes_multilocation<Arg: Into<MultiLocation>>(_arg: Arg) {}

takes_multilocation(Parent);
takes_multilocation(Here);
takes_multilocation(X1(Parachain(42)));
takes_multilocation((255, PalletInstance(8)));
takes_multilocation((Ancestor(5), Parachain(1), PalletInstance(3)));
takes_multilocation((Ancestor(2), Here));
takes_multilocation(AncestorThen(
3,
X2(Parachain(43), AccountIndex64 { network: Any, index: 155 }),
));
takes_multilocation((Parent, AccountId32 { network: Any, id: [0; 32] }));
takes_multilocation((Parent, Here));
takes_multilocation(ParentThen(X1(Parachain(75))));
takes_multilocation([Parachain(100), PalletInstance(3)]);

assert_eq!(v0::MultiLocation::Null.try_into(), Ok(MultiLocation::here()));
assert_eq!(
v0::MultiLocation::X1(v0::Junction::Parent).try_into(),
Ok(MultiLocation::parent())
);
assert_eq!(
v0::MultiLocation::X2(v0::Junction::Parachain(88), v0::Junction::Parent).try_into(),
Err::<MultiLocation, ()>(()),
);
assert_eq!(
v0::MultiLocation::X3(
v0::Junction::Parent,
v0::Junction::Parent,
v0::Junction::GeneralKey(b"foo".to_vec()),
)
.try_into(),
Ok(MultiLocation { parents: 2, interior: X1(GeneralKey(b"foo".to_vec())) }),
);
}
}