Skip to content

Commit 25100ab

Browse files
committed
gnd: Use Inflector to turn names to kebab_case
1 parent 67f3017 commit 25100ab

File tree

5 files changed

+12
-24
lines changed

5 files changed

+12
-24
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ graphman-server = { path = "./server/graphman" }
6767
graphman = { path = "./core/graphman" }
6868
graphman-store = { path = "./core/graphman_store" }
6969
graphql-tools = "0.5.0"
70+
Inflector = "0.11.3"
7071
itertools = "0.14.0"
7172
lazy_static = "1.5.0"
7273
prost = "0.14"

gnd/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ graphql-parser = "0.4"
5555
regex = "1"
5656
ethabi = "17.2"
5757
serde_yaml = "0.9"
58+
Inflector = { workspace = true }
5859

5960
# Migrations
6061
semver = "1"

gnd/src/commands/add.rs

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::path::{Path, PathBuf};
88

99
use anyhow::{anyhow, Context, Result};
1010
use clap::Parser;
11+
use inflector::Inflector;
1112
use serde_json::Value as JsonValue;
1213

1314
use crate::formatter::format_typescript;
@@ -356,7 +357,7 @@ fn add_mapping_file(project_dir: &Path, contract_name: &str, events: &[EventInfo
356357
let src_dir = project_dir.join("src");
357358
fs::create_dir_all(&src_dir).context("Failed to create src directory")?;
358359

359-
let mapping_file = src_dir.join(format!("{}.ts", to_kebab_case(contract_name)));
360+
let mapping_file = src_dir.join(format!("{}.ts", contract_name.to_kebab_case()));
360361

361362
if mapping_file.exists() {
362363
step(
@@ -448,22 +449,6 @@ fn generate_event_handler(event: &EventInfo) -> String {
448449
)
449450
}
450451

451-
/// Convert a string to kebab-case.
452-
fn to_kebab_case(s: &str) -> String {
453-
let mut result = String::new();
454-
for (i, c) in s.chars().enumerate() {
455-
if c.is_uppercase() {
456-
if i > 0 {
457-
result.push('-');
458-
}
459-
result.push(c.to_lowercase().next().unwrap_or(c));
460-
} else {
461-
result.push(c);
462-
}
463-
}
464-
result
465-
}
466-
467452
/// Update the manifest with the new data source.
468453
fn update_manifest(
469454
manifest_path: &Path,
@@ -555,7 +540,7 @@ fn update_manifest(
555540
);
556541
mapping.insert(
557542
serde_yaml::Value::String("file".to_string()),
558-
serde_yaml::Value::String(format!("./src/{}.ts", to_kebab_case(contract_name))),
543+
serde_yaml::Value::String(format!("./src/{}.ts", contract_name.to_kebab_case())),
559544
);
560545

561546
// Build the data source
@@ -651,11 +636,11 @@ mod tests {
651636

652637
#[test]
653638
fn test_to_kebab_case() {
654-
assert_eq!(to_kebab_case("MyContract"), "my-contract");
655-
assert_eq!(to_kebab_case("SimpleToken"), "simple-token");
656-
assert_eq!(to_kebab_case("Contract"), "contract");
657-
assert_eq!(to_kebab_case("contract"), "contract");
658-
assert_eq!(to_kebab_case("ERC20Token"), "e-r-c20-token");
639+
assert_eq!("MyContract".to_kebab_case(), "my-contract");
640+
assert_eq!("SimpleToken".to_kebab_case(), "simple-token");
641+
assert_eq!("Contract".to_kebab_case(), "contract");
642+
assert_eq!("contract".to_kebab_case(), "contract");
643+
assert_eq!("ERC20Token".to_kebab_case(), "erc20-token");
659644
}
660645

661646
#[test]

store/postgres/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ diesel_migrations = { workspace = true }
1818
fallible-iterator = "0.3.0"
1919
graph = { path = "../../graph" }
2020
graphman-store = { workspace = true }
21-
Inflector = "0.11.3"
21+
Inflector = { workspace = true }
2222
lazy_static = "1.5"
2323
lru_time_cache = "0.11"
2424
postgres = "0.19.1"

0 commit comments

Comments
 (0)