-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: James Sturtevant <[email protected]>
- Loading branch information
1 parent
f37bd95
commit 9a11c78
Showing
4 changed files
with
119 additions
and
116 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters