Skip to content

Commit

Permalink
c#: Mechanical refactors (#1110)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
jsturtevant authored Dec 20, 2024
1 parent c2e7892 commit 9b91987
Show file tree
Hide file tree
Showing 5 changed files with 3,604 additions and 3,557 deletions.
110 changes: 110 additions & 0 deletions crates/csharp/src/csharp_ident.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
use heck::{ToLowerCamelCase, ToUpperCamelCase};

pub(crate) trait ToCSharpIdent: ToOwned {
fn csharp_keywords() -> Vec<&'static str>;
fn to_csharp_ident(&self) -> Self::Owned;
fn to_csharp_ident_upper(&self) -> Self::Owned;
}

impl ToCSharpIdent for str {
// Source: https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/
fn csharp_keywords() -> Vec<&'static str> {
vec![
"abstract",
"as",
"base",
"bool",
"break",
"byte",
"case",
"catch",
"char",
"checked",
"class",
"const",
"continue",
"decimal",
"default",
"delegate",
"do",
"double",
"else",
"enum",
"event",
"explicit",
"extern",
"false",
"finally",
"fixed",
"float",
"for",
"foreach",
"goto",
"if",
"implicit",
"in",
"int",
"interface",
"internal",
"is",
"lock",
"long",
"namespace",
"new",
"null",
"object",
"operator",
"out",
"override",
"params",
"private",
"protected",
"public",
"readonly",
"ref",
"return",
"sbyte",
"sealed",
"short",
"sizeof",
"stackalloc",
"static",
"string",
"struct",
"switch",
"this",
"throw",
"true",
"try",
"typeof",
"uint",
"ulong",
"unchecked",
"unsafe",
"ushort",
"using",
"virtual",
"void",
"volatile",
"while",
]
}

fn to_csharp_ident(&self) -> String {
// Escape C# keywords
if Self::csharp_keywords().contains(&self) {
format!("@{}", self)
} else {
self.to_lower_camel_case()
}
}

fn to_csharp_ident_upper(&self) -> String {
// Escape C# keywords
if Self::csharp_keywords().contains(&self) {
format!("@{}", self)
} else {
self.to_upper_camel_case()
}
}
}
Loading

0 comments on commit 9b91987

Please sign in to comment.