aboutsummaryrefslogtreecommitdiff
path: root/macros/src/lib.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2021-09-21 16:21:20 +0100
committerAkshay <[email protected]>2021-09-21 16:21:20 +0100
commit7953d8cc9345794613ba2e7b90af9429579bc0cf (patch)
treebccaa3ba5623055c76cebd226efcca0ecd4db017 /macros/src/lib.rs
parent3eec886fe83b30636622d6ba97880bf473de8a0a (diff)
add error code and report codegen, document a bit
Diffstat (limited to 'macros/src/lib.rs')
-rw-r--r--macros/src/lib.rs29
1 files changed, 29 insertions, 0 deletions
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 {
53fn generate_meta_impl(struct_name: &Ident, meta: &LintMeta) -> TokenStream2 { 53fn generate_meta_impl(struct_name: &Ident, meta: &LintMeta) -> TokenStream2 {
54 let name_fn = generate_name_fn(meta); 54 let name_fn = generate_name_fn(meta);
55 let note_fn = generate_note_fn(meta); 55 let note_fn = generate_note_fn(meta);
56 let code_fn = generate_code_fn(meta);
57 let report_fn = generate_report_fn();
56 let match_with_fn = generate_match_with_fn(meta); 58 let match_with_fn = generate_match_with_fn(meta);
57 let match_kind = generate_match_kind(meta); 59 let match_kind = generate_match_kind(meta);
58 quote! { 60 quote! {
59 impl Metadata for #struct_name { 61 impl Metadata for #struct_name {
60 #name_fn 62 #name_fn
61 #note_fn 63 #note_fn
64 #code_fn
65 #report_fn
62 #match_with_fn 66 #match_with_fn
63 #match_kind 67 #match_kind
64 } 68 }
@@ -99,6 +103,31 @@ fn generate_note_fn(meta: &LintMeta) -> TokenStream2 {
99 panic!("Invalid value for `note`"); 103 panic!("Invalid value for `note`");
100} 104}
101 105
106fn generate_code_fn(meta: &LintMeta) -> TokenStream2 {
107 let code = meta
108 .0
109 .get(&format_ident!("code"))
110 .unwrap_or_else(|| panic!("`code` not present"));
111 if let syn::Expr::Lit(code_lit) = code {
112 if let Lit::Int(code_int) = &code_lit.lit {
113 return quote! {
114 fn code() -> u32 {
115 #code_int
116 }
117 };
118 }
119 }
120 panic!("Invalid value for `note`");
121}
122
123fn generate_report_fn() -> TokenStream2 {
124 quote! {
125 fn report() -> Report {
126 Report::new(Self::note(), Self::code())
127 }
128 }
129}
130
102fn generate_match_with_fn(meta: &LintMeta) -> TokenStream2 { 131fn generate_match_with_fn(meta: &LintMeta) -> TokenStream2 {
103 let match_with_lit = meta 132 let match_with_lit = meta
104 .0 133 .0