aecr-sync 0.1.0

ISO 19650 Workset CDE and element-borrow synchronization
Documentation
//! Error types for ISO 19650 CDE workflows, workset borrow leases, and delta synchronization.

use aecr_pig::{ActorId, ContentHash, Diagnostic, NodeId, PigError};
use thiserror::Error;

use crate::iso19650::{CdeState, SuitabilityCode};

/// Errors returned by ISO 19650 container validation, promotion gates, worksets, and delta sync.
#[derive(Debug, Error)]
pub enum SyncError {
    /// ISO 19650-2 container identifier failed token format validation.
    #[error("invalid ISO 19650 container id: {0}")]
    InvalidContainerId(String),

    /// Requested CDE state transition or suitability code violates the ISO 19650 lifecycle matrix.
    #[error("invalid CDE transition from {from} to {to} with suitability {suitability}")]
    InvalidTransition {
        /// Current container lifecycle state.
        from: CdeState,
        /// Requested target lifecycle state.
        to: CdeState,
        /// Suitability code supplied for the transition.
        suitability: SuitabilityCode,
    },

    /// Mutation or non-archival transition rejected because the container is immutable.
    #[error("container in state {0} is immutable")]
    ContainerLocked(CdeState),

    /// WIP-to-Shared promotion rejected by [`aecr_pig::VerificationPipeline`].
    #[error("promotion to Shared blocked: {0:?}")]
    PromotionBlocked(Vec<Diagnostic>),

    /// Node cannot be edited or acquired because another actor holds an exclusive borrow lease.
    #[error("element {node} is borrowed by actor '{owner}'")]
    ElementBorrowed {
        /// Contended PIG node identifier.
        node: NodeId,
        /// Actor currently holding the exclusive borrow lease.
        owner: ActorId,
    },

    /// Delta base root hash does not match any active revision in local Command Bus history.
    #[error("unknown base root hash {0}")]
    UnknownBaseHash(ContentHash),

    /// Merkle root hash chain mismatch detected while validating or applying a delta envelope.
    #[error("root hash mismatch: expected {expected}, actual {actual}")]
    HashMismatch {
        /// Expected SHA-256 Merkle root hash.
        expected: ContentHash,
        /// Observed SHA-256 Merkle root hash.
        actual: ContentHash,
    },

    /// Underlying Parametric Intent Graph or Command Bus error.
    #[error("PIG error: {0}")]
    Pig(#[from] PigError),
}

#[cfg(test)]
mod tests {
    use aecr_pig::{ActorId, ContentHash, Diagnostic, DiagnosticSeverity, NodeId, PigError};

    use super::SyncError;
    use crate::iso19650::{CdeState, SuitabilityCode};

    #[test]
    fn sync_error_variants_format_cleanly() {
        let bad_id = SyncError::InvalidContainerId("bad-id".to_owned());
        assert!(bad_id.to_string().contains("bad-id"));

        let bad_tx = SyncError::InvalidTransition {
            from: CdeState::WorkInProgress,
            to: CdeState::Published,
            suitability: SuitabilityCode::A1,
        };
        assert!(bad_tx.to_string().contains("WorkInProgress"));
        assert!(bad_tx.to_string().contains("Published"));
        assert!(bad_tx.to_string().contains("A1"));

        let locked = SyncError::ContainerLocked(CdeState::Archived);
        assert!(locked.to_string().contains("Archived"));

        let blocked = SyncError::PromotionBlocked(vec![Diagnostic {
            node: Some(NodeId(4)),
            code: "CHECK_FAILED".to_owned(),
            message: "clash detected".to_owned(),
            severity: DiagnosticSeverity::Error,
        }]);
        assert!(blocked.to_string().contains("CHECK_FAILED"));

        let borrowed = SyncError::ElementBorrowed {
            node: NodeId(7),
            owner: ActorId::new("architect-1"),
        };
        assert!(borrowed.to_string().contains("7"));
        assert!(borrowed.to_string().contains("architect-1"));

        let unknown = SyncError::UnknownBaseHash(ContentHash::ZERO);
        assert!(unknown.to_string().contains(&ContentHash::ZERO.to_hex()));

        let mismatch = SyncError::HashMismatch {
            expected: ContentHash::ZERO,
            actual: ContentHash::from_bytes([1_u8; 32]),
        };
        assert!(mismatch.to_string().contains("mismatch"));

        let pig_err = SyncError::from(PigError::NodeNotFound(NodeId(9)));
        assert!(matches!(pig_err, SyncError::Pig(_)));
        assert!(pig_err.to_string().contains("9"));
    }
}