Skip to content

Commit

Permalink
Generate enum variants
Browse files Browse the repository at this point in the history
  • Loading branch information
morr0ne committed Aug 15, 2024
1 parent 04d4cbb commit e804169
Show file tree
Hide file tree
Showing 2 changed files with 715 additions and 132 deletions.
40 changes: 31 additions & 9 deletions gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,14 @@ fn description_to_docs(description: Option<&String>) -> Vec<TokenStream> {
docs
}

fn value_to_u32(value: &str) -> u32 {
if let Some(s) = value.strip_prefix("0x") {
u32::from_str_radix(s, 16).expect("Invalid enum value")
} else {
value.parse().expect("Invalid enum value")
}
}

fn make_ident<D: Display>(ident: D) -> Ident {
if KEYWORDS.contains(&ident.to_string().as_str()) {
return format_ident!("r#{ident}");
Expand All @@ -446,8 +454,6 @@ fn make_ident<D: Display>(ident: D) -> Ident {
}

fn write_enums(interface: &Interface) -> Vec<TokenStream> {
// quote! {}

let mut enums = Vec::new();

for e in &interface.enums {
Expand All @@ -456,8 +462,28 @@ fn write_enums(interface: &Interface) -> Vec<TokenStream> {

if !e.bitfield {
let mut variants = Vec::new();
let mut match_variants = Vec::new();

for entry in &e.entries {
let mut prefix = "";

if entry.name.chars().next().unwrap().is_numeric() {
prefix = "_"
}

let docs = description_to_docs(entry.description.as_ref());
let name = make_ident(format!("{prefix}{}", entry.name.to_upper_camel_case()));
let value = value_to_u32(&entry.value);

// match_variants.push_str(&format!("{value} => Ok(Self::{prefix}{name}),"));

variants.push(quote! {
#(#docs)*
#name = #value
});

variants.push(quote! { Temp });
match_variants.push(quote! {});
}

enums.push(quote! {
#[repr(u32)]
Expand All @@ -472,7 +498,7 @@ fn write_enums(interface: &Interface) -> Vec<TokenStream> {

fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
// {match_variants}
// #(#match_variants),*
_ => Err(crate::wire::DecodeError::MalformedPayload)
}
}
Expand All @@ -492,11 +518,7 @@ fn write_enums(interface: &Interface) -> Vec<TokenStream> {

let docs = description_to_docs(entry.summary.as_ref());

let value: u32 = if let Some(s) = entry.value.strip_prefix("0x") {
u32::from_str_radix(s, 16).expect("Invalid enum value")
} else {
entry.value.parse().expect("Invalid enum value")
};
let value = value_to_u32(&entry.value);

variants.push(quote! {
#(#docs)*
Expand Down
Loading

0 comments on commit e804169

Please sign in to comment.