Skip to content

Commit

Permalink
Move Ident code to own file
Browse files Browse the repository at this point in the history
Signed-off-by: James Sturtevant <[email protected]>
  • Loading branch information
jsturtevant committed Dec 19, 2024
1 parent f37bd95 commit 9a11c78
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 116 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()
}
}
}
3 changes: 2 additions & 1 deletion crates/csharp/src/function.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::csharp_ident::ToCSharpIdent;
use crate::interface::InterfaceGenerator;
use crate::{CSharp, ResourceInfo, ToCSharpIdent};
use crate::{CSharp, ResourceInfo};
use heck::ToUpperCamelCase;
use std::fmt::Write;
use std::mem;
Expand Down
3 changes: 2 additions & 1 deletion crates/csharp/src/interface.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::csharp_ident::ToCSharpIdent;
use crate::function::FunctionBindgen;
use crate::{CSharp, ResourceInfo, ToCSharpIdent};
use crate::{CSharp, ResourceInfo};
use heck::{ToShoutySnakeCase, ToUpperCamelCase};
use std::collections::{HashMap, HashSet};
use std::fmt::Write;
Expand Down
119 changes: 5 additions & 114 deletions crates/csharp/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use anyhow::Result;
use heck::{ToLowerCamelCase, ToUpperCamelCase};
use csharp_ident::ToCSharpIdent;
use heck::ToUpperCamelCase;
use indexmap::IndexMap;
use std::{
collections::{HashMap, HashSet},
fmt::Write,
iter, mem,
ops::Deref,
};
use wit_bindgen_core::{
abi::WasmType,
Direction,
};
use wit_bindgen_core::{abi::WasmType, Direction};
use wit_bindgen_core::{
uwrite,
wit_parser::{
Expand All @@ -20,6 +18,8 @@ use wit_bindgen_core::{
Files, InterfaceGenerator as _, WorldGenerator,
};
use wit_component::{StringEncoding, WitPrinter};

mod csharp_ident;
mod csproj;
mod function;
mod interface;
Expand Down Expand Up @@ -1027,115 +1027,6 @@ fn is_primitive(ty: &Type) -> bool {
)
}

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()
}
}
}

/// Group the specified functions by resource (or `None` for freestanding functions).
///
/// The returned map is constructed by iterating over `funcs`, then iterating over `all_resources`, thereby
Expand Down

0 comments on commit 9a11c78

Please sign in to comment.