Skip to content

Commit

Permalink
Fix and rename enum.
Browse files Browse the repository at this point in the history
  • Loading branch information
ralfbiedert committed Feb 1, 2025
1 parent 1c70924 commit 0ead025
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 640 deletions.
4 changes: 2 additions & 2 deletions crates/backend_c/src/converters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ pub fn primitive_to_typename(x: PrimitiveType) -> String {
}

pub fn enum_to_typename(g: &Interop, x: &EnumType) -> String {
format!("{}{}", g.prefix, x.rust_name()).to_naming_style(&g.type_naming)
format!("{}{}", g.prefix, x.rust_name()).to_naming_style(&g.enum_variant_naming)
}

pub fn enum_variant_to_name(g: &Interop, the_enum: &EnumType, x: &Variant) -> String {
format!("{}{}_{}", g.prefix, the_enum.rust_name().to_naming_style(&g.type_naming), x.name()).to_naming_style(&g.enum_variant_naming)
format!("{}{}_{}", g.prefix, the_enum.rust_name().to_naming_style(&g.enum_variant_naming), x.name()).to_naming_style(&g.enum_variant_naming)
}

pub fn opaque_to_typename(g: &Interop, x: &OpaqueType) -> String {
Expand Down
40 changes: 20 additions & 20 deletions crates/backend_c/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,41 @@ pub enum Indentation {

/// Naming style used in generated C code
#[derive(Clone, Debug, PartialEq, Eq, Default)]
pub enum TypeNames {
pub enum NameCase {
/// Names all in lowercase without spacing e.g. 'thetypename'
Lowercase,
Lower,
/// Names all in uppercase without spacing e.g. 'THETYPENAME'
#[default]
Uppercase,
Upper,
/// Names in mixed case starting with lowercase without spacing e.g. 'theTypeName'
LowerCamelCase,
LowerCamel,
/// Names in mixed case starting with uppercase without spacing e.g. '`TheTypeName`'
UpperCamelCase,
UpperCamel,
/// Names in lower case with '_' as spacing e.g. '`the_type_name`'
SnakeCase,
Snake,
/// Names in upper case with '_' as spacing e.g. '`THE_TYPE_NAME`'
ShoutySnakeCase,
ShoutySnake,
}

pub trait ToNamingStyle {
fn to_naming_style(&self, style: &TypeNames) -> String;
fn to_naming_style(&self, style: &NameCase) -> String;
}

impl ToNamingStyle for String {
fn to_naming_style(&self, style: &TypeNames) -> String {
fn to_naming_style(&self, style: &NameCase) -> String {
self.as_str().to_naming_style(style)
}
}

impl ToNamingStyle for &str {
fn to_naming_style(&self, style: &TypeNames) -> String {
fn to_naming_style(&self, style: &NameCase) -> String {
match style {
TypeNames::Lowercase => self.to_lowercase(),
TypeNames::Uppercase => self.to_uppercase(),
TypeNames::LowerCamelCase => self.to_lower_camel_case(),
TypeNames::UpperCamelCase => self.to_upper_camel_case(),
TypeNames::SnakeCase => self.to_snake_case(),
TypeNames::ShoutySnakeCase => self.to_shouty_snake_case(),
NameCase::Lower => self.to_lowercase(),
NameCase::Upper => self.to_uppercase(),
NameCase::LowerCamel => self.to_lower_camel_case(),
NameCase::UpperCamel => self.to_upper_camel_case(),
NameCase::Snake => self.to_snake_case(),
NameCase::ShoutySnake => self.to_shouty_snake_case(),
}
}
}
Expand Down Expand Up @@ -114,13 +114,13 @@ pub struct Interop {
/// How to add code documentation
documentation: Documentation,
/// How to convert type names
pub(crate) type_naming: TypeNames,
pub(crate) type_naming: NameCase,
/// How to convert enum variant names
pub(crate) enum_variant_naming: TypeNames,
pub(crate) enum_variant_naming: NameCase,
/// How to convert const names
pub(crate) const_naming: TypeNames,
pub(crate) const_naming: NameCase,
/// How to convert function parameter names
function_parameter_naming: TypeNames,
function_parameter_naming: NameCase,
/// How to emit functions
function_style: Functions,
pub(crate) inventory: Inventory,
Expand Down
2 changes: 1 addition & 1 deletion crates/backend_c/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,4 @@ mod docs;
mod generator;

pub use docs::DocGenerator;
pub use generator::{Documentation, Functions, Indentation, Interop, InteropBuilder, TypeNames};
pub use generator::{Documentation, Functions, Indentation, Interop, InteropBuilder, NameCase};
Loading

0 comments on commit 0ead025

Please sign in to comment.