use proc_macro2::{Span, TokenStream};
use quote::{format_ident, quote};
pub fn generate_conversion_functions(input: proc_macro::TokenStream) -> syn::Result<TokenStream> {
if !input.is_empty() {
return Err(syn::Error::new(Span::call_site(), "No arguments expected"))
}
let from_tuples = generate_conversion_from_tuples();
let from_v1 = generate_conversion_from_v1();
Ok(quote! {
#from_tuples
#from_v1
})
}
fn generate_conversion_from_tuples() -> TokenStream {
let from_tuples = (0..8usize)
.map(|num_junctions| {
let junctions =
(0..=num_junctions).map(|_| format_ident!("Junction")).collect::<Vec<_>>();
let idents = (0..=num_junctions).map(|i| format_ident!("j{}", i)).collect::<Vec<_>>();
let variant = &format_ident!("X{}", num_junctions + 1);
let array_size = num_junctions + 1;
quote! {
impl From<( #(#junctions,)* )> for MultiLocation {
fn from( ( #(#idents,)* ): ( #(#junctions,)* ) ) -> Self {
MultiLocation::#variant( #(#idents),* )
}
}
impl From<[Junction; #array_size]> for MultiLocation {
fn from(j: [Junction; #array_size]) -> Self {
let [#(#idents),*] = j;
MultiLocation::#variant( #(#idents),* )
}
}
}
})
.collect::<TokenStream>();
quote! {
impl From<()> for MultiLocation {
fn from(_: ()) -> Self {
MultiLocation::Null
}
}
impl From<Junction> for MultiLocation {
fn from(x: Junction) -> Self {
MultiLocation::X1(x)
}
}
impl From<[Junction; 0]> for MultiLocation {
fn from(_: [Junction; 0]) -> Self {
MultiLocation::Null
}
}
#from_tuples
}
}
fn generate_conversion_from_v1() -> TokenStream {
let match_variants = (0..8u8)
.map(|cur_num| {
let variant = format_ident!("X{}", cur_num + 1);
let idents = (1..=cur_num).map(|i| format_ident!("j{}", i)).collect::<Vec<_>>();
quote! {
crate::v1::Junctions::#variant( j0 #(, #idents)* ) => res
.pushed_with(Junction::from(j0))
#( .and_then(|res| res.pushed_with(Junction::from(#idents))) )*
.map_err(|_| ()),
}
})
.collect::<TokenStream>();
quote! {
impl TryFrom<crate::v1::MultiLocation> for MultiLocation {
type Error = ();
fn try_from(v1: crate::v1::MultiLocation) -> core::result::Result<Self, ()> {
let mut res = MultiLocation::Null;
for _ in 0..v1.parents {
res.push(Junction::Parent)?;
}
match v1.interior {
crate::v1::Junctions::Here => Ok(res),
#match_variants
}
}
}
}
}