Skip to content

Commit 5f832bb

Browse files
authored
add an accept_as_ident function (#973)
1 parent 21e1e7c commit 5f832bb

5 files changed

Lines changed: 52 additions & 1 deletion

File tree

typify-impl/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ use type_entry::{
2020

2121
use crate::util::{sanitize, Case};
2222

23+
pub use crate::util::accept_as_ident;
24+
2325
#[cfg(test)]
2426
mod test_util;
2527

typify-impl/src/util.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -797,13 +797,35 @@ pub(crate) fn sanitize(input: &str, case: Case) -> String {
797797
};
798798

799799
// Make sure the string is a valid Rust identifier.
800-
if syn::parse_str::<syn::Ident>(&out).is_ok() {
800+
if accept_as_ident(&out) {
801801
out
802802
} else {
803803
format!("{}_", out)
804804
}
805805
}
806806

807+
/// Return true if the string is a valid Rust identifier.
808+
///
809+
/// If this function returns false, typify adds a trailing underscore to it. For
810+
/// example, `fn` becomes `fn_`.
811+
pub fn accept_as_ident(ident: &str) -> bool {
812+
// Adapted from https://docs.rs/syn/2.0.114/src/syn/ident.rs.html#60-74. The
813+
// main change is adding `gen` to the list.
814+
match ident {
815+
"_" |
816+
// Based on https://doc.rust-lang.org/1.65.0/reference/keywords.html
817+
"abstract" | "as" | "async" | "await" | "become" | "box" | "break" |
818+
"const" | "continue" | "crate" | "do" | "dyn" | "else" | "enum" |
819+
"extern" | "false" | "final" | "fn" | "for" | "gen" | "if" | "impl" |
820+
"in" | "let" | "loop" | "macro" | "match" | "mod" | "move" | "mut" |
821+
"override" | "priv" | "pub" | "ref" | "return" | "Self" | "self" |
822+
"static" | "struct" | "super" | "trait" | "true" | "try" | "type" |
823+
"typeof" | "unsafe" | "unsized" | "use" | "virtual" | "where" |
824+
"while" | "yield" => false,
825+
_ => true,
826+
}
827+
}
828+
807829
pub(crate) fn recase(input: &str, case: Case) -> (String, Option<String>) {
808830
let new = sanitize(input, case);
809831
let rename = if new == input {
@@ -1038,6 +1060,8 @@ mod tests {
10381060
fn test_sanitize() {
10391061
assert_eq!(sanitize("type", Case::Snake), "type_");
10401062
assert_eq!(sanitize("ref", Case::Snake), "ref_");
1063+
assert_eq!(sanitize("gen", Case::Snake), "gen_");
1064+
assert_eq!(sanitize("gen", Case::Pascal), "Gen");
10411065
assert_eq!(sanitize("+1", Case::Snake), "plus1");
10421066
assert_eq!(sanitize("-1", Case::Snake), "minus1");
10431067
assert_eq!(sanitize("@timestamp", Case::Pascal), "Timestamp");

typify/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@
148148
149149
#![deny(missing_docs)]
150150

151+
pub use typify_impl::accept_as_ident;
151152
pub use typify_impl::CrateVers;
152153
pub use typify_impl::Error;
153154
pub use typify_impl::Type;

typify/tests/schemas/rust-collisions.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,9 @@
261261
"for": {
262262
"type": "string"
263263
},
264+
"gen": {
265+
"type": "string"
266+
},
264267
"if": {
265268
"type": "string"
266269
},
@@ -391,6 +394,7 @@
391394
"false",
392395
"fn",
393396
"for",
397+
"gen",
394398
"if",
395399
"impl",
396400
"in",

typify/tests/schemas/rust-collisions.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,7 @@ impl Pin {
710710
#[doc = " \"final\","]
711711
#[doc = " \"fn\","]
712712
#[doc = " \"for\","]
713+
#[doc = " \"gen\","]
713714
#[doc = " \"if\","]
714715
#[doc = " \"impl\","]
715716
#[doc = " \"in\","]
@@ -800,6 +801,9 @@ impl Pin {
800801
#[doc = " \"for\": {"]
801802
#[doc = " \"type\": \"string\""]
802803
#[doc = " },"]
804+
#[doc = " \"gen\": {"]
805+
#[doc = " \"type\": \"string\""]
806+
#[doc = " },"]
803807
#[doc = " \"if\": {"]
804808
#[doc = " \"type\": \"string\""]
805809
#[doc = " },"]
@@ -937,6 +941,8 @@ pub struct RustKeywordMonster {
937941
pub fn_: ::std::string::String,
938942
#[serde(rename = "for")]
939943
pub for_: ::std::string::String,
944+
#[serde(rename = "gen")]
945+
pub gen_: ::std::string::String,
940946
#[serde(rename = "if")]
941947
pub if_: ::std::string::String,
942948
#[serde(rename = "impl")]
@@ -2147,6 +2153,7 @@ pub mod builder {
21472153
final_: ::std::result::Result<::std::string::String, ::std::string::String>,
21482154
fn_: ::std::result::Result<::std::string::String, ::std::string::String>,
21492155
for_: ::std::result::Result<::std::string::String, ::std::string::String>,
2156+
gen_: ::std::result::Result<::std::string::String, ::std::string::String>,
21502157
if_: ::std::result::Result<::std::string::String, ::std::string::String>,
21512158
impl_: ::std::result::Result<::std::string::String, ::std::string::String>,
21522159
in_: ::std::result::Result<::std::string::String, ::std::string::String>,
@@ -2201,6 +2208,7 @@ pub mod builder {
22012208
final_: Err("no value supplied for final_".to_string()),
22022209
fn_: Err("no value supplied for fn_".to_string()),
22032210
for_: Err("no value supplied for for_".to_string()),
2211+
gen_: Err("no value supplied for gen_".to_string()),
22042212
if_: Err("no value supplied for if_".to_string()),
22052213
impl_: Err("no value supplied for impl_".to_string()),
22062214
in_: Err("no value supplied for in_".to_string()),
@@ -2426,6 +2434,16 @@ pub mod builder {
24262434
.map_err(|e| format!("error converting supplied value for for_: {e}"));
24272435
self
24282436
}
2437+
pub fn gen_<T>(mut self, value: T) -> Self
2438+
where
2439+
T: ::std::convert::TryInto<::std::string::String>,
2440+
T::Error: ::std::fmt::Display,
2441+
{
2442+
self.gen_ = value
2443+
.try_into()
2444+
.map_err(|e| format!("error converting supplied value for gen_: {e}"));
2445+
self
2446+
}
24292447
pub fn if_<T>(mut self, value: T) -> Self
24302448
where
24312449
T: ::std::convert::TryInto<::std::string::String>,
@@ -2762,6 +2780,7 @@ pub mod builder {
27622780
final_: value.final_?,
27632781
fn_: value.fn_?,
27642782
for_: value.for_?,
2783+
gen_: value.gen_?,
27652784
if_: value.if_?,
27662785
impl_: value.impl_?,
27672786
in_: value.in_?,
@@ -2818,6 +2837,7 @@ pub mod builder {
28182837
final_: Ok(value.final_),
28192838
fn_: Ok(value.fn_),
28202839
for_: Ok(value.for_),
2840+
gen_: Ok(value.gen_),
28212841
if_: Ok(value.if_),
28222842
impl_: Ok(value.impl_),
28232843
in_: Ok(value.in_),

0 commit comments

Comments
 (0)