From 1bdb3a9da2b12cad41bf942ddd6bf33097a01470 Mon Sep 17 00:00:00 2001 From: Paolo Barbolini Date: Fri, 16 Jan 2026 18:42:56 +0100 Subject: [PATCH] Replace `reflector` with `heck` --- graph-codegen/Cargo.toml | 2 +- graph-codegen/src/api_types/method_macro.rs | 2 +- graph-codegen/src/api_types/mod_file_writer.rs | 2 +- graph-codegen/src/api_types/request_metadata.rs | 12 ++++++------ graph-codegen/src/lib.rs | 2 +- graph-codegen/src/macros/macro_queue_writer.rs | 12 ++++++------ graph-codegen/src/openapi/mod.rs | 4 ++-- graph-codegen/src/openapi/operation.rs | 12 ++++++++---- graph-codegen/src/openapi/path_item.rs | 2 +- graph-codegen/src/parser/request.rs | 4 ++-- graph-codegen/src/parser/resource.rs | 8 ++++---- graph-codegen/src/traits/request.rs | 6 +++--- graph-core/Cargo.toml | 2 +- graph-core/src/resource/resource_identity.rs | 10 +++++----- 14 files changed, 42 insertions(+), 38 deletions(-) diff --git a/graph-codegen/Cargo.toml b/graph-codegen/Cargo.toml index d7a34865..9b2d397f 100644 --- a/graph-codegen/Cargo.toml +++ b/graph-codegen/Cargo.toml @@ -16,7 +16,7 @@ anyhow = "1.0.69" bytes = "1" derive_builder = "0.12.0" from_as = "0.1" -Inflector = "0.11.4" +heck = "0.5" lazy_static = "1.4.0" rayon = "1.5.0" regex = "1" diff --git a/graph-codegen/src/api_types/method_macro.rs b/graph-codegen/src/api_types/method_macro.rs index ab287176..4b34b943 100644 --- a/graph-codegen/src/api_types/method_macro.rs +++ b/graph-codegen/src/api_types/method_macro.rs @@ -2,7 +2,7 @@ use crate::api_types::RequestTask; use crate::parser::HttpMethod; use crate::settings::{GeneratedMacroType, MethodMacroModifier}; use from_as::*; -use inflector::Inflector; +use heck::ToSnakeCase; use std::io::{Read, Write}; /// Represents the macro used for describing requests. This is the outer diff --git a/graph-codegen/src/api_types/mod_file_writer.rs b/graph-codegen/src/api_types/mod_file_writer.rs index d0934f72..cef0ad88 100644 --- a/graph-codegen/src/api_types/mod_file_writer.rs +++ b/graph-codegen/src/api_types/mod_file_writer.rs @@ -3,7 +3,7 @@ use from_as::*; use graph_core::resource::ResourceIdentity; use graph_error::{GraphFailure, GraphResult}; use graph_http::io_tools::create_dir; -use inflector::Inflector; +use heck::ToSnakeCase; use std::collections::HashSet; use std::fs::OpenOptions; use std::io::{Read, Write}; diff --git a/graph-codegen/src/api_types/request_metadata.rs b/graph-codegen/src/api_types/request_metadata.rs index b1a6eb28..3a45039b 100644 --- a/graph-codegen/src/api_types/request_metadata.rs +++ b/graph-codegen/src/api_types/request_metadata.rs @@ -2,13 +2,13 @@ use crate::api_types::metadata_modifier::ModifierMap; use crate::api_types::WriteConfiguration; use crate::api_types::{Metadata, MetadataModifier, MethodMacro, RequestClientList, RequestTask}; use crate::filter::Filter; -use crate::inflector::Inflector; use crate::macros::{MacroImplWriter, MacroQueueWriter}; use crate::openapi::{OpenApi, PathItem}; use crate::parser::HttpMethod; use crate::traits::{FilterMetadata, RequestParser, INTERNAL_PATH_ID}; use from_as::*; use graph_core::resource::ResourceIdentity; +use heck::{ToSnakeCase, ToUpperCamelCase}; use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; use std::io::{Read, Write}; use std::str::FromStr; @@ -75,16 +75,16 @@ impl RequestMetadata { self.parent = format!("{resource_id_string}Id"); self.original_parent = resource_id_string; } else { - self.parent = format!("{}Id", original_parent.to_pascal_case()); - self.original_parent = original_parent.to_pascal_case(); + self.parent = format!("{}Id", original_parent.to_upper_camel_case()); + self.original_parent = original_parent.to_upper_camel_case(); self.resource_identity = ResourceIdentity::from_str(&self.original_parent).ok(); } } pub fn transform_secondary_request(&mut self, operation_mapping: &str, original_parent: &str) { self.operation_mapping = operation_mapping.to_string(); - self.parent = original_parent.to_pascal_case(); - self.original_parent = original_parent.to_pascal_case(); + self.parent = original_parent.to_upper_camel_case(); + self.original_parent = original_parent.to_upper_camel_case(); self.resource_identity = ResourceIdentity::from_str(&self.original_parent).ok(); } @@ -150,7 +150,7 @@ impl Metadata for RequestMetadata { impl MetadataModifier for RequestMetadata { fn replace_operation_mapping(&mut self, replacement: &str) { self.operation_mapping = replacement.to_string(); - self.parent = replacement.to_pascal_case(); + self.parent = replacement.to_upper_camel_case(); } fn replace_operation_id(&mut self, replacement: &str) { diff --git a/graph-codegen/src/lib.rs b/graph-codegen/src/lib.rs index f64499ed..895029c5 100644 --- a/graph-codegen/src/lib.rs +++ b/graph-codegen/src/lib.rs @@ -1,6 +1,6 @@ #![recursion_limit = "1024"] #![allow(deprecated)] -pub extern crate inflector; +pub extern crate heck; #[macro_use] extern crate derive_builder; #[macro_use] diff --git a/graph-codegen/src/macros/macro_queue_writer.rs b/graph-codegen/src/macros/macro_queue_writer.rs index 961b31ae..97a67fbf 100644 --- a/graph-codegen/src/macros/macro_queue_writer.rs +++ b/graph-codegen/src/macros/macro_queue_writer.rs @@ -3,7 +3,6 @@ use crate::api_types::{ RequestTask, }; use crate::api_types::{ModWriteConfiguration, WriteConfiguration}; -use crate::inflector::Inflector; use crate::openapi::OpenApi; use crate::settings::{get_method_macro_modifiers, ResourceSettings}; use anyhow::anyhow; @@ -11,6 +10,7 @@ use bytes::{BufMut, BytesMut}; use from_as::*; use graph_core::resource::ResourceIdentity; use graph_http::io_tools::create_dir; +use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase}; use std::collections::{BTreeMap, BTreeSet, HashSet, VecDeque}; use std::fmt::Debug; use std::fmt::Write as _; @@ -360,9 +360,9 @@ pub trait MacroImplWriter { let ris: VecDeque<(String, ResourceIdentity)> = keys .iter() .map(|key| { - let key_id = key.to_pascal_case(); + let key_id = key.to_upper_camel_case(); if key_id.ends_with("Id") { - let key_id_stripped = key.to_camel_case()[..key.len() - 2].to_string(); + let key_id_stripped = key.to_lower_camel_case()[..key.len() - 2].to_string(); ( key_id.clone(), ResourceIdentity::from_str(key_id_stripped.as_str()) @@ -371,7 +371,7 @@ pub trait MacroImplWriter { } else { ( key_id.clone(), - ResourceIdentity::from_str(&key.to_camel_case()) + ResourceIdentity::from_str(&key.to_lower_camel_case()) .unwrap_or_else(|_| panic!("Unable to find variant for {key_id}")), ) } @@ -402,13 +402,13 @@ pub trait MacroImplWriter { let client_names: Vec = keys .iter() - .map(|name| format!("{}ApiClient", name.to_pascal_case())) + .map(|name| format!("{}ApiClient", name.to_upper_camel_case())) .collect(); // Build ApiClientLink enum to add the client being generated as a method link from one client // to another. This is for ease of use and doesnt always work for ever client name. for (name, _) in ris.iter() { - let client_name = format!("{}ApiClient", name.to_pascal_case()); + let client_name = format!("{}ApiClient", name.to_upper_camel_case()); if client_name.contains("Id") { let mut method_name = name.to_snake_case(); if method_name.ends_with("s_id") && !method_name.ends_with("es_id") { diff --git a/graph-codegen/src/openapi/mod.rs b/graph-codegen/src/openapi/mod.rs index 0071d994..de2f4dc8 100644 --- a/graph-codegen/src/openapi/mod.rs +++ b/graph-codegen/src/openapi/mod.rs @@ -62,7 +62,7 @@ use crate::traits::RequestParser; use from_as::*; use graph_error::GraphFailure; use graph_http::url::GraphUrl; -use inflector::Inflector; +use heck::ToUpperCamelCase; use rayon::prelude::*; use reqwest::Url; use serde_json::Value; @@ -305,7 +305,7 @@ impl OpenApi { .map(|(path, _path_item)| { path.split('/') .filter(|s| !s.trim().is_empty()) - .map(|s| s.to_pascal_case()) + .map(|s| s.to_upper_camel_case()) .take(1) .collect() }) diff --git a/graph-codegen/src/openapi/operation.rs b/graph-codegen/src/openapi/operation.rs index 5833afdb..00868a45 100644 --- a/graph-codegen/src/openapi/operation.rs +++ b/graph-codegen/src/openapi/operation.rs @@ -1,5 +1,4 @@ use crate::api_types::RequestMetadata; -use crate::inflector::Inflector; use crate::parser::HttpMethod; use crate::{ openapi::{ @@ -10,6 +9,7 @@ use crate::{ }; use from_as::*; use graph_core::resource::ResourceIdentity; +use heck::{ToLowerCamelCase, ToUpperCamelCase}; use std::str::FromStr; use std::{ collections::{HashMap, VecDeque}, @@ -158,13 +158,17 @@ impl Operation { if operation_mapping.contains('.') { let v: Vec<&str> = operation_mapping.split('.').collect(); - parent = v.last().map(|s| s.to_pascal_case()).unwrap_or_default(); + parent = v + .last() + .map(|s| s.to_upper_camel_case()) + .unwrap_or_default(); } else { - parent = operation_mapping.to_pascal_case(); + parent = operation_mapping.to_upper_camel_case(); } let original_parent = parent.clone(); - let resource_identity = ResourceIdentity::from_str(&original_parent.to_camel_case()).ok(); + let resource_identity = + ResourceIdentity::from_str(&original_parent.to_lower_camel_case()).ok(); RequestMetadata { has_body: self.has_body(), diff --git a/graph-codegen/src/openapi/path_item.rs b/graph-codegen/src/openapi/path_item.rs index 1aabc040..c02d7f4a 100644 --- a/graph-codegen/src/openapi/path_item.rs +++ b/graph-codegen/src/openapi/path_item.rs @@ -3,7 +3,7 @@ use crate::openapi::{EitherT, Operation, Parameter, Reference, Server}; use crate::parser::HttpMethod; use crate::traits::{PathMatcher, RequestParser}; use from_as::*; -use inflector::Inflector; +use heck::ToSnakeCase; use std::collections::{HashMap, HashSet}; use std::{ collections::VecDeque, diff --git a/graph-codegen/src/parser/request.rs b/graph-codegen/src/parser/request.rs index b935abf1..9ab0ade1 100644 --- a/graph-codegen/src/parser/request.rs +++ b/graph-codegen/src/parser/request.rs @@ -3,7 +3,7 @@ use crate::{ traits::{HashMapExt, RequestParser}, }; use from_as::*; -use inflector::Inflector; +use heck::ToLowerCamelCase; use std::{ collections::{ hash_set::{Difference, Iter}, @@ -366,7 +366,7 @@ impl RequestSet { vec.retain(|s| !s.is_empty()); if let Some(name) = vec.pop_front() { if !name.is_empty() { - names.push(name.to_camel_case()); + names.push(name.to_lower_camel_case()); } } } diff --git a/graph-codegen/src/parser/resource.rs b/graph-codegen/src/parser/resource.rs index 926eb5a9..cd9084b8 100644 --- a/graph-codegen/src/parser/resource.rs +++ b/graph-codegen/src/parser/resource.rs @@ -1,6 +1,6 @@ use crate::parser::RequestSet; use from_as::*; -use inflector::Inflector; +use heck::ToLowerCamelCase; use std::{ collections::{BTreeSet, HashMap, VecDeque}, convert::TryFrom, @@ -23,7 +23,7 @@ impl ResourceNames { pub fn sort(&mut self) { let mut v: Vec = self.to_vec(); - v = v.iter().map(|s| s.to_camel_case()).collect(); + v = v.iter().map(|s| s.to_lower_camel_case()).collect(); v.sort(); self.names = v.into_iter().collect(); } @@ -39,7 +39,7 @@ impl From> for ResourceNames { vec.retain(|s| !s.is_empty()); if let Some(name) = vec.pop_front() { if !name.is_empty() { - names.push(name.to_camel_case()); + names.push(name.to_lower_camel_case()); } } } @@ -73,7 +73,7 @@ impl From> for ResourceNames { fn from(map: HashMap) -> Self { let mut resource_names = ResourceNames::default(); for (name, _request_set) in map.iter() { - resource_names.names.insert(name.to_camel_case()); + resource_names.names.insert(name.to_lower_camel_case()); } resource_names } diff --git a/graph-codegen/src/traits/request.rs b/graph-codegen/src/traits/request.rs index f96ed289..335acf11 100644 --- a/graph-codegen/src/traits/request.rs +++ b/graph-codegen/src/traits/request.rs @@ -2,7 +2,7 @@ use crate::parser::error::ParseError; use crate::parser::{HttpMethod, Request}; use crate::traits::HashMapExt; use from_as::*; -use inflector::Inflector; +use heck::{ToSnakeCase, ToUpperCamelCase}; use regex::Regex; use std::collections::{HashMap, HashSet, VecDeque}; use std::io::{Read, Write}; @@ -511,9 +511,9 @@ impl RequestParser for &str { vec.retain(|l| !l.is_empty()); let first = vec.pop_front().unwrap(); let last = vec.pop_front().unwrap(); - map.entry_modify_insert(first.to_pascal_case(), last.to_pascal_case()); + map.entry_modify_insert(first.to_upper_camel_case(), last.to_upper_camel_case()); } else { - map.insert(link.to_pascal_case(), vec![]); + map.insert(link.to_upper_camel_case(), vec![]); } } diff --git a/graph-core/Cargo.toml b/graph-core/Cargo.toml index ffa446d4..9183b6ed 100644 --- a/graph-core/Cargo.toml +++ b/graph-core/Cargo.toml @@ -12,7 +12,7 @@ homepage = "https://github.com/sreeise/graph-rs-sdk" async-trait = "0.1.35" base64 = "0.21.0" dyn-clone = "1.0.14" -Inflector = "0.11.4" +heck = "0.5" http = { workspace = true } jsonwebtoken = "9.1.0" parking_lot = "0.12.1" diff --git a/graph-core/src/resource/resource_identity.rs b/graph-core/src/resource/resource_identity.rs index a6d5c38a..a611b6c8 100644 --- a/graph-core/src/resource/resource_identity.rs +++ b/graph-core/src/resource/resource_identity.rs @@ -1,5 +1,5 @@ use std::fmt::Display; -use inflector::Inflector; +use heck::{ToLowerCamelCase, ToSnakeCase, ToUpperCamelCase}; /// Comprises both top level and second level resources. /// These are not generated from OpenApi, except for top level resources, @@ -409,7 +409,7 @@ impl Display for ResourceIdentity { ResourceIdentity::DevicesRegisteredUsers => "registeredUsers".into(), ResourceIdentity::Custom => "".into(), - _ => self.as_ref().to_camel_case(), + _ => self.as_ref().to_lower_camel_case(), }; write!(f, "{}", str) } @@ -429,11 +429,11 @@ impl ResourceIdentity { } pub fn exact_camel_case(&self) -> String { - self.as_ref().to_camel_case() + self.as_ref().to_lower_camel_case() } pub fn exact_pascal_case(&self) -> String { - self.as_ref().to_pascal_case() + self.as_ref().to_upper_camel_case() } pub fn exact_snake_case(&self) -> String { @@ -533,6 +533,6 @@ pub enum TopLevelResource { impl Display for TopLevelResource { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "{}", self.as_ref().to_camel_case()) + write!(f, "{}", self.as_ref().to_lower_camel_case()) } }