#![deny(clippy::all)] #![deny(clippy::pedantic)] #![deny(missing_docs)] //! Utility macros that should be in std but is not. use std::iter::repeat; use proc_macro::TokenStream; use quote::quote; use syn::parse::Parse; use syn::{parse_macro_input, LitChar, LitInt, Token}; /// Repeats a character N number of times resulting in a string literal. /// /// # Arguments /// Seperated by commas. /// /// - The character to repeat /// - Number of times to repeat it. /// /// # Examples /// ``` /// # use utility_macros::repeat_char; /// /// let eight_o = repeat_char!('o', 8); /// /// assert_eq!(eight_o, "oooooooo"); /// ``` #[proc_macro] pub fn repeat_char(input: TokenStream) -> TokenStream { let RepeatCharMacroArgs { character, count } = parse_macro_input!(input as RepeatCharMacroArgs); let repeated = repeat(character.value()).take(count).collect::(); quote! { #repeated } .into() } struct RepeatCharMacroArgs { character: LitChar, count: usize, } impl Parse for RepeatCharMacroArgs { fn parse(input: syn::parse::ParseStream) -> syn::Result { let character = input.parse::()?; input.parse::()?; let count = input.parse::()?.base10_parse::()?; Ok(Self { character, count }) } }