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

Commit 6f5475bf authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: integrate rusty packets into existing code

still gotta wait for parsing to land for this to officially work, but
this allows us to play with the ergonomics independently based on what
compiles..

next CLs will start to improve the ergos.

some ergos improved this CL.

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I5eadd7619c5d392c2dd03331c5d604d9a52a1687
parent 6d141b5c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -61,7 +61,7 @@ void EnumGen::GenLogging(std::ostream& stream) {
}

void EnumGen::GenRustDef(std::ostream& stream) {
  stream << "#[derive(FromPrimitive, ToPrimitive)]\n";
  stream << "#[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)]\n";
  stream << "pub enum " << e_.name_ << " {";
  for (const auto& pair : e_.constants_) {
    stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ",";
+1 −1
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use std::convert::TryInto;
use thiserror::Error;
use std::rc::Rc;
use std::sync::Arc;

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

+13 −8
Original line number Diff line number Diff line
@@ -742,12 +742,14 @@ void PacketDef::GenBuilderConstructor(std::ostream& s) const {

void PacketDef::GenRustChildEnums(std::ostream& s) const {
  if (!children_.empty()) {
    s << "#[derive(Debug)] ";
    s << "enum " << name_ << "DataChild {";
    for (const auto& child : children_) {
      s << child->name_ << "(Rc<" << child->name_ << "Data>),";
      s << child->name_ << "(Arc<" << child->name_ << "Data>),";
    }
    s << "None,";
    s << "}\n";
    s << "#[derive(Debug)] ";
    s << "pub enum " << name_ << "Child {";
    for (const auto& child : children_) {
      s << child->name_ << "(" << child->name_ << "Packet),";
@@ -758,6 +760,7 @@ void PacketDef::GenRustChildEnums(std::ostream& s) const {
}

void PacketDef::GenRustStructDeclarations(std::ostream& s) const {
  s << "#[derive(Debug)] ";
  s << "struct " << name_ << "Data {";

  // Generate struct fields
@@ -768,16 +771,18 @@ void PacketDef::GenRustStructDeclarations(std::ostream& s) const {
  s << "}\n";

  // Generate accessor struct
  s << "#[derive(Debug, Clone)] ";
  s << "pub struct " << name_ << "Packet {";
  auto lineage = GetAncestors();
  lineage.push_back(this);
  for (auto it = lineage.begin(); it != lineage.end(); it++) {
    auto def = *it;
    s << util::CamelCaseToUnderScore(def->name_) << ": Rc<" << def->name_ << "Data>,";
    s << util::CamelCaseToUnderScore(def->name_) << ": Arc<" << def->name_ << "Data>,";
  }
  s << "}\n";

  // Generate builder struct
  s << "#[derive(Debug)] ";
  s << "pub struct " << name_ << "Builder {";
  auto params = GetParamList().GetFieldsWithoutTypes({
      PayloadField::kFieldType,
@@ -943,7 +948,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
  s << "impl " << name_ << "Packet {";
  if (parent_ == nullptr) {
    s << "pub fn parse(bytes: &[u8]) -> Result<Self> { ";
    s << "Ok(Self::new(Rc::new(" << name_ << "Data::parse(bytes)?)))";
    s << "Ok(Self::new(Arc::new(" << name_ << "Data::parse(bytes)?)))";
    s << "}";
  }
  auto root = GetRootDef();
@@ -956,11 +961,11 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
  s << "}\n";

  if (!children_.empty()) {
    s << " pub fn specialize(self) -> " << name_ << "Child {";
    s << " match self." << util::CamelCaseToUnderScore(name_) << ".child {";
    s << " pub fn specialize(&self) -> " << name_ << "Child {";
    s << " match &self." << util::CamelCaseToUnderScore(name_) << ".child {";
    for (const auto& child : children_) {
      s << name_ << "DataChild::" << child->name_ << "(_) => " << name_ << "Child::" << child->name_ << "("
        << child->name_ << "Packet::new(self." << root_accessor << ")),";
        << child->name_ << "Packet::new(self." << root_accessor << ".clone())),";
    }
    s << name_ << "DataChild::None => " << name_ << "Child::None,";
    s << "}}";
@@ -969,7 +974,7 @@ void PacketDef::GenRustAccessStructImpls(std::ostream& s) const {
  lineage.push_back(this);
  const ParentDef* prev = nullptr;

  s << " fn new(root: Rc<" << root->name_ << "Data>) -> Self {";
  s << " fn new(root: Arc<" << root->name_ << "Data>) -> Self {";
  for (auto it = lineage.begin(); it != lineage.end(); it++) {
    auto def = *it;
    auto accessor_name = util::CamelCaseToUnderScore(def->name_);
@@ -1044,7 +1049,7 @@ void PacketDef::GenRustBuilderStructImpls(std::ostream& s) const {
    });

    auto accessor_name = util::CamelCaseToUnderScore(ancestor->name_);
    s << "let " << accessor_name << "= Rc::new(" << ancestor->name_ << "Data {";
    s << "let " << accessor_name << "= Arc::new(" << ancestor->name_ << "Data {";
    for (auto field : fields) {
      auto constraint = all_constraints.find(field->GetName());
      s << field->GetName() << ": ";
+1 −0
Original line number Diff line number Diff line
@@ -358,6 +358,7 @@ void StructDef::GenRustSizeField(std::ostream& s) const {
}

void StructDef::GenRustDeclarations(std::ostream& s) const {
  s << "#[derive(Debug)] ";
  s << "pub struct " << name_ << "{";

  // Generate struct fields
+5 −1
Original line number Diff line number Diff line
@@ -5,8 +5,8 @@ rust_library {
    srcs: ["src/lib.rs"],
    edition: "2018",
    rustlibs: [
        "libbt_packet",
        "libbt_facade_proto",
        "libbt_packets",
        "libbytes",
        "libfutures",
        "libthiserror",
@@ -19,6 +19,10 @@ rust_library {
        "liblazy_static",
        "liblog_rust",
        "libbt_common",
        "libnum_traits",
    ],
    proc_macros: [
        "libnum_derive",
    ],
    target: {
        android: {
Loading