• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use proc_macro2::{Literal, TokenStream};
16 use quote::{format_ident, quote};
17 
18 use crate::ast;
19 
20 use super::utils::get_integer_type;
21 
generate_enum(id: &str, tags: &[ast::Tag], width: usize) -> TokenStream22 pub fn generate_enum(id: &str, tags: &[ast::Tag], width: usize) -> TokenStream {
23     let id_ident = format_ident!("{id}");
24     let tag_ids = tags.iter().map(|tag| format_ident!("{}", tag.id())).collect::<Vec<_>>();
25     let tag_values = tags
26         .iter()
27         .map(|tag| Literal::u64_unsuffixed(tag.value().unwrap() as u64))
28         .collect::<Vec<_>>();
29     let backing_ident = get_integer_type(width);
30 
31     quote! {
32         #[derive(Copy, Clone, PartialEq, Eq, Debug)]
33         pub enum #id_ident {
34             #(#tag_ids),*
35         }
36 
37         impl #id_ident {
38             pub fn new(value: #backing_ident) -> Result<Self, ParseError> {
39                 match value {
40                     #(#tag_values => Ok(Self::#tag_ids)),*,
41                     _ => Err(ParseError::InvalidEnumValue),
42                 }
43             }
44 
45             pub fn value(&self) -> #backing_ident {
46                 match self {
47                     #(Self::#tag_ids => #tag_values),*,
48                 }
49             }
50 
51             fn try_parse(buf: BitSlice) -> Result<Self, ParseError> {
52                 let value = buf.slice(#width)?.try_parse()?;
53                 match value {
54                     #(#tag_values => Ok(Self::#tag_ids)),*,
55                     _ => Err(ParseError::InvalidEnumValue),
56                 }
57             }
58         }
59 
60         impl Serializable for #id_ident {
61             fn serialize(&self, writer: &mut impl BitWriter) -> Result<(), SerializeError> {
62                 writer.write_bits(#width, || Ok(self.value()));
63                 Ok(())
64             }
65         }
66 
67         impl From<#id_ident> for #backing_ident {
68             fn from(x: #id_ident) -> #backing_ident {
69                 x.value()
70             }
71         }
72 
73         impl TryFrom<#backing_ident> for #id_ident {
74             type Error = ParseError;
75 
76             fn try_from(value: #backing_ident) -> Result<Self, ParseError> {
77                 Self::new(value)
78             }
79         }
80     }
81 }
82