Skip to content

Commit 9b91987

Browse files
authored
c#: Mechanical refactors (#1110)
* Rename generators for clarity Signed-off-by: James Sturtevant <jsturtevant@gmail.com> * Move interface generator to own module Signed-off-by: James Sturtevant <jsturtevant@gmail.com> * Move function generator to own module Signed-off-by: James Sturtevant <jsturtevant@gmail.com> * Move Ident code to own file Signed-off-by: James Sturtevant <jsturtevant@gmail.com> * Move World generator code to own file Signed-off-by: James Sturtevant <jsturtevant@gmail.com> * Add some comments and alittle clean up Signed-off-by: James Sturtevant <jsturtevant@gmail.com> --------- Signed-off-by: James Sturtevant <jsturtevant@gmail.com>
1 parent c2e7892 commit 9b91987

File tree

5 files changed

+3604
-3557
lines changed

5 files changed

+3604
-3557
lines changed

crates/csharp/src/csharp_ident.rs

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
use heck::{ToLowerCamelCase, ToUpperCamelCase};
2+
3+
pub(crate) trait ToCSharpIdent: ToOwned {
4+
fn csharp_keywords() -> Vec<&'static str>;
5+
fn to_csharp_ident(&self) -> Self::Owned;
6+
fn to_csharp_ident_upper(&self) -> Self::Owned;
7+
}
8+
9+
impl ToCSharpIdent for str {
10+
// Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
11+
fn csharp_keywords() -> Vec<&'static str> {
12+
vec![
13+
"abstract",
14+
"as",
15+
"base",
16+
"bool",
17+
"break",
18+
"byte",
19+
"case",
20+
"catch",
21+
"char",
22+
"checked",
23+
"class",
24+
"const",
25+
"continue",
26+
"decimal",
27+
"default",
28+
"delegate",
29+
"do",
30+
"double",
31+
"else",
32+
"enum",
33+
"event",
34+
"explicit",
35+
"extern",
36+
"false",
37+
"finally",
38+
"fixed",
39+
"float",
40+
"for",
41+
"foreach",
42+
"goto",
43+
"if",
44+
"implicit",
45+
"in",
46+
"int",
47+
"interface",
48+
"internal",
49+
"is",
50+
"lock",
51+
"long",
52+
"namespace",
53+
"new",
54+
"null",
55+
"object",
56+
"operator",
57+
"out",
58+
"override",
59+
"params",
60+
"private",
61+
"protected",
62+
"public",
63+
"readonly",
64+
"ref",
65+
"return",
66+
"sbyte",
67+
"sealed",
68+
"short",
69+
"sizeof",
70+
"stackalloc",
71+
"static",
72+
"string",
73+
"struct",
74+
"switch",
75+
"this",
76+
"throw",
77+
"true",
78+
"try",
79+
"typeof",
80+
"uint",
81+
"ulong",
82+
"unchecked",
83+
"unsafe",
84+
"ushort",
85+
"using",
86+
"virtual",
87+
"void",
88+
"volatile",
89+
"while",
90+
]
91+
}
92+
93+
fn to_csharp_ident(&self) -> String {
94+
// Escape C# keywords
95+
if Self::csharp_keywords().contains(&self) {
96+
format!("@{}", self)
97+
} else {
98+
self.to_lower_camel_case()
99+
}
100+
}
101+
102+
fn to_csharp_ident_upper(&self) -> String {
103+
// Escape C# keywords
104+
if Self::csharp_keywords().contains(&self) {
105+
format!("@{}", self)
106+
} else {
107+
self.to_upper_camel_case()
108+
}
109+
}
110+
}

0 commit comments

Comments
 (0)