acr-cli 0.4.2

A CLI tool for AtCoder competitive programming in Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
pub mod generator;
pub mod testcase;

use std::path::PathBuf;

use anyhow::Context;

/// Context for the current problem directory.
#[derive(Debug)]
pub struct ProblemContext {
    pub contest_id: String,
    pub problem_alphabet: String,
    pub task_screen_name: String,
    pub problem_dir: PathBuf,
    pub problem_url: String,
}

/// Context for a contest workspace directory.
#[derive(Debug)]
pub struct ContestContext {
    pub contest_id: String,
    pub contest_dir: PathBuf,
}

/// The detected context based on the current working directory.
pub enum CurrentContext {
    ProblemDir(ProblemContext),
    ContestDir(ContestContext),
    Outside,
}

/// Detect the current context from the working directory.
pub fn detect_current_context() -> CurrentContext {
    if let Ok(ctx) = detect_problem_dir() {
        CurrentContext::ProblemDir(ctx)
    } else if let Ok(ctx) = detect_contest_dir() {
        CurrentContext::ContestDir(ctx)
    } else {
        CurrentContext::Outside
    }
}

/// Resolve a problem context based on the current context and an optional problem identifier.
/// Used by test and submit commands.
pub fn require_problem_context(
    current: CurrentContext,
    problem: Option<&str>,
) -> anyhow::Result<ProblemContext> {
    match current {
        CurrentContext::ProblemDir(ctx) => match problem {
            Some(_) => anyhow::bail!(
                "Cannot specify a problem from a problem directory. Move to the contest directory."
            ),
            None => Ok(ctx),
        },
        CurrentContext::ContestDir(ctx) => match problem {
            Some(p) => detect_problem_dir_from(&ctx.contest_dir.join(p.to_lowercase()))
                .with_context(|| format!("Problem '{}' not found", p)),
            None => anyhow::bail!("Specify a problem, or run from a problem directory."),
        },
        CurrentContext::Outside => {
            anyhow::bail!("Run this command from a problem or contest directory.")
        }
    }
}

/// Detect the problem context from the current working directory.
/// Reads `[package.metadata.acr]` from the Cargo.toml in cwd.
pub fn detect_problem_dir() -> anyhow::Result<ProblemContext> {
    let cwd = std::env::current_dir().context("Failed to get current directory")?;
    detect_problem_dir_from(&cwd)
}

/// Detect the problem context from a given directory.
pub fn detect_problem_dir_from(dir: &std::path::Path) -> anyhow::Result<ProblemContext> {
    let cargo_toml_path = dir.join("Cargo.toml");
    let content = std::fs::read_to_string(&cargo_toml_path)
        .with_context(|| format!("No Cargo.toml found in {}", dir.display()))?;
    let doc: toml::Value = toml::from_str(&content).context("Failed to parse Cargo.toml")?;

    let problem_url = doc
        .get("package")
        .and_then(|p| p.get("metadata"))
        .and_then(|m| m.get("acr"))
        .and_then(|a| a.get("problem_url"))
        .and_then(|u| u.as_str())
        .ok_or_else(|| {
            anyhow::anyhow!(
                "Not a problem directory: [package.metadata.acr] not found in {}",
                cargo_toml_path.display()
            )
        })?
        .to_string();

    // Parse URL: https://atcoder.jp/contests/{contest_id}/tasks/{task_screen_name}
    let parts: Vec<&str> = problem_url.split('/').collect();
    // Expected: ["https:", "", "atcoder.jp", "contests", "{contest_id}", "tasks", "{task_screen_name}"]
    let contest_id = parts
        .get(4)
        .ok_or_else(|| anyhow::anyhow!("Invalid problem_url: {}", problem_url))?
        .to_string();
    let task_screen_name = parts
        .get(6)
        .ok_or_else(|| anyhow::anyhow!("Invalid problem_url: {}", problem_url))?
        .to_string();

    let problem_alphabet = dir
        .file_name()
        .and_then(|n| n.to_str())
        .unwrap_or("")
        .to_string();

    Ok(ProblemContext {
        contest_id,
        problem_alphabet,
        task_screen_name,
        problem_dir: dir.to_path_buf(),
        problem_url,
    })
}

/// List all problem contexts in a contest directory, sorted by alphabet.
pub fn list_contest_problems(contest_dir: &std::path::Path) -> anyhow::Result<Vec<ProblemContext>> {
    let mut problems = Vec::new();
    for entry in std::fs::read_dir(contest_dir)
        .with_context(|| format!("Failed to read directory: {}", contest_dir.display()))?
    {
        let path = entry?.path();
        if path.is_dir()
            && let Ok(ctx) = detect_problem_dir_from(&path)
        {
            problems.push(ctx);
        }
    }
    problems.sort_by(|a, b| a.problem_alphabet.cmp(&b.problem_alphabet));
    Ok(problems)
}

/// Find a contest directory by contest ID, searching from the current working directory.
pub fn find_contest_dir_by_id(contest_id: &str) -> anyhow::Result<ContestContext> {
    let cwd = std::env::current_dir().context("Failed to get current directory")?;
    find_contest_dir_by_id_from(&cwd, contest_id)
}

