Skip to content

Commit 636f7ae

Browse files
committed
prepend fields that are rust keywords with x
Type becomes xtype etc
1 parent ef12055 commit 636f7ae

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/keywords.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// https://doc.rust-lang.org/stable/reference/keywords.html
2+
const KEYWORDS: [&str; 52] = [
3+
"as", "break", "const", "continue", "crate", "else", "enum", "extern", "false", "fn", "for",
4+
"if", "impl", "in", "let", "loop", "match", "mod", "move", "mut", "pub", "ref", "return",
5+
"self", "Self", "static", "struct", "super", "trait", "true", "type", "unsafe", "use", "where",
6+
"while", "async", "await", "dyn", "abstract", "become", "box", "do", "final", "macro",
7+
"override", "priv", "typeof", "unsized", "virtual", "yield", "try", "union",
8+
];
9+
10+
pub fn is_keyword(x: &str) -> bool {
11+
KEYWORDS.contains(&x.to_lowercase().as_str())
12+
}
13+
14+
#[test]
15+
fn value_is_a_keyword() {
16+
assert!(is_keyword("type"));
17+
assert!(is_keyword("TYPE"));
18+
}
19+
20+
#[test]
21+
fn value_is_not_a_keyword() {
22+
assert!(!is_keyword("rpms"));
23+
}

src/main.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{fs::File, io::BufWriter, io::Write, path::PathBuf};
66
use structopt::StructOpt;
77

88
mod includes;
9+
mod keywords;
910
mod pad;
1011

1112
#[derive(Debug, StructOpt)]
@@ -522,7 +523,11 @@ fn type_name(x: &str) -> String {
522523
}
523524

524525
fn field_name(x: &str) -> String {
525-
x.to_snake_case()
526+
if keywords::is_keyword(x) {
527+
format!("x{}", x.to_snake_case())
528+
} else {
529+
x.to_snake_case()
530+
}
526531
}
527532

528533
fn enum_name(msg: &Message, signal: &Signal) -> String {

0 commit comments

Comments
 (0)