lufa_rs_macros/
lib.rs

1//! This module provides procedural macros for use in the LUFA-based Rust keyboard firmware.
2mod hid_descriptor;
3
4use proc_macro::TokenStream;
5use quote::quote;
6use syn::Lit;
7use syn::parse_macro_input;
8
9/// Generates an HID descriptor for a USB device.
10#[proc_macro]
11pub fn hid_descriptor(input: TokenStream) -> TokenStream {
12    hid_descriptor::hid_descriptor_impl(input)
13}
14
15/// Converts a string literal into a wide character array.
16/// 
17/// This macro takes a string literal as input and converts each character into a 16-bit wide character.
18#[proc_macro]
19pub fn literal_to_wchar_array(input: TokenStream) -> TokenStream {
20    let mut res: Vec<i16> = Vec::new();
21    let lit = parse_macro_input!(input as Lit);
22    match lit {
23        Lit::Str(str_lit) => {
24            for c in str_lit.value().chars() {
25                res.push(TryInto::<u16>::try_into(c).unwrap() as i16);
26            }
27            quote! {[#(#res),*]}
28        }
29        _ => {
30            syn::Error::new(lit.span(), "Literal must be a string".to_string()).into_compile_error()
31        }
32    }
33    .into()
34}