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

Commit 001619d1 authored by Martin Geisler's avatar Martin Geisler
Browse files

pdl: Parse structs like packets

The auto-generated “Packet” suffix now seems wrong, so I removed it.
This means that both “packet Foo” and “struct Foo” will generate the
following Rust types:

    pub struct Foo
    pub struct FooBuilder
    struct FooData

One potential problem with this: if someone has a “packet Packet”,
this now collides with “trait Packet” from our preamble. We might need
to correct for this when we integrate the generated code with clients.

Test: atest pdl_tests pdl_rust_generator_tests_{le,be}
Change-Id: I19354010e46ef1b5738fee9f7fb13ff7778d1b03
parent d69fc851
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -73,6 +73,8 @@ rust_test_host {
        "tests/generated/packet_decl_simple_scalars_big_endian.rs",
        "tests/generated/packet_decl_simple_scalars_little_endian.rs",
        "tests/generated/preamble.rs",
        "tests/generated/struct_decl_complex_scalars_big_endian.rs",
        "tests/generated/struct_decl_complex_scalars_little_endian.rs",
    ],
}

+19 −3
Original line number Diff line number Diff line
@@ -40,7 +40,8 @@ pub fn mask_bits(n: usize) -> syn::LitInt {
    syn::parse_str::<syn::LitInt>(&format!("{:#x}{suffix}", (1u64 << n) - 1)).unwrap()
}

/// Generate code for an `ast::Decl::Packet` enum value.
/// Generate code for `ast::Decl::Packet` and `ast::Decl::Struct`
/// values.
fn generate_packet_decl(
    scope: &lint::Scope<'_>,
    //  File:
@@ -66,8 +67,8 @@ fn generate_packet_decl(
    field_parser.done();

    let id_lower = format_ident!("{}", id.to_lowercase());
    let id_packet = format_ident!("{id}");
    let id_data = format_ident!("{id}Data");
    let id_packet = format_ident!("{id}Packet");
    let id_builder = format_ident!("{id}Builder");

    let field_names =
@@ -238,7 +239,8 @@ fn generate_enum_decl(id: &str, tags: &[ast::Tag]) -> proc_macro2::TokenStream {

fn generate_decl(scope: &lint::Scope<'_>, file: &ast::File, decl: &ast::Decl) -> String {
    match decl {
        ast::Decl::Packet { id, constraints, fields, parent_id, .. } => generate_packet_decl(
        ast::Decl::Packet { id, constraints, fields, parent_id, .. }
        | ast::Decl::Struct { id, constraints, fields, parent_id, .. } => generate_packet_decl(
            scope,
            file.endianness.value,
            id,
@@ -369,6 +371,20 @@ mod tests {
        "#,
    );

    test_pdl!(
        struct_decl_complex_scalars,
        r#"
          struct Foo {
            a: 3,
            b: 8,
            c: 5,
            d: 24,
            e: 12,
            f: 4,
          }
        "#,
    );

    test_pdl!(packet_decl_8bit_enum, " enum Foo :  8 { A = 1, B = 2 } packet Bar { x: Foo }");
    test_pdl!(packet_decl_24bit_enum, "enum Foo : 24 { A = 1, B = 2 } packet Bar { x: Foo }");
    test_pdl!(packet_decl_64bit_enum, "enum Foo : 64 { A = 1, B = 2 } packet Bar { x: Foo }");
+1 −1
Original line number Diff line number Diff line
@@ -73,7 +73,7 @@ fn generate_unit_tests(input: &str, packet_names: &[&str], module_name: &str) {
                &test_vector.packed
            );
            let packed = hexadecimal_to_vec(&test_vector.packed);
            let packet_name = format_ident!("{}Packet", test_packet);
            let packet_name = format_ident!("{}", test_packet);
            let builder_name = format_ident!("{}Builder", test_packet);

            let object = test_vector.unpacked.as_object().unwrap_or_else(|| {
+9 −9
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ struct BarData {
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BarPacket {
pub struct Bar {
    #[cfg_attr(feature = "serde", serde(flatten))]
    bar: Arc<BarData>,
}
@@ -120,7 +120,7 @@ impl BarData {
        3
    }
}
impl Packet for BarPacket {
impl Packet for Bar {
    fn to_bytes(self) -> Bytes {
        let mut buffer = BytesMut::with_capacity(self.bar.get_total_size());
        self.bar.write_to(&mut buffer);
@@ -130,17 +130,17 @@ impl Packet for BarPacket {
        self.to_bytes().to_vec()
    }
}
impl From<BarPacket> for Bytes {
    fn from(packet: BarPacket) -> Self {
impl From<Bar> for Bytes {
    fn from(packet: Bar) -> Self {
        packet.to_bytes()
    }
}
impl From<BarPacket> for Vec<u8> {
    fn from(packet: BarPacket) -> Self {
impl From<Bar> for Vec<u8> {
    fn from(packet: Bar) -> Self {
        packet.to_vec()
    }
}
impl BarPacket {
impl Bar {
    pub fn parse(mut bytes: &[u8]) -> Result<Self> {
        Ok(Self::new(Arc::new(BarData::parse(bytes)?)).unwrap())
    }
@@ -153,8 +153,8 @@ impl BarPacket {
    }
}
impl BarBuilder {
    pub fn build(self) -> BarPacket {
    pub fn build(self) -> Bar {
        let bar = Arc::new(BarData { x: self.x });
        BarPacket::new(bar).unwrap()
        Bar::new(bar).unwrap()
    }
}
+9 −9
Original line number Diff line number Diff line
@@ -86,7 +86,7 @@ struct BarData {
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct BarPacket {
pub struct Bar {
    #[cfg_attr(feature = "serde", serde(flatten))]
    bar: Arc<BarData>,
}
@@ -120,7 +120,7 @@ impl BarData {
        3
    }
}
impl Packet for BarPacket {
impl Packet for Bar {
    fn to_bytes(self) -> Bytes {
        let mut buffer = BytesMut::with_capacity(self.bar.get_total_size());
        self.bar.write_to(&mut buffer);
@@ -130,17 +130,17 @@ impl Packet for BarPacket {
        self.to_bytes().to_vec()
    }
}
impl From<BarPacket> for Bytes {
    fn from(packet: BarPacket) -> Self {
impl From<Bar> for Bytes {
    fn from(packet: Bar) -> Self {
        packet.to_bytes()
    }
}
impl From<BarPacket> for Vec<u8> {
    fn from(packet: BarPacket) -> Self {
impl From<Bar> for Vec<u8> {
    fn from(packet: Bar) -> Self {
        packet.to_vec()
    }
}
impl BarPacket {
impl Bar {
    pub fn parse(mut bytes: &[u8]) -> Result<Self> {
        Ok(Self::new(Arc::new(BarData::parse(bytes)?)).unwrap())
    }
@@ -153,8 +153,8 @@ impl BarPacket {
    }
}
impl BarBuilder {
    pub fn build(self) -> BarPacket {
    pub fn build(self) -> Bar {
        let bar = Arc::new(BarData { x: self.x });
        BarPacket::new(bar).unwrap()
        Bar::new(bar).unwrap()
    }
}
Loading