summaryrefslogtreecommitdiff
path: root/src/lib.rs
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2022-11-12 15:01:19 +0100
committerHampusM <hampus@hampusmat.com>2022-11-12 15:01:19 +0100
commitd82f5da5aef06a0989430c5ea9cc60a483f1fbff (patch)
tree4f70ab3e1e1b7af5095a5ec24e025e99d0207d5b /src/lib.rs
parentc584b7177060dd32e77872ad161fb1ed181e4d4a (diff)
feat: add project & repeat_char macroHEADmaster
Diffstat (limited to 'src/lib.rs')
-rw-r--r--src/lib.rs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/lib.rs b/src/lib.rs
new file mode 100644
index 0000000..5649972
--- /dev/null
+++ b/src/lib.rs
@@ -0,0 +1,62 @@
+#![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::<String>();
+
+ quote! {
+ #repeated
+ }
+ .into()
+}
+
+struct RepeatCharMacroArgs
+{
+ character: LitChar,
+ count: usize,
+}
+
+impl Parse for RepeatCharMacroArgs
+{
+ fn parse(input: syn::parse::ParseStream) -> syn::Result<Self>
+ {
+ let character = input.parse::<LitChar>()?;
+
+ input.parse::<Token![,]>()?;
+
+ let count = input.parse::<LitInt>()?.base10_parse::<usize>()?;
+
+ Ok(Self { character, count })
+ }
+}