cargo_e/
e_parser.rs

1/// Parses the stderr output to extract available items (e.g. binaries or examples)
2/// by looking for a marker of the form "Available {item}:".
3///
4/// # Example
5/// ```
6/// use cargo_e::e_parser::parse_available;
7///
8/// let stderr = "Available examples:\n  example1\n  example2\n";
9/// let result = parse_available(stderr, "examples");
10/// assert_eq!(result, vec!["example1", "example2"]);
11/// ```
12pub fn parse_available(stderr: &str, item: &str) -> Vec<String> {
13    let marker = format!("Available {}:", item);
14    let mut available = Vec::new();
15    let mut collecting = false;
16
17    for line in stderr.lines() {
18        if collecting {
19            let trimmed = line.trim();
20            if !trimmed.is_empty() {
21                available.push(trimmed.to_string());
22            }
23        }
24        if line.contains(&marker) {
25            collecting = true;
26        }
27    }
28    available
29}
30
31/// Reads run history from a file and returns a HashMap mapping target names to run counts.
32pub fn read_run_history(
33    history_path: &std::path::Path,
34) -> std::collections::HashMap<String, usize> {
35    let mut history = std::collections::HashMap::new();
36    if let Ok(contents) = std::fs::read_to_string(history_path) {
37        for line in contents.lines() {
38            let trimmed = line.trim();
39            if !trimmed.is_empty() {
40                // Increment the count for this target.
41                *history.entry(trimmed.to_string()).or_insert(0) += 1;
42            }
43        }
44    }
45    history
46}