-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenums.rs
More file actions
165 lines (137 loc) · 4.54 KB
/
enums.rs
File metadata and controls
165 lines (137 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use indexmap::IndexMap;
use planus_types::intermediate::{EnumVariant, IntegerLiteral};
use std::borrow::Cow;
macro_rules! write_str {
($self:ident, $s:expr) => {
$self.file_contents.push(Cow::Borrowed($s))
};
}
macro_rules! write_fmt {
($self:ident, $($arg:tt)*) => {
$self.file_contents.push(Cow::Owned(format!($($arg)*)))
};
}
/// Examples of what this function does:
/// FriendlyFire => FriendlyFire
/// ContactFF => ContactFf
/// ContactSilent => ContactSilent
/// ContactFFSilent => ContactFfSilent
///
/// If a string doesn't need to be updated, the original is returned
pub fn normalize_caps(input: &str) -> Cow<'_, str> {
let bytes = input.as_bytes();
let mut i = 0;
// check if changes need to be made
// if a change is be needed,
// `i` will be the location of where we need to start
while i < bytes.len() - 1 {
if bytes[i].is_ascii_uppercase() && bytes[i + 1].is_ascii_uppercase() {
if i + 2 == bytes.len() {
break;
}
if bytes[i + 2].is_ascii_uppercase() {
break;
}
}
i += 1;
}
if i == bytes.len() - 1 {
// no changes need to be made - return the original
return Cow::Borrowed(input);
}
// changes must be made, `i` stores the location of the first change
let mut result = String::with_capacity(bytes.len());
result.push_str(&input[..i]);
let mut chars = input.chars().skip(i);
while let Some(mut char) = chars.next() {
let mut num_upper = 0;
loop {
let Some(next_char) = chars.next() else {
result.push(char.to_ascii_lowercase());
break;
};
if next_char.is_ascii_uppercase() {
num_upper += 1;
result.push(if num_upper == 1 {
char
} else {
char.to_ascii_lowercase()
});
char = next_char;
} else {
result.push(char);
result.push(next_char);
break;
}
}
}
Cow::Owned(result)
}
pub struct EnumBindGenerator<'a> {
name: &'a str,
variants: &'a IndexMap<IntegerLiteral, EnumVariant>,
file_contents: Vec<Cow<'static, str>>,
}
impl<'a> EnumBindGenerator<'a> {
pub fn new(name: &'a str, variants: &'a IndexMap<IntegerLiteral, EnumVariant>) -> Self {
Self {
name,
variants,
file_contents: Vec::new(),
}
}
fn generate_new_method(&mut self) {
write_str!(self, " #[new]");
assert!(u8::try_from(self.variants.len()).is_ok());
write_str!(self, " #[pyo3(signature = (value=Default::default()))]");
write_str!(self, " pub fn new(value: u8) -> PyResult<Self> {");
write_str!(self, " match value {");
for (var_num, var_info) in self.variants {
write_fmt!(
self,
" {} => Ok(Self::{}),",
var_num.to_u64(),
normalize_caps(&var_info.name)
);
}
write_str!(
self,
" v => Err(PyValueError::new_err(format!(\"Unknown value of {v}\"))),"
);
write_str!(self, " }");
write_str!(self, " }");
}
fn generate_str_method(&mut self) {
write_str!(self, " pub fn __str__(&self) -> String {");
write_str!(self, " self.__repr__()");
write_str!(self, " }");
}
fn generate_repr_method(&mut self) {
write_str!(self, " pub fn __repr__(&self) -> String {");
write_fmt!(self, " format!(\"{}.{{self:?}}\")", self.name);
write_str!(self, " }");
}
fn generate_py_methods(&mut self) {
write_str!(self, "#[pymethods]");
write_fmt!(self, "impl {} {{", self.name);
self.generate_new_method();
write_str!(self, "");
self.generate_str_method();
write_str!(self, "");
self.generate_repr_method();
write_str!(self, "}");
write_str!(self, "");
}
pub fn generate_binds(mut self) -> Vec<Cow<'static, str>> {
write_str!(self, "use crate::flat;");
write_str!(
self,
"use pyo3::{PyResult, exceptions::PyValueError, pyclass, pymethods};"
);
write_str!(self, "");
write_fmt!(self, "pub use flat::{};", self.name);
write_str!(self, "");
self.generate_py_methods();
self.file_contents
}
}