Skip to content

Commit

Permalink
t macro now works with any number of subkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptistemontan committed Aug 31, 2023
1 parent d775e9f commit 300fed2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 28 deletions.
23 changes: 8 additions & 15 deletions leptos_i18n_macro/src/t_macro/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use quote::quote;
use syn::parse_macro_input;

use self::parsed_input::ParsedInput;
use self::parsed_input::{Keys, ParsedInput};

pub mod interpolate;
pub mod parsed_input;
Expand All @@ -14,25 +14,18 @@ pub fn t_macro(tokens: proc_macro::TokenStream) -> proc_macro::TokenStream {
pub fn t_macro_inner(input: ParsedInput) -> proc_macro2::TokenStream {
let ParsedInput {
context,
key,
keys,
interpolations,
} = input;
let get_key = match key {
parsed_input::Key::Key(key) => quote!(leptos_i18n::I18nContext::get_keys(#context).#key),
parsed_input::Key::Namespace { namespace, key } => {
quote!(leptos_i18n::I18nContext::get_keys(#context).#namespace.#key)
let get_key = match keys {
Keys::SingleKey(key) => quote!(leptos_i18n::I18nContext::get_keys(#context).#key),
Keys::Subkeys(keys) => quote!(leptos_i18n::I18nContext::get_keys(#context)#(.#keys)*),
Keys::Namespace(namespace, keys) => {
quote!(leptos_i18n::I18nContext::get_keys(#context).#namespace #(.#keys)*)
}
};
if let Some(interpolations) = interpolations {
quote! {
move || {
let _key = #get_key;
#(
let _key = _key.#interpolations;
)*
_key
}
}
quote!(move || #get_key #(.#interpolations)*)
} else {
quote!(move || #get_key)
}
Expand Down
41 changes: 28 additions & 13 deletions leptos_i18n_macro/src/t_macro/parsed_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,23 @@ use syn::Token;

use super::interpolate::InterpolatedValue;

pub enum Key {
Key(Ident),
Namespace { namespace: Ident, key: Ident },
pub enum Keys {
SingleKey(Ident),
Subkeys(Vec<Ident>),
Namespace(Ident, Vec<Ident>),
}

pub struct ParsedInput {
pub context: Ident,
pub key: Key,
pub keys: Keys,
pub interpolations: Option<Vec<InterpolatedValue>>,
}

impl syn::parse::Parse for ParsedInput {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let context = input.parse()?;
input.parse::<Comma>()?;
let key = input.parse()?;
let keys = input.parse()?;
let comma = input.parse::<Comma>();
let interpolations = match comma {
Ok(_) => {
Expand All @@ -34,22 +35,36 @@ impl syn::parse::Parse for ParsedInput {
};
Ok(ParsedInput {
context,
key,
keys,
interpolations,
})
}
}

impl syn::parse::Parse for Key {
fn parse_subkeys(input: syn::parse::ParseStream, keys: &mut Vec<Ident>) -> syn::Result<()> {
keys.push(input.parse()?);
while input.peek(Token![.]) {
input.parse::<Token![.]>()?;
keys.push(input.parse()?);
}
Ok(())
}

impl syn::parse::Parse for Keys {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let key = input.parse()?;
if input.peek(Token![.]) {
let first_key = input.parse()?;
if input.peek(Token![::]) {
input.parse::<Token![::]>()?;
let mut keys = vec![];
parse_subkeys(input, &mut keys)?;
Ok(Keys::Namespace(first_key, keys))
} else if input.peek(Token![.]) {
input.parse::<Token![.]>()?;
let namespace = key;
let key = input.parse()?;
Ok(Key::Namespace { namespace, key })
let mut keys = vec![first_key];
parse_subkeys(input, &mut keys)?;
Ok(Keys::Subkeys(keys))
} else {
Ok(Key::Key(key))
Ok(Keys::SingleKey(first_key))
}
}
}

0 comments on commit 300fed2

Please sign in to comment.