Skip to main content

vacro_report_macro/
lib.rs

1use proc_macro::TokenStream;
2use vacro_doc_i18n::doc_i18n;
3
4use crate::impls::{help_impl, parse_quote_impl, report_scope_impl};
5
6mod impls;
7mod utils;
8
9#[doc_i18n]
10/// @cn 作用域标记宏。
11/// @en Scope marker macro.
12///
13/// ::: @cn
14///
15/// 将此属性添加到函数上,以启用该作用域内 `parse_quote!` 调用的增强错误报告功能。
16/// 当 `parse_quote!` 解析失败时,它会打印生成的 TokenStream 并高亮错误位置,而不是仅仅 Panic。
17///
18/// :::
19///
20/// ::: @en
21///
22/// Attach this attribute to a function to enable enhanced error reporting for `parse_quote!` calls within that scope.
23/// When `parse_quote!` fails, it will print the generated TokenStream and highlight the error, instead of just panicking.
24/// :::
25#[proc_macro_attribute]
26pub fn scope(attr: TokenStream, item: TokenStream) -> TokenStream {
27    report_scope_impl(attr.into(), item.into()).into()
28}
29
30#[doc_i18n]
31/// ::: @cn
32/// 增强版 `parse_quote!`(内部使用)。
33///
34/// 该宏通常不直接由用户调用,而是通过 `#[scope]` 宏重写标准的 `syn::parse_quote!` 调用来自动使用。
35/// :::
36/// ::: @en
37/// Enhanced `parse_quote!` (Internal Use).
38///
39/// This macro is usually not called directly. It is automatically used when `#[scope]` rewrites
40/// standard `syn::parse_quote!` calls.
41/// :::
42#[proc_macro]
43pub fn parse_quote(input: TokenStream) -> TokenStream {
44    parse_quote_impl(input.into(), false).into()
45}
46
47#[doc_i18n]
48/// ::: @cn
49/// 增强版 `parse_quote_spanned!`(内部使用)。
50///
51/// 该宏通常不直接由用户调用,而是通过 `#[scope]` 宏重写标准的 `syn::parse_quote_spanned!` 调用来自动使用。
52/// :::
53/// ::: @en
54/// Enhanced `parse_quote_spanned!` (Internal Use).
55///
56/// This macro is usually not called directly. It is automatically used when `#[scope]` rewrites
57/// standard `syn::parse_quote_spanned!` calls.
58/// :::
59#[proc_macro]
60pub fn parse_quote_spanned(input: TokenStream) -> TokenStream {
61    parse_quote_impl(input.into(), true).into()
62}
63
64#[doc_i18n]
65/// @cn 创建带有增强错误报告和智能提示的包装类型。
66/// @en Creates a wrapper type with enhanced error reporting and smart hints.
67///
68/// ::: @cn
69///
70/// 此宏定义一个代理底层类型(如 `syn::Ident`)的包装结构体,允许附加自定义的错误信息和帮助文本。
71///
72/// 若启用了 `vacro-parser` 支持,还可以提供 `example` 字段。这将激活智能提示联动,使解析器在报错时显示具体的代码示例而非类型名。
73///
74/// ```rust
75/// # use vacro_report::help;
76/// help!(Arithmetic: syn::Expr {
77///     error: "expected an arithmetic expression",
78///     help: "try using explicit values or operations",
79///     example: "1 + 2" // 可选:用于 vacro-parser 智能提示
80/// });
81/// ```
82/// :::
83///
84/// ::: @en
85///
86/// Defines a wrapper struct that proxies an underlying type (like `syn::Ident`), allowing custom error messages and help text to be attached.
87///
88/// If `vacro-parser` support is enabled, the `example` field can be provided. This activates smart hint synergy, causing the parser to show specific code examples instead of type names when errors occur.
89///
90/// ```rust
91/// # use vacro_report::help;
92/// help!(Arithmetic: syn::Expr {
93///     error: "expected an arithmetic expression",
94///     help: "try using explicit values or operations",
95///     example: "1 + 2" // Optional: for vacro-parser smart hints
96/// });
97/// ```
98/// :::
99#[proc_macro]
100pub fn help(input: TokenStream) -> TokenStream {
101    help_impl(input.into()).into()
102}