Skip to content

Commit ae936e2

Browse files
committed
Enforce normalized names in python enums values
1 parent f24df1f commit ae936e2

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

codegen/enums.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ macro_rules! write_fmt {
2121
/// ContactFFSilent => ContactFfSilent
2222
///
2323
/// If a string doesn't need to be updated, the original is returned
24-
fn normalize_caps<'a>(input: &'a str) -> Cow<'a, str> {
24+
pub fn normalize_caps(input: &str) -> Cow<'_, str> {
2525
let bytes = input.as_bytes();
2626
let mut i = 0;
2727

@@ -108,7 +108,7 @@ impl<'a> EnumBindGenerator<'a> {
108108
write_str!(self, " #[default]");
109109

110110
for (var_num, var_info) in self.variants {
111-
write_fmt!(self, " {} = {var_num},", var_info.name);
111+
write_fmt!(self, " {} = {var_num},", normalize_caps(&var_info.name));
112112
}
113113

114114
write_str!(self, "}");
@@ -121,12 +121,11 @@ impl<'a> EnumBindGenerator<'a> {
121121
write_str!(self, " match flat_t {");
122122

123123
for var_info in self.variants.values() {
124+
let var_name = normalize_caps(&var_info.name);
124125
write_fmt!(
125126
self,
126-
" flat::{}::{} => Self::{},",
127-
self.name,
128-
normalize_caps(&var_info.name),
129-
var_info.name,
127+
" flat::{}::{var_name} => Self::{var_name},",
128+
self.name
130129
);
131130
}
132131

@@ -142,12 +141,11 @@ impl<'a> EnumBindGenerator<'a> {
142141
write_str!(self, " match py_type {");
143142

144143
for var_info in self.variants.values() {
144+
let var_name = normalize_caps(&var_info.name);
145145
write_fmt!(
146146
self,
147-
" {}::{} => Self::{},",
147+
" {}::{var_name} => Self::{var_name},",
148148
self.name,
149-
var_info.name,
150-
normalize_caps(&var_info.name),
151149
);
152150
}
153151

@@ -171,7 +169,7 @@ impl<'a> EnumBindGenerator<'a> {
171169
self,
172170
" {} => Ok(Self::{}),",
173171
var_num.to_u64(),
174-
var_info.name
172+
normalize_caps(&var_info.name)
175173
);
176174
}
177175

codegen/pyi.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use planus_types::{
55
};
66
use std::{borrow::Cow, fs, io};
77

8-
use crate::structs::DEFAULT_OVERRIDES;
8+
use crate::{enums::normalize_caps, structs::DEFAULT_OVERRIDES};
99

1010
macro_rules! write_str {
1111
($self:ident, $s:expr) => {
@@ -67,7 +67,11 @@ pub fn generator(type_data: &IndexMap<AbsolutePath, Declaration>) -> io::Result<
6767
}
6868
DeclarationKind::Enum(info) => {
6969
for (var_val, var_info) in &info.variants {
70-
write_fmt!(file, " {} = {type_name}({var_val})", var_info.name);
70+
write_fmt!(
71+
file,
72+
" {} = {type_name}({var_val})",
73+
normalize_caps(&var_info.name)
74+
);
7175

7276
if !var_info.docstrings.docstrings.is_empty() {
7377
write_str!(file, " \"\"\"");

0 commit comments

Comments
 (0)