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

Commit 832a3bae authored by Qasim Javed's avatar Qasim Javed
Browse files

rusty-gd: Generate constructor for structs

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest

Change-Id: I65e5688c515bfc5e9792405b8207ebafe1d0538e
parent 9e2a70b7
Loading
Loading
Loading
Loading
+51 −1
Original line number Diff line number Diff line
@@ -754,6 +754,20 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const {
  s << "pub struct " << name_ << "Packet {";

  // Generate struct fields
  GenRustStructFieldNameAndType(s);

  // Generate size field
  auto params = GetParamList().GetFieldsWithoutTypes({
      PayloadField::kFieldType,
      BodyField::kFieldType,
  });
  if (params.size() > 0) {
    s << ", size: usize";
  }
  s << "}\n";
}

void PacketDef::GenRustStructFieldNameAndType(std::ostream& s) const {
  auto params = GetParamList().GetFieldsWithoutTypes({
      PayloadField::kFieldType,
      BodyField::kFieldType,
@@ -764,11 +778,47 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const {
      s << ", ";
    }
  }
}

  s << "}\n";
void PacketDef::GenRustStructFieldNames(std::ostream& s) const {
  auto params = GetParamList().GetFieldsWithoutTypes({
      PayloadField::kFieldType,
      BodyField::kFieldType,
  });
  for (int i = 0; i < params.size(); i++) {
    s << params[i]->GetName();
    if (i != params.size() - 1) {
      s << ", ";
    }
  }
}

void PacketDef::GenRustStructSizeField(std::ostream& s) const {
  int size = 0;
  auto params = GetParamList().GetFieldsWithoutTypes({
      PayloadField::kFieldType,
      BodyField::kFieldType,
  });
  for (int i = 0; i < params.size(); i++) {
    size += params[i]->GetSize().bytes();
  }
  if (params.size() > 0) {
    s << ", size: " << size;
  }
}

void PacketDef::GenRustStructImpls(std::ostream& s) const {
  s << "impl " << name_ << "Packet {";
  s << "pub fn new(";
  GenRustStructFieldNameAndType(s);
  s << ") -> Self { Self {";
  GenRustStructFieldNames(s);
  GenRustStructSizeField(s);
  s << "}}}\n";
}

void PacketDef::GenRustDef(std::ostream& s) const {
  GenRustChildEnums(s);
  GenRustStructDeclarations(s);
  GenRustStructImpls(s);
}
+8 −0
Original line number Diff line number Diff line
@@ -69,5 +69,13 @@ class PacketDef : public ParentDef {

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

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

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

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

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

  void GenRustDef(std::ostream& s) const;
};