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

Commit 17cb6146 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: separate data storage from packet view

create XYZData structs & enums, which will be internally accessed through
the exposed view

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost
Change-Id: I734e1ce5198223b51364e0cb5cd8370104c7b8ef
parent 6c35d197
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use std::convert::TryInto;
use thiserror::Error;
use std::rc::Rc;

type Result<T> = std::result::Result<T, Error>;

+11 −2
Original line number Diff line number Diff line
@@ -742,6 +742,11 @@ void PacketDef::GenBuilderConstructor(std::ostream& s) const {

void PacketDef::GenRustChildEnums(std::ostream& s) const {
  if (!children_.empty()) {
    s << "enum " << name_ << "DataChild {";
    for (const auto& child : children_) {
      s << child->name_ << "(" << child->name_ << "Data),";
    }
    s << "}\n";
    s << "pub enum " << name_ << "Child {";
    for (const auto& child : children_) {
      s << child->name_ << "(" << child->name_ << "Packet),";
@@ -751,7 +756,7 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const {
}

void PacketDef::GenRustStructDeclarations(std::ostream& s) const {
  s << "pub struct " << name_ << "Packet {";
  s << "struct " << name_ << "Data {";

  // Generate struct fields
  GenRustStructFieldNameAndType(s);
@@ -767,6 +772,10 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const {
    s << " size: usize";
  }
  s << "}\n";

  s << "pub struct " << name_ << "Packet {";
  s << "root: Rc<" << GetRootDef()->name_ << "Data>,";
  s << "}\n";
}

bool PacketDef::GenRustStructFieldNameAndType(std::ostream& s) const {
@@ -817,7 +826,7 @@ void PacketDef::GenRustStructSizeField(std::ostream& s) const {
}

void PacketDef::GenRustStructImpls(std::ostream& s) const {
  s << "impl " << name_ << "Packet {";
  s << "impl " << name_ << "Data {";
  s << "pub fn new(";
  bool fields_exist = GenRustStructFieldNameAndType(s);
  s << ") -> Self { Self {";
+8 −0
Original line number Diff line number Diff line
@@ -482,3 +482,11 @@ void ParentDef::GenInstanceOf(std::ostream& s) const {
    s << "return true;}";
  }
}

const ParentDef* ParentDef::GetRootDef() const {
  if (parent_ == nullptr) {
    return this;
  }

  return parent_->GetRootDef();
}
+2 −0
Original line number Diff line number Diff line
@@ -61,6 +61,8 @@ class ParentDef : public TypeDef {

  void GenInstanceOf(std::ostream& s) const;

  const ParentDef* GetRootDef() const;

  FieldList fields_;

  ParentDef* parent_{nullptr};