为 Rust 宏创建代理函数

Creating a proxy function for a Rust macro

我正在按照此变通方法获取信息!请登录!等进行测试:

这就是我所做的。我创建了要在我们测试的情况下导入的代理函数,如下所示:

#[cfg(not(test))] 
use log::{info, error}; // Use log crate when building application
#[cfg(test)]
use crate::{info, error}; // Workaround to use prinltn! for logs.

这里是:

#[macro_export]
macro_rules! info {
    (target: $target:expr, $($arg:tt)*) => { println!("target: {}, info: {}", $target, $($arg),*) };
    ($($arg:tt)*) => { println!("info: {}", $($arg),*) };
}

#[macro_export]
macro_rules! error {
    (target: $target:expr, $($arg:tt)*) => { printn!("target: {}, info: {}", $target, $($arg),*) };
    ($($arg:tt)*) => { println!("error: {}", $($arg),*) };
}

然后我这样调用:

       error!("Unauthorized message has authentication header but WwwAuthenticate fails to parse. RTSP Message: {:?}", message);

但我得到:

error: expected expression, found `,`
   --> src/rtsp_machine.rs:440:96
    |
440 |                     "Unauthorized message has no AuthenticationInfo header. RTSP Message: {:?}",
    |                                                                                                ^ expected expression

宏参数中的重复有问题,但我不知道具体是什么

您没有指定参数用逗号分隔的可能性,因此 "expected expression, found ," 错误。

您还必须包含 format!,因为生成的 println! 的第一个参数不是格式链。

然后,还将 $(,)? 部分的最后一个逗号设为可选,我可以将您的宏更改为:

#[macro_export]
macro_rules! error {
    (target: $target:expr $(,$arg:tt)*) => { println!("target: {}, info: {}", $target, format!($($arg,)*)) };
    ($($arg:tt),*$(,)?) => { println!("error: {}", format!($($arg,)*)) };
}

示例:

fn main() {
    error!(target: "my target" , " thing={:?}", "test");
    error!("bla={} and bit={}", 54, 7);
}

会打印

target: my target, info:  thing="test"
error: bla=54 and bit=7