supercode-harness 0.5.149

The optional native Volter Harness agent and tool harness
Documentation
//! The workflow layer's read door: `harness.v1.workflow.load`, the same code the `supercode
//! workflow load` verb calls. A harness's board is read as one typed [`Workflow`] value; the
//! board is written only through the harness's own door (`hermes kanban …`), never here.
use std::path::Path;

use serde::{Deserialize, Serialize};
use supercode_interchange::workflow::Workflow;

use crate::Result;

/// Which harness's board is read.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum WorkflowHarness {
    /// Hermes's Kanban: `kanban/kanban.db` and `kanban/boards/<slug>/kanban.db` under the home.
    Hermes,
    /// supercode's own board, as its backing declares it (a `ztrack.json` marker beside the board's document).
    Supercode,
}

impl WorkflowHarness {
    /// The store a home's board is kept in, as the home declares it: a supercode board's marker, else a Hermes
    /// `kanban.db`. A caller reads a board without naming its store.
    pub fn of_home(home: &Path) -> Self {
        let supercode = home.join("kanban").join("ztrack.json").is_file()
            || std::fs::read_dir(home.join("kanban").join("boards"))
                .map(|entries| {
                    entries
                        .flatten()
                        .any(|entry| entry.path().join("ztrack.json").is_file())
                })
                .unwrap_or(false);
        if supercode || !home.join("kanban.db").is_file() {
            Self::Supercode
        } else {
            Self::Hermes
        }
    }
}

/// What a load answers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowRead {
    /// The boards.
    pub workflow: Workflow,
}

/// Read a harness's board as one workflow value.
pub fn load(from: WorkflowHarness, home: &Path) -> Result<WorkflowRead> {
    let workflow = match from {
        WorkflowHarness::Hermes => {
            supercode_interchange::workflow::codec::hermes::from_hermes(home)?
        }
        WorkflowHarness::Supercode => {
            supercode_interchange::workflow::codec::hermes::from_supercode(home)?
        }
    };
    Ok(WorkflowRead { workflow })
}