Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c967555

Browse files
add expand path function
1 parent 3f2aa2a commit c967555

File tree

1 file changed

+14
-10
lines changed

1 file changed

+14
-10
lines changed

‎src/helper.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -193,14 +193,21 @@ mod file {
193193

194194
use crate::{cache::models::Problem, Error};
195195

196+
/// Substitutes fid and slug values into the given path template.
197+
/// Replaces: - ${fid} with problem.fid - ${slug} with the slug where '-' is replaced by '_',
198+
/// since some languages (e.g., Rust) don't support '-' in file names.
199+
/// # Example:
200+
/// ${fid}_${slug} -> 1.two-sum → 1_two_sum.py
201+
fn expand_path_template(path: &str, problem: &Problem) -> String {
202+
let safe_slug = problem.slug.replace("-", "_");
203+
path.replace("${fid}", &problem.fid.to_string())
204+
.replace("${slug}", &safe_slug)
205+
}
196206
/// Generate test cases path by fid
197207
pub fn test_cases_path(problem: &Problem) -> crate::Result<String> {
198208
let conf = crate::config::Config::locate()?;
199-
let mut path = format!("{}/{}.tests.dat", conf.storage.code()?, conf.code.pick);
200-
let safe_slug = problem.slug.replace("-", "_");
201-
path = path.replace("${fid}", &problem.fid.to_string());
202-
path = path.replace("${slug}", &safe_slug);
203-
Ok(path)
209+
let raw_path = format!("{}/{}.tests.dat", conf.storage.code()?, conf.code.pick);
210+
Ok(expand_path_template(&raw_path, problem))
204211
}
205212

206213
/// Generate code path by fid
@@ -211,17 +218,14 @@ mod file {
211218
lang = l.ok_or(Error::NoneError)?;
212219
}
213220

214-
let mut path = format!(
221+
let raw_path = format!(
215222
"{}/{}.{}",
216223
conf.storage.code()?,
217224
conf.code.pick,
218225
suffix(&lang)?,
219226
);
220-
let safe_slug = problem.slug.replace("-", "_");
221-
path = path.replace("${fid}", &problem.fid.to_string());
222-
path = path.replace("${slug}", &safe_slug);
223227

224-
Ok(path)
228+
Ok(expand_path_template(&raw_path, problem))
225229
}
226230

227231
/// Load python scripts

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /