| 1 | use std::str::FromStr; |
| 2 | |
| 3 | use syn::Token; |
| 4 | use syn::parse::{Parse, ParseStream}; |
| 5 | |
| 6 | use crate::create_syn_error; |
| 7 | use crate::parsing::types::{get_bits_from_type, is_supported_bitfield_type}; |
| 8 | |
| 9 | /// Represents the `#[bitfield]` attribute. |
| 10 | #[derive(Clone)] |
| 11 | pub(crate) struct BitfieldAttribute { |
| 12 | /// The integer type of the bitfield. |
| 13 | pub(crate) ty: syn::Type, |
| 14 | /// The number of bits of the bitfield. |
| 15 | pub(crate) bits: u8, |
| 16 | /// The order of the bits in the bitfield. |
| 17 | pub(crate) bit_order: BitOrder, |
| 18 | /// The endianness of the [`from_bits`] input. |
| 19 | pub(crate) from_endian: Endian, |
| 20 | /// The endianness of the [`into_bits`] output. |
| 21 | pub(crate) into_endian: Endian, |
| 22 | /// Whether to generate the `new` or `new_without_defaults` function. |
| 23 | pub(crate) generate_new_func: bool, |
| 24 | /// Whether to generate the `into_bits` function. |
| 25 | pub(crate) generate_into_bits_func: bool, |
| 26 | /// Whether to generate the `from_bits` function. |
| 27 | pub(crate) generate_from_bits_func: bool, |
| 28 | /// Whether to generate the from trait functions. |
| 29 | pub(crate) generate_from_trait_funcs: bool, |
| 30 | /// Whether to generate the `default` implementation. |
| 31 | pub(crate) generate_default_impl: bool, |
| 32 | /// Whether to generate the builder. |
| 33 | pub(crate) generate_builder: bool, |
| 34 | /// Whether to generate the `debug_impl` function. |
| 35 | pub(crate) generate_debug_impl: bool, |
| 36 | /// Whether to generate the bit operations. |
| 37 | pub(crate) generate_bit_ops: bool, |
| 38 | /// Whether to generate the set bits implementation. |
| 39 | pub(crate) generate_set_bits_impl: bool, |
| 40 | /// Whether to generate the clear bits implementation. |
| 41 | pub(crate) generate_clear_bits_impl: bool, |
| 42 | /// Whether to generate the `to_builder` function. |
| 43 | pub(crate) generate_to_builder: bool, |
| 44 | /// Whether to generate the `neg` function. |
| 45 | pub(crate) generate_neg_func: bool, |
| 46 | } |
| 47 | |
| 48 | /// The order of the bits in the bitfield. |
| 49 | /// |
| 50 | /// If the order is `Lsb`, the top most field will be the least significant bit |
| 51 | /// and the bottom most field is the most significant bit. If the order is |
| 52 | /// `Msb`, the top most field will be the most significant bit and the bottom |
| 53 | /// most field is the least significant bit. |
| 54 | #[derive(Copy, Clone, PartialEq, Eq)] |
| 55 | pub(crate) enum BitOrder { |
| 56 | Lsb, |
| 57 | Msb, |
| 58 | } |
| 59 | |
| 60 | impl FromStr for BitOrder { |
| 61 | type Err = (); |
| 62 | |
| 63 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 64 | match s.to_ascii_lowercase().as_str() { |
| 65 | "lsb" => Ok(BitOrder::Lsb), |
| 66 | "msb" => Ok(BitOrder::Msb), |
| 67 | _ => Err(()), |
| 68 | } |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// The endianness of the bytes. |
| 73 | #[derive(Clone, PartialEq)] |
| 74 | pub(crate) enum Endian { |
| 75 | Little, |
| 76 | Big, |
| 77 | } |
| 78 | |
| 79 | impl FromStr for Endian { |
| 80 | type Err = (); |
| 81 | |
| 82 | fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 83 | match s.to_ascii_lowercase().as_str() { |
| 84 | "little" => Ok(Endian::Little), |
| 85 | "big" => Ok(Endian::Big), |
| 86 | _ => Err(()), |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | impl Parse for BitfieldAttribute { |
| 92 | fn parse(input: ParseStream) -> syn::Result<Self> { |
| 93 | // Get the type of the bitfield. |
| 94 | let ty = match input.parse() { |
| 95 | Ok(ty) => ty, |
| 96 | Err(err) => { |
| 97 | return Err(create_syn_error( |
| 98 | input.span(), |
| 99 | format!( |
| 100 | "Failed to parse the 'bitfield' attribute field type: {}. The 'bitfield' attribute must have an unsigned integer type as its first argument.", |
| 101 | err |
| 102 | ), |
| 103 | )); |
| 104 | } |
| 105 | }; |
| 106 | |
| 107 | if !is_supported_bitfield_type(&ty) { |
| 108 | return Err(create_syn_error( |
| 109 | input.span(), |
| 110 | "The 'bitfield' attribute must have an unsigned integer type as its first argument.", |
| 111 | )); |
| 112 | } |
| 113 | |
| 114 | let bits = get_bits_from_type(&ty)?; |
| 115 | |
| 116 | // Move to the next argument. |
| 117 | if !input.is_empty() { |
| 118 | <Token![,]>::parse(input)?; |
| 119 | } |
| 120 | |
| 121 | let mut bit_order = BitOrder::Lsb; |
| 122 | let mut from_endian = Endian::Big; |
| 123 | let mut into_endian = Endian::Big; |
| 124 | let mut generate_new_func = true; |
| 125 | let mut generate_into_bits_func = true; |
| 126 | let mut generate_from_bits_func = true; |
| 127 | let mut generate_from_trait_funcs = true; |
| 128 | let mut generate_default_impl = true; |
| 129 | let mut generate_builder = true; |
| 130 | let mut generate_debug_impl = true; |
| 131 | let mut generate_bit_ops = false; |
| 132 | let mut generate_set_bits_impl = true; |
| 133 | let mut generate_clear_bits_impl = true; |
| 134 | let mut generate_to_builder = false; |
| 135 | let mut generate_neg_func = false; |
| 136 | |
| 137 | // Parse the remaining arguments which is in the form of `[key] = [val]`. |
| 138 | while !input.is_empty() { |
| 139 | // Parse the argument key. |
| 140 | let arg_ident = syn::Ident::parse(input)?; |
| 141 | |
| 142 | // Move to the key part of the argument. |
| 143 | <Token![=]>::parse(input)?; |
| 144 | |
| 145 | match arg_ident.to_string().as_str() { |
| 146 | "order" => { |
| 147 | let order_str = match input.parse::<syn::Ident>() { |
| 148 | Ok(order) => order.to_string(), |
| 149 | Err(err) => { |
| 150 | return Err(create_syn_error( |
| 151 | input.span(), |
| 152 | format!("Failed to parse the 'order' arg '{}'.", err), |
| 153 | )); |
| 154 | } |
| 155 | }; |
| 156 | bit_order = match BitOrder::from_str(&order_str) { |
| 157 | Ok(order) => order, |
| 158 | Err(_) => { |
| 159 | return Err(create_syn_error( |
| 160 | input.span(), |
| 161 | format!( |
| 162 | "Invalid order: '{}', must be either 'lsb' or 'msb'.", |
| 163 | order_str |
| 164 | ), |
| 165 | )); |
| 166 | } |
| 167 | }; |
| 168 | } |
| 169 | // Doesn't apply to user passed functions. |
| 170 | "from_endian" => { |
| 171 | let from_endian_str = match input.parse::<syn::Ident>() { |
| 172 | Ok(from_endian) => from_endian.to_string(), |
| 173 | Err(err) => { |
| 174 | return Err(create_syn_error( |
| 175 | input.span(), |
| 176 | format!( |
| 177 | "Failed to parse the 'from_endian' arg '{}', must be either 'little' or 'big'.", |
| 178 | err |
| 179 | ), |
| 180 | )); |
| 181 | } |
| 182 | }; |
| 183 | from_endian = match Endian::from_str(&from_endian_str) { |
| 184 | Ok(endian) => endian, |
| 185 | Err(_) => { |
| 186 | return Err(create_syn_error( |
| 187 | input.span(), |
| 188 | format!( |
| 189 | "Invalid endian: '{}', must be either 'little' or 'big'.", |
| 190 | from_endian_str |
| 191 | ), |
| 192 | )); |
| 193 | } |
| 194 | }; |
| 195 | } |
| 196 | // Doesn't apply to user passed functions. |
| 197 | "into_endian" => { |
| 198 | let into_endian_str = match input.parse::<syn::Ident>() { |
| 199 | Ok(into_endian) => into_endian.to_string(), |
| 200 | Err(err) => { |
| 201 | return Err(create_syn_error( |
| 202 | input.span(), |
| 203 | format!( |
| 204 | "Failed to parse the 'into_endian' arg '{}', must be either 'little' or 'big'.", |
| 205 | err |
| 206 | ), |
| 207 | )); |
| 208 | } |
| 209 | }; |
| 210 | into_endian = match Endian::from_str(&into_endian_str) { |
| 211 | Ok(endian) => endian, |
| 212 | Err(_) => { |
| 213 | return Err(create_syn_error( |
| 214 | input.span(), |
| 215 | format!( |
| 216 | "Invalid endian: '{}', must be either 'little' or 'big'.", |
| 217 | into_endian_str |
| 218 | ), |
| 219 | )); |
| 220 | } |
| 221 | }; |
| 222 | } |
| 223 | "new" => { |
| 224 | generate_new_func = match input.parse::<syn::LitBool>() { |
| 225 | Ok(val) => val.value, |
| 226 | Err(err) => { |
| 227 | return Err(create_syn_error( |
| 228 | input.span(), |
| 229 | format!( |
| 230 | "Failed to parse the 'new' arg '{}', must be either 'true' or 'false'.", |
| 231 | err |
| 232 | ), |
| 233 | )); |
| 234 | } |
| 235 | }; |
| 236 | } |
| 237 | "into_bits" => { |
| 238 | generate_into_bits_func = match input.parse::<syn::LitBool>() { |
| 239 | Ok(val) => val.value, |
| 240 | Err(err) => { |
| 241 | return Err(create_syn_error( |
| 242 | input.span(), |
| 243 | format!( |
| 244 | "Failed to parse the 'into_bits' arg '{}', must be either 'true' or 'false'.", |
| 245 | err |
| 246 | ), |
| 247 | )); |
| 248 | } |
| 249 | }; |
| 250 | } |
| 251 | "from_bits" => { |
| 252 | generate_from_bits_func = match input.parse::<syn::LitBool>() { |
| 253 | Ok(val) => val.value, |
| 254 | Err(err) => { |
| 255 | return Err(create_syn_error( |
| 256 | input.span(), |
| 257 | format!( |
| 258 | "Failed to parse the 'from_bits' arg '{}', must be either 'true' or 'false'.", |
| 259 | err |
| 260 | ), |
| 261 | )); |
| 262 | } |
| 263 | }; |
| 264 | } |
| 265 | "from" => { |
| 266 | generate_from_trait_funcs = match input.parse::<syn::LitBool>() { |
| 267 | Ok(val) => val.value, |
| 268 | Err(err) => { |
| 269 | return Err(create_syn_error( |
| 270 | input.span(), |
| 271 | format!( |
| 272 | "Failed to parse the 'from_type' arg '{}', must be either 'true' or 'false'.", |
| 273 | err |
| 274 | ), |
| 275 | )); |
| 276 | } |
| 277 | } |
| 278 | } |
| 279 | "default" => { |
| 280 | generate_default_impl = match input.parse::<syn::LitBool>() { |
| 281 | Ok(val) => val.value, |
| 282 | Err(err) => { |
| 283 | return Err(create_syn_error( |
| 284 | input.span(), |
| 285 | format!( |
| 286 | "Failed to parse the 'default' arg '{}', must be either 'true' or 'false'.", |
| 287 | err |
| 288 | ), |
| 289 | )); |
| 290 | } |
| 291 | }; |
| 292 | } |
| 293 | "builder" => { |
| 294 | generate_builder = match input.parse::<syn::LitBool>() { |
| 295 | Ok(val) => val.value, |
| 296 | Err(err) => { |
| 297 | return Err(create_syn_error( |
| 298 | input.span(), |
| 299 | format!( |
| 300 | "Failed to parse the 'builder' arg '{}', must be either 'true' or 'false'.", |
| 301 | err |
| 302 | ), |
| 303 | )); |
| 304 | } |
| 305 | } |
| 306 | } |
| 307 | "to_builder" => { |
| 308 | generate_to_builder = match input.parse::<syn::LitBool>() { |
| 309 | Ok(val) => val.value, |
| 310 | Err(err) => { |
| 311 | return Err(create_syn_error( |
| 312 | input.span(), |
| 313 | format!( |
| 314 | "Failed to parse the 'builder' arg '{}', must be either 'true' or 'false'.", |
| 315 | err |
| 316 | ), |
| 317 | )); |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | "debug" => { |
| 322 | generate_debug_impl = match input.parse::<syn::LitBool>() { |
| 323 | Ok(val) => val.value, |
| 324 | Err(err) => { |
| 325 | return Err(create_syn_error( |
| 326 | input.span(), |
| 327 | format!( |
| 328 | "Failed to parse the 'debug' arg '{}', must be either 'true', or 'false'.", |
| 329 | err |
| 330 | ), |
| 331 | )); |
| 332 | } |
| 333 | }; |
| 334 | } |
| 335 | "bit_ops" => { |
| 336 | generate_bit_ops = match input.parse::<syn::LitBool>() { |
| 337 | Ok(val) => val.value, |
| 338 | Err(err) => { |
| 339 | return Err(create_syn_error( |
| 340 | input.span(), |
| 341 | format!( |
| 342 | "Failed to parse the 'big_ops' arg '{}', must be either 'true', or 'false'.", |
| 343 | err |
| 344 | ), |
| 345 | )); |
| 346 | } |
| 347 | }; |
| 348 | } |
| 349 | "set_bits" => { |
| 350 | generate_set_bits_impl = match input.parse::<syn::LitBool>() { |
| 351 | Ok(val) => val.value, |
| 352 | Err(err) => { |
| 353 | return Err(create_syn_error( |
| 354 | input.span(), |
| 355 | format!( |
| 356 | "Failed to parse the 'set_bits' arg '{}', must be either 'true', or 'false'.", |
| 357 | err |
| 358 | ), |
| 359 | )); |
| 360 | } |
| 361 | }; |
| 362 | } |
| 363 | "clear_bits" => { |
| 364 | generate_clear_bits_impl = match input.parse::<syn::LitBool>() { |
| 365 | Ok(val) => val.value, |
| 366 | Err(err) => { |
| 367 | return Err(create_syn_error( |
| 368 | input.span(), |
| 369 | format!( |
| 370 | "Failed to parse the 'set_bits' arg '{}', must be either 'true', or 'false'.", |
| 371 | err |
| 372 | ), |
| 373 | )); |
| 374 | } |
| 375 | }; |
| 376 | } |
| 377 | "neg" => { |
| 378 | generate_neg_func = match input.parse::<syn::LitBool>() { |
| 379 | Ok(val) => val.value, |
| 380 | Err(err) => { |
| 381 | return Err(create_syn_error( |
| 382 | input.span(), |
| 383 | format!( |
| 384 | "Failed to parse the 'neg' arg '{}', must be either 'true', or 'false'.", |
| 385 | err |
| 386 | ), |
| 387 | )); |
| 388 | } |
| 389 | }; |
| 390 | } |
| 391 | _ => { |
| 392 | return Err(create_syn_error( |
| 393 | arg_ident.span(), |
| 394 | format!("Unknown 'bitfield' argument: {}", arg_ident), |
| 395 | )); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | // Move to the next argument. |
| 400 | if !input.is_empty() { |
| 401 | <Token![,]>::parse(input)?; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | Ok(Self { |
| 406 | ty, |
| 407 | bits, |
| 408 | bit_order, |
| 409 | from_endian, |
| 410 | into_endian, |
| 411 | generate_new_func, |
| 412 | generate_into_bits_func, |
| 413 | generate_from_bits_func, |
| 414 | generate_default_impl, |
| 415 | generate_debug_impl, |
| 416 | generate_builder, |
| 417 | generate_from_trait_funcs, |
| 418 | generate_bit_ops, |
| 419 | generate_set_bits_impl, |
| 420 | generate_clear_bits_impl, |
| 421 | generate_to_builder, |
| 422 | generate_neg_func, |
| 423 | }) |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | #[cfg(test)] |
| 428 | mod tests { |
| 429 | use quote::quote; |
| 430 | |
| 431 | use super::*; |
| 432 | |
| 433 | #[test] |
| 434 | fn test_parse_bitfield_no_args_returns_error() { |
| 435 | let args = quote!(); |
| 436 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 437 | assert!(args.is_err()); |
| 438 | assert!(args.err().unwrap().to_string().contains( |
| 439 | "The 'bitfield' attribute must have an unsigned integer type as its first argument" |
| 440 | )); |
| 441 | } |
| 442 | |
| 443 | #[test] |
| 444 | fn test_parse_bitfield_invalid_type_arg_returns_error() { |
| 445 | let args = quote!(eff); |
| 446 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 447 | assert!(args.is_err()); |
| 448 | assert!(args.err().unwrap().to_string().contains( |
| 449 | "The 'bitfield' attribute must have an unsigned integer type as its first argument." |
| 450 | )); |
| 451 | |
| 452 | let args = quote!(i32); |
| 453 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 454 | assert!(args.is_err()); |
| 455 | assert!(args.err().unwrap().to_string().contains( |
| 456 | "The 'bitfield' attribute must have an unsigned integer type as its first argument." |
| 457 | )); |
| 458 | } |
| 459 | |
| 460 | #[test] |
| 461 | fn test_parse_bitfield_unsigned_integer_types() { |
| 462 | let args = quote!(u8); |
| 463 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 464 | assert!(args.is_ok()); |
| 465 | assert_eq!(args.unwrap().bits, 8); |
| 466 | |
| 467 | let args = quote!(u16); |
| 468 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 469 | assert!(args.is_ok()); |
| 470 | assert_eq!(args.unwrap().bits, 16); |
| 471 | |
| 472 | let args = quote!(u32); |
| 473 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 474 | assert!(args.is_ok()); |
| 475 | assert_eq!(args.unwrap().bits, 32); |
| 476 | |
| 477 | let args = quote!(u64); |
| 478 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 479 | assert!(args.is_ok()); |
| 480 | assert_eq!(args.unwrap().bits, 64); |
| 481 | |
| 482 | let args = quote!(u128); |
| 483 | let args = syn::parse2::<BitfieldAttribute>(args); |
| 484 | assert!(args.is_ok()); |
| 485 | assert_eq!(args.unwrap().bits, 128); |
| 486 | } |
| 487 | } |