diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 09:22:09 +0000 |
commit | 43a97878ce14b72f0981164f87f2e35e14151312 (patch) | |
tree | 620249daf56c0258faa40cbdcf9cfba06de2a846 /third_party/rust/unic-langid-macros-impl/src | |
parent | Initial commit. (diff) | |
download | firefox-43a97878ce14b72f0981164f87f2e35e14151312.tar.xz firefox-43a97878ce14b72f0981164f87f2e35e14151312.zip |
Adding upstream version 110.0.1.upstream/110.0.1upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/unic-langid-macros-impl/src')
-rw-r--r-- | third_party/rust/unic-langid-macros-impl/src/lib.rs | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/third_party/rust/unic-langid-macros-impl/src/lib.rs b/third_party/rust/unic-langid-macros-impl/src/lib.rs new file mode 100644 index 0000000000..ee27a51e81 --- /dev/null +++ b/third_party/rust/unic-langid-macros-impl/src/lib.rs @@ -0,0 +1,108 @@ +extern crate proc_macro; + +use proc_macro::TokenStream; + +use proc_macro_hack::proc_macro_hack; +use quote::quote; +use syn::{parse_macro_input, LitStr}; + +use unic_langid_impl::{subtags, LanguageIdentifier}; + +#[proc_macro_hack] +pub fn lang(input: TokenStream) -> TokenStream { + let id = parse_macro_input!(input as LitStr); + let parsed: subtags::Language = id.value().parse().expect("Malformed Language Subtag"); + + let lang: Option<u64> = parsed.into(); + let lang = if let Some(lang) = lang { + quote!(unsafe { $crate::subtags::Language::from_raw_unchecked(#lang) }) + } else { + quote!(None) + }; + + TokenStream::from(quote! { + #lang + }) +} + +#[proc_macro_hack] +pub fn script(input: TokenStream) -> TokenStream { + let id = parse_macro_input!(input as LitStr); + let parsed: subtags::Script = id.value().parse().expect("Malformed Script Subtag"); + + let script: u32 = parsed.into(); + + TokenStream::from(quote! { + unsafe { $crate::subtags::Script::from_raw_unchecked(#script) } + }) +} + +#[proc_macro_hack] +pub fn region(input: TokenStream) -> TokenStream { + let id = parse_macro_input!(input as LitStr); + let parsed: subtags::Region = id.value().parse().expect("Malformed Region Subtag"); + + let region: u32 = parsed.into(); + + TokenStream::from(quote! { + unsafe { $crate::subtags::Region::from_raw_unchecked(#region) } + }) +} + +#[proc_macro_hack] +pub fn variant_fn(input: TokenStream) -> TokenStream { + let id = parse_macro_input!(input as LitStr); + let parsed: subtags::Variant = id.value().parse().expect("Malformed Variant Subtag"); + + let variant: u64 = parsed.into(); + + TokenStream::from(quote! { + unsafe { $crate::subtags::Variant::from_raw_unchecked(#variant) } + }) +} + +#[proc_macro_hack] +pub fn langid(input: TokenStream) -> TokenStream { + let id = parse_macro_input!(input as LitStr); + let parsed: LanguageIdentifier = id.value().parse().expect("Malformed Language Identifier"); + + let (lang, script, region, variants) = parsed.into_parts(); + + let lang: Option<u64> = lang.into(); + let lang = if let Some(lang) = lang { + quote!(unsafe { $crate::subtags::Language::from_raw_unchecked(#lang) }) + } else { + quote!($crate::subtags::Language::default()) + }; + + let script = if let Some(script) = script { + let script: u32 = script.into(); + quote!(Some(unsafe { $crate::subtags::Script::from_raw_unchecked(#script) })) + } else { + quote!(None) + }; + + let region = if let Some(region) = region { + let region: u32 = region.into(); + quote!(Some(unsafe { $crate::subtags::Region::from_raw_unchecked(#region) })) + } else { + quote!(None) + }; + + let variants = if !variants.is_empty() { + let v: Vec<_> = variants + .iter() + .map(|v| { + let variant: u64 = v.into(); + quote!(unsafe { $crate::subtags::Variant::from_raw_unchecked(#variant) }) + }) + .collect(); + quote!(Some(Box::new([#(#v,)*]))) + } else { + quote!(None) + }; + + TokenStream::from(quote! { + unsafe { $crate::LanguageIdentifier::from_raw_parts_unchecked(#lang, #script, #region, #variants) } + }) +} |