Skip to main content

mana_core/ops/
show.rs

1use std::path::{Path, PathBuf};
2
3use anyhow::{Context, Result};
4
5use crate::discovery::find_unit_file;
6use crate::unit::Unit;
7
8/// Result of loading a unit.
9pub struct GetResult {
10    pub unit: Unit,
11    pub path: PathBuf,
12}
13
14/// Load a unit by ID and return its full data.
15pub fn get(mana_dir: &Path, id: &str) -> Result<GetResult> {
16    let unit_path =
17        find_unit_file(mana_dir, id).with_context(|| format!("Unit not found: {}", id))?;
18    let unit =
19        Unit::from_file(&unit_path).with_context(|| format!("Failed to load unit: {}", id))?;
20    Ok(GetResult {
21        unit,
22        path: unit_path,
23    })
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29    use crate::ops::create::{self, tests::minimal_params};
30    use std::fs;
31    use tempfile::TempDir;
32
33    fn setup() -> (TempDir, PathBuf) {
34        let dir = TempDir::new().unwrap();
35        let bd = dir.path().join(".mana");
36        fs::create_dir(&bd).unwrap();
37        crate::config::Config {
38            project: "test".to_string(),
39            next_id: 1,
40            auto_close_parent: true,
41            run: None,
42            plan: None,
43            max_loops: 10,
44            max_concurrent: 4,
45            poll_interval: 30,
46            extends: vec![],
47            rules_file: None,
48            file_locking: false,
49            worktree: false,
50            on_close: None,
51            on_fail: None,
52            post_plan: None,
53            verify_timeout: None,
54            review: None,
55            user: None,
56            user_email: None,
57            auto_commit: false,
58            commit_template: None,
59            research: None,
60            run_model: None,
61            plan_model: None,
62            review_model: None,
63            research_model: None,
64            batch_verify: false,
65            memory_reserve_mb: 0,
66            notify: None,
67        }
68        .save(&bd)
69        .unwrap();
70        (dir, bd)
71    }
72
73    #[test]
74    fn get_existing() {
75        let (_dir, bd) = setup();
76        create::create(&bd, minimal_params("My task")).unwrap();
77        let r = get(&bd, "1").unwrap();
78        assert_eq!(r.unit.title, "My task");
79        assert!(r.path.exists());
80    }
81
82    #[test]
83    fn get_nonexistent() {
84        let (_dir, bd) = setup();
85        assert!(get(&bd, "99").is_err());
86    }
87}