Skip to content

Commit

Permalink
Apply cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
hchataing committed Nov 15, 2024
1 parent b1630cb commit c61af7b
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 38 deletions.
8 changes: 4 additions & 4 deletions pdl-compiler/src/analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1804,10 +1804,10 @@ fn desugar_flags(file: &mut File) {
let mut condition_ids: HashMap<String, Vec<(String, usize)>> = 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.
Expand Down
1 change: 0 additions & 1 deletion pdl-compiler/src/backends/rust/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,6 @@ impl Encoder {
syn::parse_str::<syn::LitInt>(&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;
Expand Down
5 changes: 4 additions & 1 deletion pdl-compiler/src/backends/rust_legacy/serializer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion pdl-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
54 changes: 23 additions & 31 deletions pdl-tests/tests/semantic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ enum Enum16 : 16 {
Y = 0x5678,
}
packet CondTest {
packet Test {
cond: 1,
_reserved_ : 7,
a: 8 if cond = 0,
Expand All @@ -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 { .. })
));
}
}

0 comments on commit c61af7b

Please sign in to comment.