1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
extern crate quote; #[macro_use] extern crate synstructure; use std::convert::TryFrom; use kg_diag::*; use proc_macro2::Span; decl_derive!([Detail, attributes(diag)] => detail_derive); struct DiagAttr { code: u32, severity: Severity, } fn path_eq(path: &syn::Path, s: &str) -> bool { if let Some(ident) = path.get_ident() { return ident == s; } false } fn detail_derive(mut st: synstructure::Structure) -> proc_macro2::TokenStream { let mut code_offset: u32 = 0; let mut severity = Severity::Failure; let container_attr = find_nested_attr(&st.ast().attrs, "diag"); if let Some(params) = container_attr { for p in params { match p { syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref path, lit: syn::Lit::Int(ref i), .. })) if path_eq(path, "code_offset") => { code_offset = i.base10_parse().unwrap_or_default(); } syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref path, lit: syn::Lit::Str(ref s), .. })) if path_eq(path, "severity") => match Severity::try_from(s.value().as_ref()) { Ok(s) => severity = s, Err(value) => panic!(format!( "invalid default severity \"{}\" for type {}", value, st.ast().ident )), }, syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref path, lit: syn::Lit::Char(ref c), .. })) if path_eq(path, "severity") => match Severity::try_from(c.value()) { Ok(s) => severity = s, Err(value) => panic!(format!( "invalid default severity '{}' for type {}", value, st.ast().ident )), }, _ => { panic!(format!( "invalid diag(...) attribute for type {}", st.ast().ident )); } } } } let mut attrs = Vec::with_capacity(st.variants().len()); let mut code = code_offset + 1; for ref mut v in st.variants_mut() { v.filter(|_| false); let mut a = DiagAttr { code, severity }; let vattr = find_nested_attr(v.ast().attrs, "diag"); if let Some(params) = vattr { for p in params { match p { syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref path, lit: syn::Lit::Int(ref i), .. })) if path_eq(path, "code") => { a.code = code_offset + i.base10_parse::<u32>().unwrap_or_default(); } syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref path, lit: syn::Lit::Str(ref s), .. })) if path_eq(path, "severity") => match Severity::try_from(s.value().as_ref()) { Ok(s) => a.severity = s, Err(value) => panic!(format!( "invalid severity \"{}\" for variant {}", value, v.ast().ident )), }, syn::NestedMeta::Meta(syn::Meta::NameValue(syn::MetaNameValue { ref path, lit: syn::Lit::Char(ref c), .. })) if path_eq(path, "severity") => match Severity::try_from(c.value()) { Ok(s) => a.severity = s, Err(value) => panic!(format!( "invalid severity '{}' for variant {}", value, v.ast().ident )), }, _ => { panic!(format!( "invalid diag(...) attribute for variant {}", v.ast().ident )); } } } } if a.code > code { code = a.code + 1; } else { code += 1; } attrs.push(a); } for a in attrs.iter() { for b in attrs.iter() { if a as *const _ == b as *const _ { continue; } if a.code == b.code { panic!(format!( "duplicated code {} in type {}", a.code, st.ast().ident )); } } } let mut attrs_it = attrs.iter(); let severity_body = st.each_variant(|_v| { let a = attrs_it.next().unwrap(); let severity = syn::parse_str::<syn::Path>(&format!("kg_diag::Severity::{:?}", a.severity)).unwrap(); quote! { #severity } }); let mut attrs_it = attrs.iter(); let code_body = st.each_variant(|_v| { let a = attrs_it.next().unwrap(); let code = a.code; quote! { #code } }); let p = st.gen_impl(quote! { extern crate kg_diag; gen impl kg_diag::Detail for @Self { fn severity(&self) -> Severity { match *self { #severity_body } } fn code(&self) -> u32 { match *self { #code_body } } } }); p } fn find_nested_attr(attrs: &[syn::Attribute], id: &str) -> Option<Vec<syn::NestedMeta>> { let doc_path: syn::Path = syn::Ident::new("doc", Span::call_site()).into(); let mut a = None; for attr in attrs { if attr.path != doc_path && attr.style == syn::AttrStyle::Outer { let meta = { let m = attr.parse_meta(); if let Ok(syn::Meta::List(syn::MetaList { path, nested, .. })) = m { if path_eq(&path, id) { Some(nested.into_iter().collect()) } else { None } } else { None } }; if a.is_some() && meta.is_some() { panic!(format!("multiple {}(...) attributes found", id)) } else { a = meta; } } } a }