/// Find a contest directory by contest ID, searching from a given base directory.
pub fn find_contest_dir_by_id_from(
    base_dir: &std::path::Path,
    contest_id: &str,
) -> anyhow::Result<ContestContext> {
    let candidate = base_dir.join(contest_id);
    let cargo_toml = candidate.join("Cargo.toml");
    let content = std::fs::read_to_string(&cargo_toml)
        .with_context(|| format!("Contest '{}' not found", contest_id))?;
    let doc: toml::Value = toml::from_str(&content).context("Failed to parse Cargo.toml")?;
    if doc.get("workspace").is_none() {
        anyhow::bail!(
            "{} is not a contest workspace (no [workspace] in Cargo.toml)",
            candidate.display()
        );
    }
    Ok(ContestContext {
        contest_id: contest_id.to_string(),
        contest_dir: candidate,
    })
}

/// Detect the contest workspace directory from the current working directory.
/// Checks cwd and its parent for a workspace Cargo.toml with [workspace].
pub fn detect_contest_dir() -> anyhow::Result<ContestContext> {
    let cwd = std::env::current_dir().context("Failed to get current directory")?;
    detect_contest_dir_from(&cwd)
}

/// Detect the contest workspace directory from a given directory.
pub fn detect_contest_dir_from(dir: &std::path::Path) -> anyhow::Result<ContestContext> {
    for candidate in [dir, dir.parent().unwrap_or(dir)] {
        let cargo_toml = candidate.join("Cargo.toml");
        if let Ok(content) = std::fs::read_to_string(&cargo_toml)
            && let Ok(doc) = toml::from_str::<toml::Value>(&content)
            && doc.get("workspace").is_some()
        {
            let contest_id = candidate
                .file_name()
                .and_then(|n| n.to_str())
                .unwrap_or("")
                .to_string();
            return Ok(ContestContext {
                contest_id,
                contest_dir: candidate.to_path_buf(),
            });
        }
    }
    Err(anyhow::anyhow!(
        "Not in a contest workspace directory. Run this command from a contest or problem directory."
    ))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_detect_problem_dir() {
        let dir = tempfile::tempdir().unwrap();
        let problem_dir = dir.path().join("abc001").join("a");
        std::fs::create_dir_all(&problem_dir).unwrap();
        std::fs::write(
            problem_dir.join("Cargo.toml"),
            r#"[package]
name = "abc001-a"
version = "0.1.0"
edition = "2021"

[package.metadata.acr]
problem_url = "https://atcoder.jp/contests/abc001/tasks/abc001_a"
"#,
        )
        .unwrap();

        let ctx = detect_problem_dir_from(&problem_dir).unwrap();
        assert_eq!(ctx.contest_id, "abc001");
        assert_eq!(ctx.task_screen_name, "abc001_a");
        assert_eq!(ctx.problem_alphabet, "a");
        assert_eq!(
            ctx.problem_url,
            "https://atcoder.jp/contests/abc001/tasks/abc001_a"
        );
    }

    #[test]
    fn test_detect_problem_dir_not_a_problem() {
        let dir = tempfile::tempdir().unwrap();
        std::fs::write(
            dir.path().join("Cargo.toml"),
            "[package]\nname = \"test\"\n",
        )
        .unwrap();
        assert!(detect_problem_dir_from(dir.path()).is_err());
    }

    #[test]
    fn test_detect_contest_dir_from_workspace() {
        let dir = tempfile::tempdir().unwrap();
        let ws = dir.path().join("abc001");
        std::fs::create_dir_all(&ws).unwrap();
        std::fs::write(
            ws.join("Cargo.toml"),
            "[workspace]\nmembers = [\"a\"]\nresolver = \"2\"\n",
        )
        .unwrap();

        let ctx = detect_contest_dir_from(&ws).unwrap();
        assert_eq!(ctx.contest_dir, ws);
        assert_eq!(ctx.contest_id, "abc001");
    }

    /// Create a contest workspace with problem directories for testing.
    fn create_test_workspace(
        base: &std::path::Path,
        contest_id: &str,
        problems: &[&str],
    ) -> PathBuf {
        let ws = base.join(contest_id);
        std::fs::create_dir_all(&ws).unwrap();
        let members: Vec<String> = problems.iter().map(|p| format!("\"{}\"", p)).collect();
        std::fs::write(
            ws.join("Cargo.toml"),
            format!(
                "[workspace]\nmembers = [{}]\nresolver = \"2\"\n",
                members.join(", ")
            ),
        )
        .unwrap();
        for p in problems {
            let problem_dir = ws.join(p);
            std::fs::create_dir_all(&problem_dir).unwrap();
            std::fs::write(
                problem_dir.join("Cargo.toml"),
                format!(
                    r#"[package]
name = "{contest_id}-{p}"
version = "0.1.0"
edition = "2021"

[package.metadata.acr]
problem_url = "https://atcoder.jp/contests/{contest_id}/tasks/{contest_id}_{p}"
"#,
                ),
            )
            .unwrap();
        }
        ws
    }

    #[test]
    fn test_detect_contest_dir_from_problem_dir() {
        let dir = tempfile::tempdir().unwrap();
        let ws = dir.path().join("abc001");
        let problem = ws.join("a");
        std::fs::create_dir_all(&problem).unwrap();
        std::fs::write(
            ws.join("Cargo.toml"),
            "[workspace]\nmembers = [\"a\"]\nresolver = \"2\"\n",
        )
        .unwrap();

        let ctx = detect_contest_dir_from(&problem).unwrap();
        assert_eq!(ctx.contest_dir, ws);
        assert_eq!(ctx.contest_id, "abc001");
    }

    // find_contest_dir_by_id_from tests

    #[test]
    fn test_find_contest_dir_by_id_from_valid() {
        let dir = tempfile::tempdir().unwrap();
        let ws = create_test_workspace(dir.path(), "abc001", &["a"]);
        let ctx = find_contest_dir_by_id_from(dir.path(), "abc001").unwrap();
        assert_eq!(ctx.contest_id, "abc001");
        assert_eq!(ctx.contest_dir, ws);
    }

    #[test]
    fn test_find_contest_dir_by_id_from_not_found() {
        let dir = tempfile::tempdir().unwrap();
        assert!(find_contest_dir_by_id_from(dir.path(), "abc999").is_err());
    }

    #[test]
    fn test_find_contest_dir_by_id_from_not_workspace() {
        let dir = tempfile::tempdir().unwrap();
        let not_ws = dir.path().join("abc001");
        std::fs::create_dir_all(&not_ws).unwrap();
        std::fs::write(not_ws.join("Cargo.toml"), "[package]\nname = \"test\"\n").unwrap();
        assert!(find_contest_dir_by_id_from(dir.path(), "abc001").is_err());
    }

    // require_problem_context tests

    #[test]
    fn test_require_problem_context_problem_dir_none() {
        let dir = tempfile::tempdir().unwrap();
        let ws = create_test_workspace(dir.path(), "abc001", &["a"]);
        let problem_ctx = detect_problem_dir_from(&ws.join("a")).unwrap();
        let ctx = CurrentContext::ProblemDir(problem_ctx);
        let result = require_problem_context(ctx, None).unwrap();
        assert_eq!(result.contest_id, "abc001");
        assert_eq!(result.problem_alphabet, "a");
    }

    #[test]
    fn test_require_problem_context_problem_dir_some_error() {
        let dir = tempfile::tempdir().unwrap();
        let ws = create_test_workspace(dir.path(), "abc001", &["a"]);
        let problem_ctx = detect_problem_dir_from(&ws.join("a")).unwrap();
        let ctx = CurrentContext::ProblemDir(problem_ctx);
        assert!(require_problem_context(ctx, Some("b")).is_err());
    }

    #[test]
    fn test_require_problem_context_contest_dir_some() {
        let dir = tempfile::tempdir().unwrap();
        let ws = create_test_workspace(dir.path(), "abc001", &["a", "b"]);
        let contest_ctx = detect_contest_dir_from(&ws).unwrap();
        let ctx = CurrentContext::ContestDir(contest_ctx);
        let result = require_problem_context(ctx, Some("a")).unwrap();
        assert_eq!(result.contest_id, "abc001");
        assert_eq!(result.problem_alphabet, "a");
    }

    #[test]
    fn test_require_problem_context_contest_dir_none_error() {
        let dir = tempfile::tempdir().unwrap();
        let ws = create_test_workspace(dir.path(), "abc001", &["a"]);
        let contest_ctx = detect_contest_dir_from(&ws).unwrap();
        let ctx = CurrentContext::ContestDir(contest_ctx);
        assert!(require_problem_context(ctx, None).is_err());
    }

    #[test]
    fn test_require_problem_context_outside_error() {
        assert!(require_problem_context(CurrentContext::Outside, None).is_err());
        assert!(require_problem_context(CurrentContext::Outside, Some("a")).is_err());
    }

    // list_contest_problems tests

    #[test]
    fn test_list_contest_problems_sorted() {
        let dir = tempfile::tempdir().unwrap();
        let ws = create_test_workspace(dir.path(), "abc001", &["c", "a", "b"]);
        let problems = list_contest_problems(&ws).unwrap();
        let alphabets: Vec<&str> = problems
            .iter()
            .map(|p| p.problem_alphabet.as_str())
            .collect();
        assert_eq!(alphabets, vec!["a", "b", "c"]);
    }

    #[test]
    fn test_list_contest_problems_empty() {
        let dir = tempfile::tempdir().unwrap();
        let ws = dir.path().join("abc001");
        std::fs::create_dir_all(&ws).unwrap();
        std::fs::write(
            ws.join("Cargo.toml"),
            "[workspace]\nmembers = []\nresolver = \"2\"\n",
        )
        .unwrap();
        let problems = list_contest_problems(&ws).unwrap();
        assert!(problems.is_empty());
    }
}