summaryrefslogtreecommitdiff
path: root/system/gd/packet/parser/enum_gen.cc
blob: 1a2e9caa0dac0b6265b8d364c35b73bfa19bedd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Copyright 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "enum_gen.h"

#include <iostream>

#include "util.h"

EnumGen::EnumGen(EnumDef e) : e_(std::move(e)) {}

void EnumGen::GenDefinition(std::ostream& stream) {
  stream << "enum class ";
  stream << e_.name_;
  stream << " : " << util::GetTypeForSize(e_.size_);
  stream << " {";
  for (const auto& pair : e_.constants_) {
    stream << pair.second << " = 0x" << std::hex << pair.first << std::dec << ",";
  }
  stream << "};\n";
}

void EnumGen::GenDefinitionPybind11(std::ostream& stream) {
  stream << "py::enum_<" << e_.name_ << ">(m, \"" << e_.name_ << "\")";
  for (const auto& pair : e_.constants_) {
    stream << ".value(\"" << pair.second << "\", " << e_.name_ << "::" << pair.second << ")";
  }
  stream << ";\n";
}

void EnumGen::GenLogging(std::ostream& stream) {
  // Print out the switch statement that converts all the constants to strings.
  stream << "inline std::string " << e_.name_ << "Text(const " << e_.name_ << "& param) {";
  stream << "switch (param) {";
  for (const auto& pair : e_.constants_) {
    stream << "case " << e_.name_ << "::" << pair.second << ":";
    stream << "  return \"" << pair.second << "\";";
  }
  stream << "default:";
  stream << "  return std::string(\"Unknown " << e_.name_ << ": \") + std::to_string(static_cast<int>(param));";
  stream << "}";
  stream << "}\n\n";

  // Print out the stream operator so that the constant can be written to streams.
  stream << "inline std::ostream& operator<<(std::ostream& os, const " << e_.name_ << "& param) {";
  stream << "  return os << " << e_.name_ << "Text(param);";
  stream << "}\n";
}

void EnumGen::GenRustDef(std::ostream& stream) {
  stream << "#[derive(FromPrimitive, ToPrimitive, Debug, Hash, Eq, PartialEq, Clone, Copy)]\n";
  stream << "#[repr(u64)]\n";
  stream << "pub enum " << e_.name_ << " {";
  for (const auto& pair : e_.constants_) {
    stream << util::ConstantCaseToCamelCase(pair.second) << " = 0x" << std::hex << pair.first << std::dec << ",";
  }
  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_) {
      other_items.push_back(pair.second);
    }
    stream << "impl TryFrom<" << e_.try_from_enum_->name_ << "> for " << e_.name_ << " {";
    stream << "type Error = &'static str;";
    stream << "fn try_from(value: " << e_.try_from_enum_->name_ << ") -> std::result::Result<Self, Self::Error> {";
    stream << "match value {";
    for (const auto& pair : e_.constants_) {
      if (std::find(other_items.begin(), other_items.end(), pair.second) == other_items.end()) {
        continue;
      }
      auto constant_name = util::ConstantCaseToCamelCase(pair.second);
      stream << e_.try_from_enum_->name_ << "::" << constant_name << " => Ok(" << e_.name_ << "::" << constant_name
             << "),";
    }
    stream << "_ => Err(\"No mapping for provided key\"),";
    stream << "}}}";
  }
}