From f1f9e2e93850713908f4e6494808a07f3b253108 Mon Sep 17 00:00:00 2001 From: "Adam H. Leventhal" Date: Sun, 16 Jan 2022 09:09:30 -0800 Subject: [PATCH] improve santization --- Cargo.lock | 9 ++++-- progenitor-impl/Cargo.toml | 6 ++-- progenitor-impl/src/lib.rs | 57 +++++++++++++++++++++++++------------- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index db56f1b..e25c71c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -715,8 +715,10 @@ dependencies = [ "schemars", "serde", "serde_json", + "syn", "thiserror", "typify", + "unicode-xid", ] [[package]] @@ -1154,7 +1156,7 @@ checksum = "59547bce71d9c38b83d9c0e92b6066c4253371f15005def0c30d9657f50c7642" [[package]] name = "typify" version = "0.0.6-dev" -source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02" +source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09" dependencies = [ "typify-impl", "typify-macro", @@ -1163,7 +1165,7 @@ dependencies = [ [[package]] name = "typify-impl" version = "0.0.6-dev" -source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02" +source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09" dependencies = [ "convert_case", "log", @@ -1174,12 +1176,13 @@ dependencies = [ "serde_json", "syn", "thiserror", + "unicode-xid", ] [[package]] name = "typify-macro" version = "0.0.6-dev" -source = "git+https://github.com/oxidecomputer/typify#a45d3058ec748c7040988700a003e4ef9aac6f02" +source = "git+https://github.com/oxidecomputer/typify#9afa917671b29fc231bc9ce304e041bdd685af09" dependencies = [ "proc-macro2", "quote", diff --git a/progenitor-impl/Cargo.toml b/progenitor-impl/Cargo.toml index a4d38b8..f1126e9 100644 --- a/progenitor-impl/Cargo.toml +++ b/progenitor-impl/Cargo.toml @@ -16,11 +16,13 @@ proc-macro2 = "1.0" quote = "1.0" regex = "1.5" rustfmt-wrapper = "0.1" -schemars = { version = "0.8", features = [ "chrono", "uuid" ] } -serde = { version = "1.0", features = [ "derive" ] } +schemars = { version = "0.8", features = ["chrono", "uuid"] } +serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" +syn = { version = "1.0", features = ["parsing"] } thiserror = "1.0" typify = { git = "https://github.com/oxidecomputer/typify" } +unicode-xid = "0.2" [dev-dependencies] expectorate = "1.0" diff --git a/progenitor-impl/src/lib.rs b/progenitor-impl/src/lib.rs index df68840..a23646d 100644 --- a/progenitor-impl/src/lib.rs +++ b/progenitor-impl/src/lib.rs @@ -14,6 +14,7 @@ use quote::{format_ident, quote, ToTokens}; use template::PathTemplate; use thiserror::Error; use typify::{TypeId, TypeSpace}; +use unicode_xid::UnicodeXID; use crate::to_schema::ToSchema; @@ -275,10 +276,10 @@ impl Generator { let nam = parameter_data.name.clone(); let schema = parameter_data.schema()?.to_schema(); - let name = format!( - "{}{}", - sanitize(operation_id, Case::Pascal), - sanitize(&nam, Case::Pascal), + + let name = sanitize( + &format!("{}-{}", operation_id, nam), + Case::Pascal, ); let typ = self .type_space @@ -302,13 +303,13 @@ impl Generator { let nam = parameter_data.name.clone(); let mut schema = parameter_data.schema()?.to_schema(); - let name = format!( - "{}{}", - sanitize( + let name = sanitize( + &format!( + "{}-{}", operation.operation_id.as_ref().unwrap(), - Case::Pascal + nam ), - sanitize(&nam, Case::Pascal), + Case::Pascal, ); if !parameter_data.required { @@ -344,12 +345,12 @@ impl Generator { if let Some(s) = &mt.schema { let schema = s.to_schema(); - let name = format!( - "{}Body", - sanitize( + let name = sanitize( + &format!( + "{}-body", operation.operation_id.as_ref().unwrap(), - Case::Pascal - ) + ), + Case::Pascal, ); let typ = self .type_space @@ -449,12 +450,12 @@ impl Generator { let typ = if let Some(schema) = &mt.schema { let schema = schema.to_schema(); - let name = format!( - "{}Response", - sanitize( + let name = sanitize( + &format!( + "{}-response", operation.operation_id.as_ref().unwrap(), - Case::Pascal - ) + ), + Case::Pascal, ); self.type_space .add_type_with_name(&schema, Some(name))? @@ -1244,5 +1245,21 @@ impl ComponentLookup for Schema { } fn sanitize(input: &str, case: Case) -> String { - input.replace('/', "-").to_case(case) + let out = input + .replace("'", "") + .replace(|c: char| !c.is_xid_continue(), "-") + .to_case(case); + + let out = match out.chars().next() { + None => "x".to_case(case), + Some(c) if c.is_xid_start() => out, + Some(_) => format!("_{}", out), + }; + + // Make sure the string is a valid Rust identifier. + if syn::parse_str::(&out).is_ok() { + out + } else { + format!("{}_", out) + } }