Accept list of values as a plain string in a macro



examples/macros/say-hello-many-times-macro/Cargo.toml
[package]
name = "say-hello-many-times-macro"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[lib]
proc-macro = true

[dependencies]

examples/macros/say-hello-many-times-macro/src/lib.rs
extern crate proc_macro;
use proc_macro::TokenStream;

#[proc_macro]
pub fn say_hello(input: TokenStream) -> TokenStream {
    println!("Input: {}", input);
    let value = input.to_string();
    println!("Value: {}", value);

    let values = value
        .split(',')
        .map(|val| val.trim())
        .collect::<Vec<&str>>();
    let mut code = String::new();
    for val in values {
        code += format!(r#"println!("Hello '{val}'");"#).as_str();
    }

    code.parse().unwrap()
}

examples/macros/say-hello-many-times-use/Cargo.toml
[package]
name = "say-hello-many-times-use"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
say-hello-many-times-macro = { version = "0.1.0", path = "../say-hello-many-times-macro" }

examples/macros/say-hello-many-times-use/src/main.rs
extern crate say_hello_many_times_macro;
use say_hello_many_times_macro::say_hello;

fn main() {
    say_hello!(Joe);
    println!("---");
    say_hello!(cat, dog, elephant);
}

Input: Joe
Value: Joe
Input: cat, dog, elephant
Value: cat, dog, elephant
Hello 'Joe'
---
Hello 'cat'
Hello 'dog'
Hello 'elephant'