-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathrename.rs
More file actions
184 lines (160 loc) · 6.78 KB
/
Copy pathrename.rs
File metadata and controls
184 lines (160 loc) · 6.78 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::borrow::Cow;
use std::str::FromStr;
/// The type of identifier to be renamed.
#[derive(Debug, Clone, Copy)]
pub enum IdentifierType<'a> {
StructMember,
EnumVariant { prefix: &'a str },
FunctionArg,
Type,
Enum,
}
impl IdentifierType<'_> {
fn to_str(self) -> &'static str {
match self {
IdentifierType::StructMember => "m",
IdentifierType::EnumVariant { .. } => "",
IdentifierType::FunctionArg => "a",
IdentifierType::Type => "",
IdentifierType::Enum => "",
}
}
}
/// A rule to apply to an identifier when generating bindings.
#[derive(Debug, Clone, Default)]
pub enum RenameRule {
/// Do not apply any renaming. The default.
#[default]
None,
/// Converts the identifier to PascalCase and adds a context dependent prefix
GeckoCase,
/// Converts the identifier to lower case.
LowerCase,
/// Converts the identifier to upper case.
UpperCase,
/// Converts the identifier to PascalCase.
PascalCase,
/// Converts the identifier to camelCase.
CamelCase,
/// Converts the identifier to snake_case.
SnakeCase,
/// Converts the identifier to SCREAMING_SNAKE_CASE.
ScreamingSnakeCase,
/// Converts the identifier to SCREAMING_SNAKE_CASE and prefixes enum variants
/// with the enum name.
QualifiedScreamingSnakeCase,
/// Adds a given prefix
Prefix(String),
/// Adds a given prefix and converts the identifier to a specific case,
/// The prefix is applied before the conversion.
/// For example:
/// - `prefix:Foo + CamelCase` applied to `bar` with `CamelCase` would result in `FooBar`.
/// - `prefix:FOO_ + ScreamingSnakeCase` applied to `Bar` with `ScreamingSnakeCase` would result in `FOO_BAR`.
PrefixWithConversion(String, Box<Self>),
}
impl RenameRule {
pub(crate) fn not_none(&self) -> Option<&Self> {
match self {
RenameRule::None => None,
other => Some(other),
}
}
/// Applies the rename rule to a string
pub fn apply<'a>(&self, text: &'a str, context: IdentifierType) -> Cow<'a, str> {
use heck::*;
if text.is_empty() {
return Cow::Borrowed(text);
}
Cow::Owned(match self {
RenameRule::None => return Cow::Borrowed(text),
RenameRule::GeckoCase => context.to_str().to_owned() + &text.to_upper_camel_case(),
RenameRule::LowerCase => text.to_lowercase(),
RenameRule::UpperCase => text.to_uppercase(),
RenameRule::PascalCase => text.to_pascal_case(),
RenameRule::CamelCase => text.to_lower_camel_case(),
RenameRule::SnakeCase => text.to_snake_case(),
RenameRule::ScreamingSnakeCase => text.to_shouty_snake_case(),
RenameRule::QualifiedScreamingSnakeCase => {
let mut result = String::new();
if let IdentifierType::EnumVariant { prefix } = context {
result.push_str(
&RenameRule::ScreamingSnakeCase.apply(prefix, IdentifierType::Enum),
);
result.push('_');
}
result.push_str(&RenameRule::ScreamingSnakeCase.apply(text, context));
result
}
RenameRule::Prefix(prefix) => prefix.to_owned() + text,
RenameRule::PrefixWithConversion(prefix, rule) => {
let converted = rule.apply(text, context);
prefix.to_owned() + &converted
}
})
}
}
impl FromStr for RenameRule {
type Err = String;
fn from_str(s: &str) -> Result<RenameRule, Self::Err> {
const PREFIX: &str = "prefix:";
const PREFIX_LEN: usize = PREFIX.len();
match s {
"none" => Ok(RenameRule::None),
"None" => Ok(RenameRule::None),
"mGeckoCase" => Ok(RenameRule::GeckoCase),
"GeckoCase" => Ok(RenameRule::GeckoCase),
"gecko_case" => Ok(RenameRule::GeckoCase),
"lowercase" => Ok(RenameRule::LowerCase),
"LowerCase" => Ok(RenameRule::LowerCase),
"lower_case" => Ok(RenameRule::LowerCase),
"UPPERCASE" => Ok(RenameRule::UpperCase),
"UpperCase" => Ok(RenameRule::UpperCase),
"upper_case" => Ok(RenameRule::UpperCase),
"PascalCase" => Ok(RenameRule::PascalCase),
"pascal_case" => Ok(RenameRule::PascalCase),
"camelCase" => Ok(RenameRule::CamelCase),
"CamelCase" => Ok(RenameRule::CamelCase),
"camel_case" => Ok(RenameRule::CamelCase),
"snake_case" => Ok(RenameRule::SnakeCase),
"SnakeCase" => Ok(RenameRule::SnakeCase),
"SCREAMING_SNAKE_CASE" => Ok(RenameRule::ScreamingSnakeCase),
"ScreamingSnakeCase" => Ok(RenameRule::ScreamingSnakeCase),
"screaming_snake_case" => Ok(RenameRule::ScreamingSnakeCase),
"QUALIFIED_SCREAMING_SNAKE_CASE" => Ok(RenameRule::QualifiedScreamingSnakeCase),
"QualifiedScreamingSnakeCase" => Ok(RenameRule::QualifiedScreamingSnakeCase),
"qualified_screaming_snake_case" => Ok(RenameRule::QualifiedScreamingSnakeCase),
s if s.starts_with(PREFIX) => {
if s.contains('+') {
let parts: Vec<&str> = s.split('+').collect();
if parts.len() != 2 {
return Err(format!(
"Invalid RenameRule: '{s}', expected 'prefix:... + Conversion'."
));
}
let prefix = parts[0][PREFIX_LEN..].trim().to_string();
let convert = parts[1].trim();
if convert.is_empty() {
return Err(format!(
"Invalid RenameRule: '{s}', no conversion specified."
));
}
let convert_rule = RenameRule::from_str(convert)?;
if let RenameRule::Prefix(_) = convert_rule {
return Err(format!("Invalid conversion rule: '{convert}', Prefix with conversion must not be a prefix."));
}
Ok(RenameRule::PrefixWithConversion(
prefix,
Box::new(convert_rule),
))
} else {
Ok(RenameRule::Prefix(s[PREFIX_LEN..].to_string()))
}
}
_ => Err(format!("Unrecognized RenameRule: '{s}'.")),
}
}
}
deserialize_enum_str!(RenameRule);