spherenet-program-whitelist-interface 0.3.0

Interface of SphereNet's Program Whitelist Program
Documentation
//! Error types

use pinocchio::program_error::ProgramError;

/// Errors that may be returned by the Program Whitelist program.
///
/// Discriminants use a hex sentinel base (cf. vw's `0xAAAAAA00`): a visually
/// recognizable marker that keeps these `ProgramError::Custom` codes clear of
/// framework/system ranges. The base differs from vw's so the two programs'
/// custom codes never collide. Bare variants auto-increment from the base.
#[repr(u32)]
#[derive(thiserror::Error, Clone, Debug, Eq, PartialEq)]
pub enum ProgramWhitelistError {
    // --- Authentication / account validation (the shared auth-door surface) ---
    /// The account is not the canonical program whitelist account.
    #[error("Invalid program whitelist account")]
    InvalidProgramWhitelistAccount = 0xBBBBBB00,

    /// The signer does not match the pending authority.
    #[error("Pending authority mismatch")]
    PendingAuthorityMismatch, // 0xBBBBBB01

    /// No pending authority.
    #[error("No pending authority")]
    NoPendingAuthority, // 0xBBBBBB02

    /// An authority transfer is already in flight; it must be cancelled before
    /// another can be initiated.
    #[error("Authority transfer already initialized")]
    AuthorityTransferAlreadyInitialized, // 0xBBBBBB03

    // --- Entry-account validation (the entry-side door surface) ---
    /// The account is not the canonical whitelist entry PDA for the deployer.
    #[error("Invalid program whitelist entry account")]
    InvalidProgramWhitelistEntryAccount, // 0xBBBBBB04

    /// The whitelist entry account is not owned by the program whitelist
    /// program.
    #[error("Invalid program whitelist entry account owner")]
    InvalidProgramWhitelistEntryAccountOwner, // 0xBBBBBB05

    // --- Program-specific ---
    /// The deployer is not approved (whitelist entry is pending approval).
    #[error("Deployer not approved")]
    DeployerNotApproved, // 0xBBBBBB06

    /// The whitelist entry is not pending approval.
    #[error("Whitelist entry is not pending")]
    NotPending, // 0xBBBBBB07
}

impl From<ProgramWhitelistError> for ProgramError {
    fn from(e: ProgramWhitelistError) -> Self {
        ProgramError::Custom(e as u32)
    }
}

#[cfg(test)]
mod tests {
    use super::ProgramWhitelistError;

    /// Pin every discriminant to its hex-sentinel value. The variants are bare
    /// (auto-incrementing) for readability; this test is what makes a reorder or
    /// insertion that shifts a code fail loudly here, instead of silently
    /// diverging from the generated clients (which pin all values explicitly).
    #[test]
    fn error_discriminants_are_pinned() {
        assert_eq!(
            ProgramWhitelistError::InvalidProgramWhitelistAccount as u32,
            0xBBBBBB00
        );
        assert_eq!(
            ProgramWhitelistError::PendingAuthorityMismatch as u32,
            0xBBBBBB01
        );
        assert_eq!(
            ProgramWhitelistError::NoPendingAuthority as u32,
            0xBBBBBB02
        );
        assert_eq!(
            ProgramWhitelistError::AuthorityTransferAlreadyInitialized as u32,
            0xBBBBBB03
        );
        assert_eq!(
            ProgramWhitelistError::InvalidProgramWhitelistEntryAccount as u32,
            0xBBBBBB04
        );
        assert_eq!(
            ProgramWhitelistError::InvalidProgramWhitelistEntryAccountOwner as u32,
            0xBBBBBB05
        );
        assert_eq!(
            ProgramWhitelistError::DeployerNotApproved as u32,
            0xBBBBBB06
        );
        assert_eq!(ProgramWhitelistError::NotPending as u32, 0xBBBBBB07);
    }
}