我在用
schemars
在我的代码库中广泛存在,我正在用以下(简化)类型填充一个相当大的向量:
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct CommandDescription {
/// The name for the command/action as it would appear in the JSON.
pub cmd: String,
/// Parameters the command takes
pub parameters: Option<RootSchema>,
}
结构的完整定义有更多的字段,它们是以下字段的组合
String
和
RootSchema
类型。
在我的代码中,我有数百种这样的定义:
CommandDescription {
cmd: String::from(api_schema::about::ABOUT_CMD),
parameters: Some(schema_for!(api_schema::about::AboutCmd)),
},
我想创建一个宏,但我很难
schema_for!()
宏。
我的宏观定义是:
macro_rules! command_description {
($api_path:path, $prefix:ident, $type_name_cmd:ident) => {
CommandDescription {
cmd: String::from(concat!(
stringify!($api_path),
"::",
stringify!($prefix),
"_CMD"
)),
parameters: Some(schema_for!($api_path::$type_name_cmd)),
}
};
}
我援引:
command_description!(
api_schema::about,
ABOUT,
AboutCmd
)
但编译器建议我封装
$api_path
在
schema_for!()
宏放在尖括号中,但它是一个路径,而不是一个类型:我知道这一点,因为如果我添加
<$api_path>
我听说这不是一种类型。
error: missing angle brackets in associated item path
--> src/bin/rudi-service.rs:674:42
|
674 | parameters: Some(schema_for!($api_path::$type_name_cmd)),
| ^^^^^^^^^
= note: this error originates in the macro `command_description` (in Nightly builds, run with -Z macro-backtrace for more info)
help: types that don't start with an identifier need to be surrounded with angle brackets in qualified paths
|
674 | parameters: Some(schema_for!(<$api_path>::$type_name_cmd)),
| + +
我是否使用了错误的片段说明符
$api_path
参数?