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

Commit 381a8c07 authored by Martin Geisler's avatar Martin Geisler
Browse files

pdl: Implement specialize functionality

This mirrors the “inheritance” implemented by the old
bluetooth_packetgen compiler.

Tag: #feature
Bug: 228306436
Test: atest pdl_tests pdl_rust_generator_tests_{le,be}
Change-Id: I8f66bfce60e95b13446242c32c02098c5416e304
parent 702caaf6
Loading
Loading
Loading
Loading
+18 −0
Original line number Diff line number Diff line
@@ -474,6 +474,21 @@ fn generate_packet_decl(
            }
        }
    });
    let specialize = has_children_or_payload.then(|| {
        quote! {
            pub fn specialize(&self) -> #id_child {
                match &self.#id_lower.child {
                    #(
                        #id_data_child::#child(_) =>
                        #id_child::#child(#child::new(self.#top_level_id_lower.clone()).unwrap()),
                    )*
                    #id_data_child::Payload(payload) => #id_child::Payload(payload.clone()),
                    #id_data_child::None => #id_child::None,
                }
            }
        }
    });

    let builder_payload_field = has_children_or_payload.then(|| {
        quote! {
            pub payload: Option<Bytes>
@@ -566,6 +581,9 @@ fn generate_packet_decl(
                let data = #top_level_data::parse(&mut bytes)?;
                Ok(Self::new(Arc::new(data)).unwrap())
            }

            #specialize

            fn new(#top_level_id_lower: Arc<#top_level_data>)
                   -> std::result::Result<Self, &'static str> {
                #(
+8 −0
Original line number Diff line number Diff line
@@ -248,6 +248,14 @@ impl Foo {
        let data = FooData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> FooChild {
        match &self.foo.child {
            FooDataChild::Bar(_) => FooChild::Bar(Bar::new(self.foo.clone()).unwrap()),
            FooDataChild::Baz(_) => FooChild::Baz(Baz::new(self.foo.clone()).unwrap()),
            FooDataChild::Payload(payload) => FooChild::Payload(payload.clone()),
            FooDataChild::None => FooChild::None,
        }
    }
    fn new(foo: Arc<FooData>) -> std::result::Result<Self, &'static str> {
        Ok(Self { foo })
    }
+8 −0
Original line number Diff line number Diff line
@@ -248,6 +248,14 @@ impl Foo {
        let data = FooData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> FooChild {
        match &self.foo.child {
            FooDataChild::Bar(_) => FooChild::Bar(Bar::new(self.foo.clone()).unwrap()),
            FooDataChild::Baz(_) => FooChild::Baz(Baz::new(self.foo.clone()).unwrap()),
            FooDataChild::Payload(payload) => FooChild::Payload(payload.clone()),
            FooDataChild::None => FooChild::None,
        }
    }
    fn new(foo: Arc<FooData>) -> std::result::Result<Self, &'static str> {
        Ok(Self { foo })
    }
+35 −0
Original line number Diff line number Diff line
@@ -247,6 +247,15 @@ impl Parent {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> ParentChild {
        match &self.parent.child {
            ParentDataChild::Child(_) => {
                ParentChild::Child(Child::new(self.parent.clone()).unwrap())
            }
            ParentDataChild::Payload(payload) => ParentChild::Payload(payload.clone()),
            ParentDataChild::None => ParentChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        Ok(Self { parent })
    }
@@ -419,6 +428,15 @@ impl Child {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> ChildChild {
        match &self.child.child {
            ChildDataChild::GrandChild(_) => {
                ChildChild::GrandChild(GrandChild::new(self.parent.clone()).unwrap())
            }
            ChildDataChild::Payload(payload) => ChildChild::Payload(payload.clone()),
            ChildDataChild::None => ChildChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        let child = match &parent.child {
            ParentDataChild::Child(value) => value.clone(),
@@ -604,6 +622,15 @@ impl GrandChild {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> GrandChildChild {
        match &self.grandchild.child {
            GrandChildDataChild::GrandGrandChild(_) => {
                GrandChildChild::GrandGrandChild(GrandGrandChild::new(self.parent.clone()).unwrap())
            }
            GrandChildDataChild::Payload(payload) => GrandChildChild::Payload(payload.clone()),
            GrandChildDataChild::None => GrandChildChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        let child = match &parent.child {
            ParentDataChild::Child(value) => value.clone(),
@@ -793,6 +820,14 @@ impl GrandGrandChild {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> GrandGrandChildChild {
        match &self.grandgrandchild.child {
            GrandGrandChildDataChild::Payload(payload) => {
                GrandGrandChildChild::Payload(payload.clone())
            }
            GrandGrandChildDataChild::None => GrandGrandChildChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        let child = match &parent.child {
            ParentDataChild::Child(value) => value.clone(),
+35 −0
Original line number Diff line number Diff line
@@ -247,6 +247,15 @@ impl Parent {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> ParentChild {
        match &self.parent.child {
            ParentDataChild::Child(_) => {
                ParentChild::Child(Child::new(self.parent.clone()).unwrap())
            }
            ParentDataChild::Payload(payload) => ParentChild::Payload(payload.clone()),
            ParentDataChild::None => ParentChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        Ok(Self { parent })
    }
@@ -419,6 +428,15 @@ impl Child {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> ChildChild {
        match &self.child.child {
            ChildDataChild::GrandChild(_) => {
                ChildChild::GrandChild(GrandChild::new(self.parent.clone()).unwrap())
            }
            ChildDataChild::Payload(payload) => ChildChild::Payload(payload.clone()),
            ChildDataChild::None => ChildChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        let child = match &parent.child {
            ParentDataChild::Child(value) => value.clone(),
@@ -604,6 +622,15 @@ impl GrandChild {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> GrandChildChild {
        match &self.grandchild.child {
            GrandChildDataChild::GrandGrandChild(_) => {
                GrandChildChild::GrandGrandChild(GrandGrandChild::new(self.parent.clone()).unwrap())
            }
            GrandChildDataChild::Payload(payload) => GrandChildChild::Payload(payload.clone()),
            GrandChildDataChild::None => GrandChildChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        let child = match &parent.child {
            ParentDataChild::Child(value) => value.clone(),
@@ -793,6 +820,14 @@ impl GrandGrandChild {
        let data = ParentData::parse(&mut bytes)?;
        Ok(Self::new(Arc::new(data)).unwrap())
    }
    pub fn specialize(&self) -> GrandGrandChildChild {
        match &self.grandgrandchild.child {
            GrandGrandChildDataChild::Payload(payload) => {
                GrandGrandChildChild::Payload(payload.clone())
            }
            GrandGrandChildDataChild::None => GrandGrandChildChild::None,
        }
    }
    fn new(parent: Arc<ParentData>) -> std::result::Result<Self, &'static str> {
        let child = match &parent.child {
            ParentDataChild::Child(value) => value.clone(),
Loading