Skip to content

Commit 1eef3cd

Browse files
committed
Replace c_macros phf_sets with a const array
1 parent 6c9a058 commit 1eef3cd

File tree

3 files changed

+40
-25
lines changed

3 files changed

+40
-25
lines changed

enums/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ edition = "2018"
88
enum-iterator = "^0.7"
99
clap = "^2.33"
1010
askama = "^0.10"
11-
phf_codegen = "^0.10"
1211

1312
tree-sitter = "0.19.3"
1413
tree-sitter-java = "0.19.0"

enums/src/rust.rs

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
extern crate phf_codegen;
2-
31
use askama::Template;
42
use enum_iterator::IntoEnumIterator;
53
use std::env;
64
use std::fs::File;
7-
use std::io::{BufWriter, Read, Write};
5+
use std::io::{Read, Write};
86
use std::path::{Path, PathBuf};
97

108
use crate::common::*;
@@ -39,40 +37,47 @@ pub fn generate_rust(output: &str, file_template: &str) -> std::io::Result<()> {
3937
Ok(())
4038
}
4139

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+
4248
pub fn generate_macros(output: &str) -> std::io::Result<()> {
4349
create_macros_file(output, "c_macros", "PREDEFINED_MACROS")?;
4450
create_macros_file(output, "c_specials", "SPECIALS")
4551
}
4652

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!(
5055
"{}/{}/{}.txt",
5156
&env::var("CARGO_MANIFEST_DIR").unwrap(),
5257
MACROS_DEFINITION_DIR,
53-
data_name
58+
filename
5459
)))?;
5560
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();
5764
for tok in data.split(|c| *c == b'\n') {
5865
let tok = std::str::from_utf8(tok).unwrap().trim();
5966
if !tok.is_empty() {
60-
set.entry(tok);
67+
names.push(tok.to_owned());
6168
}
6269
}
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())
7883
}

enums/templates/c_macros.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Code generated; DO NOT EDIT.
2+
3+
const {{ u_name }}: &[&str] = &[
4+
{% for name in names -%}
5+
"{{ name }}",
6+
{% endfor %}
7+
];
8+
9+
pub fn is_{{ l_name }}(mac: &str) -> bool {
10+
{{ u_name }}.contains(&mac)
11+
}

0 commit comments

Comments
 (0)