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

Commit 1fb628d2 authored by Zach Johnson's avatar Zach Johnson
Browse files

rusty-gd: add stubs for to_bytes and write_to

Bug: 171749953
Tag: #gd-refactor
Test: gd/cert/run --rhost SimpleHalTest
Change-Id: I499835b0872c97f037cee68939247613bfa922da
parent 0cf0528e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@
void generate_rust_packet_preamble(std::ostream& s) {
  s <<
      R"(
use bytes::Bytes;
use bytes::{Bytes, BytesMut};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::FromPrimitive;
use std::convert::TryInto;
+17 −0
Original line number Diff line number Diff line
@@ -827,6 +827,7 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
  }
  s << "}}";

  // parse function
  s << "pub fn parse(bytes: &[u8]) -> Result<Self> {";
  auto fields = fields_.GetFieldsWithoutTypes({
      BodyField::kFieldType,
@@ -863,6 +864,22 @@ void PacketDef::GenRustStructImpls(std::ostream& s) const {
  }
  s << "})}\n";

  // to_bytes function (only on root packet types)
  if (parent_ == nullptr) {
    s << "fn to_bytes(self) -> Bytes {";
    s << " let mut buffer = BytesMut::new();";
    s << " self.write_to(&mut buffer);";
    s << " buffer.freeze()";
    s << "}\n";
  }

  // write_to function
  s << "fn write_to(&self, buffer: &mut BytesMut) {";
  if (fields_exist) {
    s << " buffer.resize(buffer.len() + self.size, 0);";
  }
  s << "}\n";

  if (fields_exist) {
    s << "pub fn get_size(&self) -> usize { self.size }";
  }