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

Commit bb881f6d authored by Qasim Javed's avatar Qasim Javed
Browse files

rusty-gd: Pretty print enum variants.

Implements the fmt::Display trait for all enums.

It will print a particular enum variant in hex followed by the name of
the variant in parantheses. Example showing the reset opcode.

0x0C03 (RESET)

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

Change-Id: I60d62a70890ab2daa51e206b13ee189a1468cbd0
parent 56fa0870
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -68,6 +68,16 @@ void EnumGen::GenRustDef(std::ostream& stream) {
  }
  stream << "}";

  stream << "impl fmt::Display for " << e_.name_ << " {";
  stream << "fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {";
  stream << "match self {";
  for (const auto& pair : e_.constants_) {
    stream << e_.name_ << "::" << util::ConstantCaseToCamelCase(pair.second) << " => "
           << "write!(f, \"{:#0" << (util::RoundSizeUp(e_.size_) / 4) + 2 << "X} (" << pair.second << ")\", "
           << "self.to_" << util::GetRustTypeForSize(e_.size_) << "().unwrap()),";
  }
  stream << "}}}\n";

  if (e_.try_from_enum_ != nullptr) {
    std::vector<std::string> other_items;
    for (const auto& pair : e_.try_from_enum_->constants_) {
+1 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ use bytes::{Bytes, BytesMut, BufMut};
use num_derive::{FromPrimitive, ToPrimitive};
use num_traits::{FromPrimitive, ToPrimitive};
use std::convert::TryInto;
use std::fmt;
use thiserror::Error;
use std::sync::Arc;

+5 −5
Original line number Diff line number Diff line
@@ -187,8 +187,8 @@ async fn dispatch(
                                    error!("failure dispatching command status {:?}", e);
                                }
                            },
                            Some(QueuedCommand{cmd, ..}) => panic!("Waiting for {:?}, got {:?}", cmd.get_op_code(), this_opcode),
                            None => panic!("Unexpected status event with opcode {:?}", this_opcode),
                            Some(QueuedCommand{cmd, ..}) => panic!("Waiting for {}, got {}", cmd.get_op_code(), this_opcode),
                            None => panic!("Unexpected status event with opcode {}", this_opcode),
                        }
                    },
                    CommandComplete(evt) => {
@@ -200,8 +200,8 @@ async fn dispatch(
                                    error!("failure dispatching command complete {:?}", e);
                                }
                            },
                            Some(QueuedCommand{cmd, ..}) => panic!("Waiting for {:?}, got {:?}", cmd.get_op_code(), this_opcode),
                            None => panic!("Unexpected complete event with opcode {:?}", this_opcode),
                            Some(QueuedCommand{cmd, ..}) => panic!("Waiting for {}, got {}", cmd.get_op_code(), this_opcode),
                            None => panic!("Unexpected complete event with opcode {}", this_opcode),
                        }
                    },
                    LeMetaEvent(evt) => {
@@ -239,7 +239,7 @@ async fn dispatch(
                hci_timeout.reset(Duration::from_secs(2));
                pending = Some(queued);
            },
            _ = hci_timeout.expired() => panic!("Timed out waiting for {:?}", pending.unwrap().cmd.get_op_code()),
            _ = hci_timeout.expired() => panic!("Timed out waiting for {}", pending.unwrap().cmd.get_op_code()),
            else => break,
        }
    }