Skip to main content

supercode_harness/
workflow_doors.rs

1//! The workflow layer's read door: `harness.v1.workflow.load`, the same code the `supercode
2//! workflow load` verb calls. A harness's board is read as one typed [`Workflow`] value; the
3//! board is written only through the harness's own door (`hermes kanban …`), never here.
4use std::path::Path;
5
6use serde::{Deserialize, Serialize};
7use supercode_interchange::workflow::Workflow;
8
9use crate::Result;
10
11/// Which harness's board is read.
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
13#[serde(rename_all = "lowercase")]
14pub enum WorkflowHarness {
15    /// Hermes's Kanban: `kanban/kanban.db` and `kanban/boards/<slug>/kanban.db` under the home.
16    Hermes,
17    /// supercode's own board, as its backing declares it (a `ztrack.json` marker beside the board's document).
18    Supercode,
19}
20
21impl WorkflowHarness {
22    /// The store a home's board is kept in, as the home declares it: a supercode board's marker, else a Hermes
23    /// `kanban.db`. A caller reads a board without naming its store.
24    pub fn of_home(home: &Path) -> Self {
25        let supercode = home.join("kanban").join("ztrack.json").is_file()
26            || std::fs::read_dir(home.join("kanban").join("boards"))
27                .map(|entries| {
28                    entries
29                        .flatten()
30                        .any(|entry| entry.path().join("ztrack.json").is_file())
31                })
32                .unwrap_or(false);
33        if supercode || !home.join("kanban.db").is_file() {
34            Self::Supercode
35        } else {
36            Self::Hermes
37        }
38    }
39}
40
41/// What a load answers.
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub struct WorkflowRead {
44    /// The boards.
45    pub workflow: Workflow,
46}
47
48/// Read a harness's board as one workflow value.
49pub fn load(from: WorkflowHarness, home: &Path) -> Result<WorkflowRead> {
50    let workflow = match from {
51        WorkflowHarness::Hermes => {
52            supercode_interchange::workflow::codec::hermes::from_hermes(home)?
53        }
54        WorkflowHarness::Supercode => {
55            supercode_interchange::workflow::codec::hermes::from_supercode(home)?
56        }
57    };
58    Ok(WorkflowRead { workflow })
59}