|
1 | | -extern crate phf_codegen; |
2 | | - |
3 | 1 | use askama::Template; |
4 | 2 | use enum_iterator::IntoEnumIterator; |
5 | 3 | use std::env; |
6 | 4 | use std::fs::File; |
7 | | -use std::io::{BufWriter, Read, Write}; |
| 5 | +use std::io::{Read, Write}; |
8 | 6 | use std::path::{Path, PathBuf}; |
9 | 7 |
|
10 | 8 | use crate::common::*; |
@@ -39,40 +37,47 @@ pub fn generate_rust(output: &str, file_template: &str) -> std::io::Result<()> { |
39 | 37 | Ok(()) |
40 | 38 | } |
41 | 39 |
|
| 40 | +#[derive(Template)] |
| 41 | +#[template(path = "c_macros.rs", escape = "none")] |
| 42 | +struct CMacrosTemplate { |
| 43 | + u_name: String, |
| 44 | + l_name: String, |
| 45 | + names: Vec<String>, |
| 46 | +} |
| 47 | + |
42 | 48 | pub fn generate_macros(output: &str) -> std::io::Result<()> { |
43 | 49 | create_macros_file(output, "c_macros", "PREDEFINED_MACROS")?; |
44 | 50 | create_macros_file(output, "c_specials", "SPECIALS") |
45 | 51 | } |
46 | 52 |
|
47 | | -fn create_macros_file(output: &str, data_name: &str, set_name: &str) -> std::io::Result<()> { |
48 | | - let mut set = phf_codegen::Set::new(); |
49 | | - let mut file = File::open(PathBuf::from(format!( |
| 53 | +fn create_macros_file(output: &str, filename: &str, u_name: &str) -> std::io::Result<()> { |
| 54 | + let mut macro_file = File::open(PathBuf::from(format!( |
50 | 55 | "{}/{}/{}.txt", |
51 | 56 | &env::var("CARGO_MANIFEST_DIR").unwrap(), |
52 | 57 | MACROS_DEFINITION_DIR, |
53 | | - data_name |
| 58 | + filename |
54 | 59 | )))?; |
55 | 60 | let mut data = Vec::new(); |
56 | | - file.read_to_end(&mut data)?; |
| 61 | + macro_file.read_to_end(&mut data)?; |
| 62 | + |
| 63 | + let mut names = Vec::new(); |
57 | 64 | for tok in data.split(|c| *c == b'\n') { |
58 | 65 | let tok = std::str::from_utf8(tok).unwrap().trim(); |
59 | 66 | if !tok.is_empty() { |
60 | | - set.entry(tok); |
| 67 | + names.push(tok.to_owned()); |
61 | 68 | } |
62 | 69 | } |
63 | | - let path = Path::new(output).join(format!("{}.rs", data_name)); |
64 | | - let mut file = BufWriter::new(File::create(&path)?); |
65 | | - writeln!(&mut file, "#[allow(clippy::unreadable_literal)]").unwrap(); |
66 | | - writeln!( |
67 | | - &mut file, |
68 | | - "static {}: phf::Set<&'static str> =\n{};\n", |
69 | | - set_name, |
70 | | - set.build() |
71 | | - )?; |
72 | | - writeln!( |
73 | | - &mut file, |
74 | | - "pub fn is_{}(mac: &str) -> bool {{ {}.contains(mac) }}\n", |
75 | | - set_name.to_lowercase(), |
76 | | - set_name, |
77 | | - ) |
| 70 | + let l_name = u_name.to_lowercase(); |
| 71 | + |
| 72 | + let path = Path::new(output).join(format!("{}.rs", filename)); |
| 73 | + |
| 74 | + let mut file = File::create(&path)?; |
| 75 | + |
| 76 | + let args = CMacrosTemplate { |
| 77 | + u_name: u_name.to_owned(), |
| 78 | + l_name, |
| 79 | + names, |
| 80 | + }; |
| 81 | + |
| 82 | + file.write_all(args.render().unwrap().as_bytes()) |
78 | 83 | } |
0 commit comments