From 7953d8cc9345794613ba2e7b90af9429579bc0cf Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 21 Sep 2021 20:51:20 +0530 Subject: add error code and report codegen, document a bit --- macros/src/lib.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'macros/src') diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 36a8bb5..c33fcba 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -53,12 +53,16 @@ fn generate_self_impl(struct_name: &Ident) -> TokenStream2 { fn generate_meta_impl(struct_name: &Ident, meta: &LintMeta) -> TokenStream2 { let name_fn = generate_name_fn(meta); let note_fn = generate_note_fn(meta); + let code_fn = generate_code_fn(meta); + let report_fn = generate_report_fn(); let match_with_fn = generate_match_with_fn(meta); let match_kind = generate_match_kind(meta); quote! { impl Metadata for #struct_name { #name_fn #note_fn + #code_fn + #report_fn #match_with_fn #match_kind } @@ -99,6 +103,31 @@ fn generate_note_fn(meta: &LintMeta) -> TokenStream2 { panic!("Invalid value for `note`"); } +fn generate_code_fn(meta: &LintMeta) -> TokenStream2 { + let code = meta + .0 + .get(&format_ident!("code")) + .unwrap_or_else(|| panic!("`code` not present")); + if let syn::Expr::Lit(code_lit) = code { + if let Lit::Int(code_int) = &code_lit.lit { + return quote! { + fn code() -> u32 { + #code_int + } + }; + } + } + panic!("Invalid value for `note`"); +} + +fn generate_report_fn() -> TokenStream2 { + quote! { + fn report() -> Report { + Report::new(Self::note(), Self::code()) + } + } +} + fn generate_match_with_fn(meta: &LintMeta) -> TokenStream2 { let match_with_lit = meta .0 -- cgit v1.2.3