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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
use proc_macro::TokenStream; fn base_values(input: TokenStream) -> (String, String, String, String, String) { let input_string = input.to_string(); let mut strings = input_string.split(','); let value_ident = strings .next() .expect("Missing first positional argument: value identifier") .trim() .replace("\"", ""); if value_ident.is_empty() { panic!("Found empty value identifier."); } let name = value_ident.replace('_', " "); let mut name_chars = name.chars(); let headline = name_chars.next().unwrap().to_uppercase().chain(name_chars).collect::<String>(); let atom_ident = format!("ident::{}", value_ident.to_uppercase()); let atom_ident_string = strings .next() .expect("Missing second positional argument: atom ident string") .trim() .replace("\"", ""); if atom_ident_string.is_empty() { panic!("Found empty atom identifier string."); } if let Some(arg) = strings.next().map(|s| s.trim()) { if !arg.is_empty() { panic!("Found unexpected third positional argument: {}.", arg); } } (value_ident, name, headline, atom_ident, atom_ident_string) } #[proc_macro] pub fn single_string_value_accessor(input: TokenStream) -> TokenStream { let (value_ident, name, headline, atom_ident, atom_ident_string) = base_values(input); format!( " /// ### {hl} impl Tag {{ /// Returns the {n} (`{ais}`). pub fn {vi}(&self) -> Option<&str> {{ self.strings_of(&{ai}).next() }} /// Removes and returns the {n} (`{ais}`). pub fn take_{vi}(&mut self) -> Option<String> {{ self.take_strings_of(&{ai}).next() }} /// Sets the {n} (`{ais}`). pub fn set_{vi}(&mut self, {vi}: impl Into<String>) {{ self.set_data({ai}, Data::Utf8({vi}.into())); }} /// Removes the {n} (`{ais}`). pub fn remove_{vi}(&mut self) {{ self.remove_data_of(&{ai}); }} /// Returns the {n} formatted in an easily readable way. fn format_{vi}(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {{ match self.{vi}() {{ Some(s) => writeln!(f, \"{n}: {{}}\", s), None => Ok(()), }} }} }} ", hl = headline, n = name, ais = atom_ident_string, vi = value_ident, ai = atom_ident, ) .parse() .expect("Error parsing accessor impl block:") } #[proc_macro] pub fn multiple_string_values_accessor(input: TokenStream) -> TokenStream { let (value_ident, name, headline, atom_ident, atom_ident_string) = base_values(input); let mut value_ident_plural = value_ident.clone(); if value_ident_plural.ends_with('y') { value_ident_plural.pop(); value_ident_plural.push_str("ies"); } else { value_ident_plural.push('s'); }; let name_plural = value_ident_plural.replace('_', " "); format!( " /// ### {hl} impl Tag {{ /// Returns all {np} (`{ais}`). pub fn {vip}(&self) -> impl Iterator<Item=&str> {{ self.strings_of(&{ai}) }} /// Returns the first {n} (`{ais}`). pub fn {vi}(&self) -> Option<&str> {{ self.strings_of(&{ai}).next() }} /// Removes and returns all {np} (`{ais}`). pub fn take_{vip}(&mut self) -> impl Iterator<Item=String> + '_ {{ self.take_strings_of(&{ai}) }} /// Removes all and returns the first {n} (`{ais}`). pub fn take_{vi}(&mut self) -> Option<String> {{ self.take_strings_of(&{ai}).next() }} /// Sets all {np} (`{ais}`). This will remove all other {np}. pub fn set_{vip}(&mut self, {vip}: impl IntoIterator<Item = String>) {{ let data = {vip}.into_iter().map(|v| Data::Utf8(v)); self.set_all_data({ai}, data); }} /// Sets the {n} (`{ais}`). This will remove all other {np}. pub fn set_{vi}(&mut self, {vi}: impl Into<String>) {{ self.set_data({ai}, Data::Utf8({vi}.into())); }} /// Adds all {np} (`{ais}`). pub fn add_{vip}(&mut self, {vip}: impl IntoIterator<Item = String>) {{ let data = {vip}.into_iter().map(|v| Data::Utf8(v)); self.add_all_data({ai}, data); }} /// Adds an {n} (`{ais}`). pub fn add_{vi}(&mut self, {vi}: impl Into<String>) {{ self.add_data({ai}, Data::Utf8({vi}.into())); }} /// Removes all {np} (`{ais}`). pub fn remove_{vip}(&mut self) {{ self.remove_data_of(&{ai}); }} /// Returns all {np} formatted in an easily readable way. fn format_{vip}(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {{ if self.{vip}().count() > 1 {{ writeln!(f, \"{np}:\")?; for s in self.{vip}() {{ writeln!(f, \" {{}}\", s)?; }} }} else if let Some(s) = self.{vi}() {{ writeln!(f, \"{n}: {{}}\", s)?; }} Ok(()) }} }} ", hl = headline, n = name, np = name_plural, ais = atom_ident_string, vi = value_ident, vip = value_ident_plural, ai = atom_ident, ) .parse() .expect("Error parsing accessor impl block:") } #[proc_macro] pub fn flag_value_accessor(input: TokenStream) -> TokenStream { let (value_ident, name, headline, atom_ident, atom_ident_string) = base_values(input); format!( " /// ### {hl} impl Tag {{ /// Returns the {n} flag (`{ais}`). pub fn {vi}(&self) -> bool {{ let vec = match self.bytes_of(&{ai}).next() {{ Some(v) => v, None => return false, }}; vec.get(0).map(|&v| v == 1).unwrap_or(false) }} /// Sets the {n} flag to true (`{ais}`). pub fn set_{vi}(&mut self) {{ self.set_data({ai}, Data::BeSigned(vec![1u8])); }} /// Removes the {n} flag (`{ais}`). pub fn remove_{vi}(&mut self) {{ self.remove_data_of(&{ai}) }} }} ", hl = headline, n = name, ais = atom_ident_string, vi = value_ident, ai = atom_ident, ) .parse() .expect("Error parsing accessor impl block:") } #[proc_macro] pub fn u16_value_accessor(input: TokenStream) -> TokenStream { let (value_ident, name, headline, atom_ident, atom_ident_string) = base_values(input); format!( " /// ### {hl} impl Tag {{ /// Returns the {n} (`{ais}`) pub fn {vi}(&self) -> Option<u16> {{ let vec = self.bytes_of(&{ai}).next()?; be_int!(vec, 0, u16) }} /// Sets the {n} (`{ais}`) pub fn set_{vi}(&mut self, {vi}: u16) {{ let vec: Vec<u8> = {vi}.to_be_bytes().to_vec(); self.set_data({ai}, Data::BeSigned(vec)); }} /// Removes the {n} (`{ais}`). pub fn remove_{vi}(&mut self) {{ self.remove_data_of(&{ai}); }} }} ", hl = headline, n = name, ais = atom_ident_string, vi = value_ident, ai = atom_ident, ) .parse() .expect("Error parsing accessor impl block:") } #[proc_macro] pub fn u32_value_accessor(input: TokenStream) -> TokenStream { let (value_ident, name, headline, atom_ident, atom_ident_string) = base_values(input); format!( " /// ### {hl} impl Tag {{ /// Returns the {n} (`{ais}`) pub fn {vi}(&self) -> Option<u32> {{ let vec = self.bytes_of(&{ai}).next()?; be_int!(vec, 0, u32) }} /// Sets the {n} (`{ais}`) pub fn set_{vi}(&mut self, {vi}: u32) {{ let vec: Vec<u8> = {vi}.to_be_bytes().to_vec(); self.set_data({ai}, Data::BeSigned(vec)); }} /// Removes the {n} (`{ais}`). pub fn remove_{vi}(&mut self) {{ self.remove_data_of(&{ai}); }} }} ", hl = headline, n = name, ais = atom_ident_string, vi = value_ident, ai = atom_ident, ) .parse() .expect("Error parsing accessor impl block:") }