risingwave_jni_core

Macro split_by_comma

source
macro_rules! split_by_comma {
    (
        {$($input:tt)*},
        $macro:path $(,$args:tt)*
    ) => { ... };
    (
        {
            ,
            $($rest:tt)*
        },
        {
            $(
                {$($prev_split:tt)*}
            )*
        },
        {
            $($current_split:tt)*
        },
        $macro:path $(,$args:tt)*
    ) => { ... };
    (
        {},
        {
            $(
                {$($prev_split:tt)*}
            )*
        },
        {
            $($current_split:tt)+
        },
        $macro:path $(,$args:tt)*
    ) => { ... };
    (
        {
            $first:tt
            $($rest:tt)*
        },
        {
            $(
                {$($prev_split:tt)*}
            )*
        },
        {
            $($current_split:tt)*
        },
        $macro:path $(,$args:tt)*
    ) => { ... };
    (
        {},
        {
            $(
                {$($prev_split:tt)*}
            )*
        },
        {},
        $macro:path $(,$args:tt)*
    ) => { ... };
}
Expand description

A macro that splits the input by comma and calls the callback $macro with the split result. The each part of the split result is within a bracket, and the callback $macro can have its first parameter as {$({$($args:tt)+})*} to match the split result.

macro_rules! call_split_by_comma {
    ({$({$($args:tt)+})*}) => {
        [$(
           stringify! {$($args)+}
        ),*]
    };
    ($($input:tt)*) => {{
        risingwave_jni_core::split_by_comma! {
            {$($input)*},
            call_split_by_comma
        }
    }};
}

let expected_result = [
    "hello",
    "my friend",
];

assert_eq!(expected_result, {call_split_by_comma!(hello, my friend)});