haematite-cli 0.21.8

The haematite command: serve a store, its door and Studio, answer MCP, query with jq, and host the store's assistant seat
Documentation
use std::fs;
use std::io;
use std::path::{Path, PathBuf};

use super::types::Error;

#[derive(Debug, PartialEq, Eq)]
pub(super) enum Event {
    Stage(PathBuf),
    Sync(PathBuf),
    Publish(PathBuf),
}

#[derive(Default)]
pub(super) struct Hooks {
    pub events: Vec<Event>,
    pub fail_payload: Option<usize>,
    pub fail_sync: Option<usize>,
    pub obstruct_publish: bool,
    pub fail_cleanup: bool,
    pub(super) syncs: usize,
}

impl Hooks {
    pub(super) fn payload(&self, index: usize, path: &Path) -> Result<(), Error> {
        if self.fail_payload == Some(index) {
            fs::create_dir(path).map_err(|error| Error::new("bundle_write_failed", &error))?;
        }
        Ok(())
    }

    pub(super) fn before_sync(&mut self) -> io::Result<()> {
        self.syncs += 1;
        if self.fail_sync == Some(self.syncs) {
            return Err(io::Error::other("fixture sync refused"));
        }
        Ok(())
    }

    pub(super) fn before_publish(&self, destination: &Path) -> io::Result<()> {
        if self.obstruct_publish {
            fs::create_dir(destination)?;
            fs::write(destination.join("sentinel"), b"retained")?;
        }
        Ok(())
    }

    pub(super) fn before_cleanup(&self) -> io::Result<()> {
        if self.fail_cleanup {
            return Err(io::Error::other("fixture cleanup refused"));
        }
        Ok(())
    }
}