Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a59e876d authored by Martin Geisler's avatar Martin Geisler Committed by Cherrypicker Worker
Browse files

pdl: Convert enum variants to UpperCamelCase

The legacy bluetooth_packetgen compiler used this convention. This is
in preparation for integration into rootcanel.

Tag: #feature
Bug: 228306436
Test: atest pdl_tests pdl_rust_generator_tests_{le,be}
(cherry picked from https://android-review.googlesource.com/q/commit:0504585bc230f8ae1fe5b9e95fa48532da662338)
Merged-In: Ia531f3d90859407a8b10ce55493d244defe4de1e
Change-Id: Ia531f3d90859407a8b10ce55493d244defe4de1e
parent e2fc024b
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@ rust_defaults {
    rustlibs: [
        "libclap",
        "libcodespan_reporting",
        "libheck",
        "libpest",
        "libproc_macro2",
        "libquote",
+1 −0
Original line number Diff line number Diff line
@@ -11,6 +11,7 @@ default = ["serde"]

[dependencies]
codespan-reporting = "0.11.1"
heck = "0.4.0"
pest = "2.4.0"
pest_derive = "2.4.0"
proc-macro2 = "1.0.46"
+7 −5
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@
#![allow(clippy::format_push_string)]

use crate::{ast, lint};
use heck::ToUpperCamelCase;
use quote::{format_ident, quote};
use std::path::Path;

@@ -246,7 +247,7 @@ fn generate_packet_decl(
                                }
                                _ => unreachable!("Invalid constraint: {constraint:?}"),
                            };
                            let tag_id = format_ident!("{tag_id}");
                            let tag_id = format_ident!("{}", tag_id.to_upper_camel_case());
                            quote!(#type_id::#tag_id)
                        }
                        _ => unreachable!("Invalid constraint: {constraint:?}"),
@@ -506,7 +507,8 @@ fn generate_packet_decl(

fn generate_enum_decl(id: &str, tags: &[ast::Tag]) -> proc_macro2::TokenStream {
    let name = format_ident!("{id}");
    let variants = tags.iter().map(|t| format_ident!("{}", t.id)).collect::<Vec<_>>();
    let variants =
        tags.iter().map(|t| format_ident!("{}", t.id.to_upper_camel_case())).collect::<Vec<_>>();
    let values = tags
        .iter()
        .map(|t| syn::parse_str::<syn::LitInt>(&format!("{:#x}", t.value)).unwrap())
@@ -742,15 +744,15 @@ mod tests {

    test_pdl!(
        packet_decl_8bit_enum_array,
        "enum Foo :  8 { A = 1, B = 2 } packet Bar { x: Foo[3] }"
        "enum Foo :  8 { FOO_BAR = 1, BAZ = 2 } packet Bar { x: Foo[3] }"
    );
    test_pdl!(
        packet_decl_24bit_enum_array,
        "enum Foo : 24 { A = 1, B = 2 } packet Bar { x: Foo[5] }"
        "enum Foo : 24 { FOO_BAR = 1, BAZ = 2 } packet Bar { x: Foo[5] }"
    );
    test_pdl!(
        packet_decl_64bit_enum_array,
        "enum Foo : 64 { A = 1, B = 2 } packet Bar { x: Foo[7] }"
        "enum Foo : 64 { FOO_BAR = 1, BAZ = 2 } packet Bar { x: Foo[7] }"
    );

    test_pdl!(
+3 −2
Original line number Diff line number Diff line
use crate::backends::rust::{mask_bits, types};
use crate::parser::ast as parser_ast;
use crate::{ast, lint};
use heck::ToUpperCamelCase;
use quote::{format_ident, quote};
use std::collections::{BTreeSet, HashMap};

@@ -128,7 +129,7 @@ impl<'a> FieldParser<'a> {
                }
                ast::FieldDesc::FixedEnum { enum_id, tag_id, .. } => {
                    let enum_id = format_ident!("{enum_id}");
                    let tag_id = format_ident!("{tag_id}");
                    let tag_id = format_ident!("{}", tag_id.to_upper_camel_case());
                    quote! {
                        if #v != #enum_id::#tag_id as #value_type {
                            return Err(Error::InvalidFixedValue {
@@ -543,7 +544,7 @@ impl<'a> FieldParser<'a> {
                                    }
                                    _ => unreachable!("Invalid constraint: {constraint:?}"),
                                };
                                let tag_id = format_ident!("{tag_id}");
                                let tag_id = format_ident!("{}", tag_id.to_upper_camel_case());
                                quote!(#type_id::#tag_id)
                            }
                            _ => unreachable!("Invalid constraint: {constraint:?}"),
+2 −1
Original line number Diff line number Diff line
use crate::backends::rust::{mask_bits, types};
use crate::parser::ast as parser_ast;
use crate::{ast, lint};
use heck::ToUpperCamelCase;
use quote::{format_ident, quote};

/// A single bit-field value.
@@ -76,7 +77,7 @@ impl<'a> FieldSerializer<'a> {
            ast::FieldDesc::FixedEnum { enum_id, tag_id, .. } => {
                let field_type = types::Integer::new(width);
                let enum_id = format_ident!("{enum_id}");
                let tag_id = format_ident!("{tag_id}");
                let tag_id = format_ident!("{}", tag_id.to_upper_camel_case());
                self.chunk.push(BitField { value: quote!(#enum_id::#tag_id), field_type, shift });
            }
            ast::FieldDesc::FixedScalar { value, .. } => {
Loading