Skip to content

Commit 61b8b57

Browse files
author
Antonio Bennett
committed
Enhance: Improve slug sanitization for valid Cargo package names
- Lowercase slugs and prefix with 'prob-' (e.g., 'prob-1_two_sum'). - Prevents Cargo.toml errors; applied to paths and package names. - Ensures cross-file consistency.
1 parent c6a904a commit 61b8b57

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

src/cmds/edit.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ use std::path::Path;
108108
let question: Question = qr?;
109109

110110
if *lang == "rust" {
111-
let sanitized_slug = problem.slug.replace(|c: char| !c.is_alphanumeric(), "_");
111+
let sanitized_slug = problem.slug.to_lowercase().replace(|c: char| !c.is_alphanumeric(), "_");
112112
let code_dir_str = format!("{}/{}-{}", conf.storage.code()?, problem.fid, sanitized_slug);
113113
let code_dir = Path::new(&code_dir_str);
114114
fs::create_dir_all(code_dir)?;
@@ -120,7 +120,7 @@ if *lang == "rust" {
120120
let cargo_path_str = format!("{}/Cargo.toml", code_dir_str);
121121
let cargo_path = Path::new(&cargo_path_str);
122122
if !cargo_path.exists() {
123-
let package_name = format!("leetcode-{}-{}", problem.fid, sanitized_slug);
123+
let package_name = format!("prob-{}-{}", problem.fid, sanitized_slug);
124124
let cargo_content = format!(
125125
r#"[package]
126126
name = "{}"

src/helper.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ pub fn test_cases_path(problem: &Problem) -> crate::Result<String> {
200200
let code_base = conf.storage.code()?;
201201

202202
let path = if lang == "rust" {
203-
let sanitized_slug = problem.slug.replace(|c: char| !c.is_alphanumeric(), "_");
203+
let sanitized_slug = problem.slug.to_lowercase().replace(|c: char| !c.is_alphanumeric(), "_");
204204
let subdir = format!("{}-{}/tests.dat", problem.fid, sanitized_slug);
205205
format!("{}/{}", code_base, subdir)
206206
} else {
@@ -224,7 +224,7 @@ pub fn code_path(problem: &Problem, l: Option<String>) -> crate::Result<String>
224224
let code_base = conf.storage.code()?;
225225

226226
let path = if lang == "rust" {
227-
let sanitized_slug = problem.slug.replace(|c: char| !c.is_alphanumeric(), "_");
227+
let sanitized_slug = problem.slug.to_lowercase().replace(|c: char| !c.is_alphanumeric(), "_");
228228
let subdir = format!("{}-{}/src/lib.rs", problem.fid, sanitized_slug);
229229
format!("{}/{}", code_base, subdir)
230230
} else {

0 commit comments

Comments
 (0)