diff --git a/pdl-compiler/src/analyzer.rs b/pdl-compiler/src/analyzer.rs index a6fa29e..e9e9b50 100644 --- a/pdl-compiler/src/analyzer.rs +++ b/pdl-compiler/src/analyzer.rs @@ -1804,10 +1804,10 @@ fn desugar_flags(file: &mut File) { let mut condition_ids: HashMap> = HashMap::new(); for field in fields.iter() { if let Some(ref cond) = field.cond { - - condition_ids.entry(cond.id.to_owned()).or_default().push( - (field.id().unwrap().to_owned(), cond.value.unwrap()) - ); + condition_ids + .entry(cond.id.to_owned()) + .or_default() + .push((field.id().unwrap().to_owned(), cond.value.unwrap())); } } // Replace condition flags in the fields. diff --git a/pdl-compiler/src/backends/rust/encoder.rs b/pdl-compiler/src/backends/rust/encoder.rs index 2f10b07..97d30b7 100644 --- a/pdl-compiler/src/backends/rust/encoder.rs +++ b/pdl-compiler/src/backends/rust/encoder.rs @@ -249,7 +249,6 @@ impl Encoder { syn::parse_str::(&format!("{}", 1 - set_value)).unwrap(); if optional_field_ids.len() >= 2 { - self.tokens.extend(quote! { let mut cond_value_is_zero = false; let mut cond_value_is_one = false; diff --git a/pdl-compiler/src/backends/rust_legacy/serializer.rs b/pdl-compiler/src/backends/rust_legacy/serializer.rs index eaf8f74..1b55081 100644 --- a/pdl-compiler/src/backends/rust_legacy/serializer.rs +++ b/pdl-compiler/src/backends/rust_legacy/serializer.rs @@ -144,7 +144,10 @@ impl<'a> FieldSerializer<'a> { match &field.desc { ast::FieldDesc::Flag { optional_field_ids, .. } => { - assert!(optional_field_ids.len() == 1, "condition flag reuse not supported by legacy generator"); + assert!( + optional_field_ids.len() == 1, + "condition flag reuse not supported by legacy generator" + ); let (optional_field_id, set_value) = &optional_field_ids[0]; let optional_field_id = optional_field_id.to_ident(); diff --git a/pdl-runtime/src/lib.rs b/pdl-runtime/src/lib.rs index c4b26d4..e1da876 100644 --- a/pdl-runtime/src/lib.rs +++ b/pdl-runtime/src/lib.rs @@ -77,7 +77,7 @@ pub enum EncodeError { element_index: usize, }, #[error("{packet}.{field} value cannot be uniquely determined")] - InconsistentConditionValue { packet: &'static str, field: &'static str}, + InconsistentConditionValue { packet: &'static str, field: &'static str }, } /// Trait implemented for all toplevel packet declarations. diff --git a/pdl-tests/tests/semantic.rs b/pdl-tests/tests/semantic.rs index c4a76b0..c8d96be 100644 --- a/pdl-tests/tests/semantic.rs +++ b/pdl-tests/tests/semantic.rs @@ -23,7 +23,7 @@ enum Enum16 : 16 { Y = 0x5678, } -packet CondTest { +packet Test { cond: 1, _reserved_ : 7, a: 8 if cond = 0, @@ -35,46 +35,38 @@ packet CondTest { mod optional_field { #[test] fn test_value_0() { - // Success - let test_value_0 = CondTest { - a: Some(255), - b: None, - }; - let mut buf = vec![]; - assert!(test_value_0.encode(&mut buf).is_ok()); + let value = Test { a: Some(255), b: None }; + let mut encoded_value = vec![]; - let decoded_cond = CondTest::decode_full(&buf).unwrap(); - assert_eq!(decoded_cond.a, test_value_0.a); - assert_eq!(decoded_cond.b, test_value_0.b); + // The optional fields provide both the same value 0. + assert!(value.encode(&mut encoded_value).is_ok()); + assert_eq!(Test::decode_full(&encoded_value), Ok(value)); } #[test] fn test_value_1() { - // Success - let test_value_1 = CondTest { - a: None, - b: Some(Enum16::X), - }; - let mut buf = vec![]; - assert!(test_value_1.encode(&mut buf).is_ok()); + let value = Test { a: None, b: Some(Enum16::X) }; + let mut encoded_value = vec![]; - let decoded_cond = CondTest::decode_full(&buf).unwrap(); - assert_eq!(decoded_cond.a, test_value_1.a); - assert_eq!(decoded_cond.b, test_value_1.b); + // The optional fields provide both the same value 0. + assert!(value.encode(&mut encoded_value).is_ok()); + assert_eq!(Test::decode_full(&encoded_value), Ok(value)); } #[test] fn test_value_inconsistent() { - let test_value_none = CondTest { - a: None, - b: None, - }; - assert!(matches!(test_value_none.encode_to_vec(), Err(EncodeError::InconsistentConditionValue { .. }))); + // The optional fields would provide the value 1 and 0 + // for the condition flag. + assert!(matches!( + Test { a: None, b: None }.encode_to_vec(), + Err(EncodeError::InconsistentConditionValue { .. }) + )); - let test_value_both = CondTest { - a: Some(255), - b: Some(Enum16::X), - }; - assert!(matches!(test_value_both.encode_to_vec(), Err(EncodeError::InconsistentConditionValue { .. }))); + // The optional fields would provide the value 0 and 1 + // for the condition flag. + assert!(matches!( + Test { a: Some(255), b: Some(Enum16::X) }.encode_to_vec(), + Err(EncodeError::InconsistentConditionValue { .. }) + )); } }