Skip to main content

tii_procmacro/
lib.rs

1use proc_macro::TokenStream;
2
3fn number_to_qvalue(i: u16) -> String {
4  // We handle the edge cases of 0 and 1000, while using f32 for the rest.
5  match i {
6    0 => "0.0".to_string(),
7    1000 => "1.0".to_string(),
8    _ => format!("{}", f64::from(i) / 1000.0),
9  }
10}
11
12#[proc_macro]
13pub fn qvalue_to_strs(_input: TokenStream) -> TokenStream {
14  let mut output = String::new();
15  output.push_str("match self.0 {");
16  for i in 0..=1000 {
17    // Generate the match arm for each number
18    output.push_str(&format!("{} => \"{}\",\n", i, number_to_qvalue(i)));
19  }
20  output.push_str("_ => unreachable!()");
21  output.push('}');
22  output.parse().unwrap()
23}
24
25#[proc_macro]
26pub fn hex_chunked_lut(input: TokenStream) -> TokenStream {
27  let x: u64 = input.to_string().parse().unwrap();
28  let mut output = String::new();
29  output.push_str("[b\"0\r\n\"");
30  for i in 1..x {
31    output.push_str(format!(", b\"{:X}\\r\\n\"", i).as_str());
32  }
33  output.push(']');
34  output.parse().unwrap()
35}