aion-context 1.0.0

Cryptographically-signed, versioned business-context file format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
// SPDX-License-Identifier: MIT OR Apache-2.0
//! Error types for AION v2
//!
//! This module defines the error types used throughout AION v2. Following Tiger Style,
//! all errors are explicit, provide actionable messages, and implement `std::error::Error`.
//!
//! # Error Categories
//!
//! Errors are organized into logical categories for clarity:
//!
//! - **I/O Errors** - File system operations
//! - **Cryptographic Errors** - Signature and encryption failures
//! - **Format Errors** - File parsing and validation
//! - **Version Errors** - Version chain and history issues
//! - **Key Management Errors** - Keyring and key storage
//! - **Validation Errors** - Input validation failures
//! - **Operational Errors** - Runtime operation failures
//!
//! # Usage Example
//!
//! ```
//! use aion_context::{AionError, Result};
//! use std::path::PathBuf;
//!
//! fn load_file(path: &str) -> Result<Vec<u8>> {
//!     std::fs::read(path).map_err(|e| AionError::FileReadError {
//!         path: PathBuf::from(path),
//!         source: e,
//!     })
//! }
//!
//! // Errors provide contextual information
//! match load_file("nonexistent.aion") {
//!     Ok(data) => println!("Loaded {} bytes", data.len()),
//!     Err(e) => eprintln!("Error: {e}"),
//! }
//! ```
//!
//! # Error Handling Best Practices
//!
//! 1. **Always use `?` operator** for error propagation
//! 2. **Add context** to errors when re-wrapping
//! 3. **Match on error types** for specific handling
//! 4. **Display errors to users** with helpful messages
//!
//! ```
//! use aion_context::Result;
//!
//! fn process_file() -> Result<()> {
//!     let data = load_file("file.aion")?;  // ✓ Propagate errors
//!     verify_signatures(&data)?;            // ✓ Chain operations
//!     Ok(())
//! }
//! # fn load_file(path: &str) -> Result<Vec<u8>> { Ok(vec![]) }
//! # fn verify_signatures(data: &[u8]) -> Result<()> { Ok(()) }
//! ```

use std::path::PathBuf;
use thiserror::Error;

use crate::types::AuthorId;

/// Top-level error type for AION v2
///
/// All errors provide contextual information to aid debugging and suggest
/// solutions to users. Errors are organized by category for clarity.
///
/// `#[non_exhaustive]` because the crate is under active development —
/// new error categories will land for crypto rotations, hardware
/// attestation, transparency-log proofs, and additional compliance
/// frameworks. Adding a variant should not force every downstream
/// consumer's exhaustive `match` to update on a minor release.
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum AionError {
    // ============================================================================
    // I/O Errors
    // ============================================================================
    /// Failed to read a file
    #[error("Failed to read file: {path}")]
    FileReadError {
        /// Path to the file that couldn't be read
        path: PathBuf,
        /// Underlying I/O error
        #[source]
        source: std::io::Error,
    },

    /// Failed to write a file
    #[error("Failed to write file: {path}")]
    FileWriteError {
        /// Path to the file that couldn't be written
        path: PathBuf,
        /// Underlying I/O error
        #[source]
        source: std::io::Error,
    },

    /// File already exists
    #[error("File already exists: {path}")]
    FileExists {
        /// Path to the existing file
        path: PathBuf,
    },

    /// File not found
    #[error("File not found: {path}")]
    FileNotFound {
        /// Path to the missing file
        path: PathBuf,
    },

    /// Permission denied
    #[error("Permission denied: {path}")]
    PermissionDenied {
        /// Path to the file with permission issues
        path: PathBuf,
    },

    // ============================================================================
    // Format Errors
    // ============================================================================
    /// Invalid file format
    #[error("Invalid file format: {reason}")]
    InvalidFormat {
        /// Description of the format issue
        reason: String,
    },

    /// Corrupted file detected via checksum mismatch
    #[error("Corrupted file: checksum mismatch (expected: {expected}, got: {actual})")]
    CorruptedFile {
        /// Expected checksum
        expected: String,
        /// Actual checksum
        actual: String,
    },

    /// Unsupported file version
    #[error("Unsupported file version: {version} (supported: {supported})")]
    UnsupportedVersion {
        /// Version found in the file
        version: u16,
        /// Supported versions
        supported: String,
    },

    /// Invalid file header
    #[error("Invalid header: {reason}")]
    InvalidHeader {
        /// Description of header issue
        reason: String,
    },

    // ============================================================================
    // Cryptographic Errors
    // ============================================================================
    /// Signature verification failed
    #[error("Signature verification failed for version {version} by author {author}")]
    SignatureVerificationFailed {
        /// Version number that failed verification
        version: u64,
        /// Author ID
        author: AuthorId,
    },

    /// Invalid signature
    #[error("Invalid signature: {reason}")]
    InvalidSignature {
        /// Description of signature issue
        reason: String,
    },

    /// A commit was attempted by a signer the registry has no active
    /// epoch for at the target version (issue #25). The write was
    /// refused before any bytes were emitted.
    #[error(
        "Unauthorized signer: author {author} has no active registry epoch at version {version}"
    )]
    UnauthorizedSigner {
        /// The author that attempted to sign.
        author: AuthorId,
        /// The version number the commit would have produced.
        version: u64,
    },

    /// The supplied signing key's public half does not match the
    /// operational key pinned in the registry for this author's active
    /// epoch (issue #25). Most often means the caller used a rotated-
    /// out key.
    #[error(
        "Key mismatch: author {author} signing key does not match registry epoch {epoch} operational key"
    )]
    KeyMismatch {
        /// The author that attempted to sign.
        author: AuthorId,
        /// The registry epoch number that pinned a different public key.
        epoch: u32,
    },

    /// Decryption failed
    #[error("Decryption failed: {reason}")]
    DecryptionFailed {
        /// Description of decryption failure
        reason: String,
    },

    /// Encryption failed
    #[error("Encryption failed: {reason}")]
    EncryptionFailed {
        /// Description of encryption failure
        reason: String,
    },

    /// Hash mismatch after decryption
    #[error("Hash mismatch: expected {expected:x?}, got {actual:x?}")]
    HashMismatch {
        /// Expected hash value
        expected: [u8; 32],
        /// Actual hash value
        actual: [u8; 32],
    },

    /// Invalid private key
    #[error("Invalid private key: {reason}")]
    InvalidPrivateKey {
        /// Description of key issue
        reason: String,
    },

    /// Invalid public key
    #[error("Invalid public key: {reason}")]
    InvalidPublicKey {
        /// Description of key issue
        reason: String,
    },

    // ============================================================================
    // Version Chain Errors
    // ============================================================================
    /// Version chain integrity broken
    #[error("Version chain broken at version {version}: parent hash mismatch")]
    BrokenVersionChain {
        /// Version where the chain breaks
        version: u64,
    },

    /// Invalid version number
    #[error("Invalid version number: {version} (current: {current})")]
    InvalidVersionNumber {
        /// Invalid version number
        version: u64,
        /// Current version
        current: u64,
    },

    /// Version number overflow
    #[error("Version overflow: cannot increment beyond {max}")]
    VersionOverflow {
        /// Maximum version reached
        max: u64,
    },

    /// Missing version in chain
    #[error("Missing version: {version}")]
    MissingVersion {
        /// Missing version number
        version: u64,
    },

    // ============================================================================
    // Key Management Errors
    // ============================================================================
    /// Key not found in keyring
    #[error("Key not found for author {author_id}: {reason}")]
    KeyNotFound {
        /// Author identifier
        author_id: crate::types::AuthorId,
        /// Description of the error
        reason: String,
    },

    /// Keyring access denied
    #[error("Keyring access denied: {reason}")]
    KeyringAccessDenied {
        /// Description of access issue
        reason: String,
    },

    /// Failed to store key
    #[error("Failed to store key: {reason}")]
    KeyStoreFailed {
        /// Description of storage failure
        reason: String,
    },

    /// Keyring operation error
    #[error("Keyring {operation} failed: {reason}")]
    KeyringError {
        /// Operation that failed
        operation: String,
        /// Description of the error
        reason: String,
    },

    // ============================================================================
    // Validation Errors
    // ============================================================================
    /// Invalid file ID
    #[error("Invalid file ID: {file_id}")]
    InvalidFileId {
        /// Invalid file ID value
        file_id: u64,
    },

    /// Invalid author ID
    #[error("Invalid author ID: {author_id}")]
    InvalidAuthorId {
        /// Invalid author ID value
        author_id: u64,
    },

    /// Invalid timestamp
    #[error("Invalid timestamp: {reason}")]
    InvalidTimestamp {
        /// Description of timestamp issue
        reason: String,
    },

    /// Invalid action code
    #[error("Invalid action code: {code}")]
    InvalidActionCode {
        /// Invalid action code value
        code: u16,
    },

    /// Broken audit chain
    #[error("Broken audit chain: expected hash {expected:?}, got {actual:?}")]
    BrokenAuditChain {
        /// Expected previous hash
        expected: [u8; 32],
        /// Actual previous hash
        actual: [u8; 32],
    },

    /// Invalid UTF-8 encoding
    #[error("Invalid UTF-8: {reason}")]
    InvalidUtf8 {
        /// Description of UTF-8 validation failure
        reason: String,
    },

    /// Rules too large
    #[error("Rules too large: {size} bytes (max: {max} bytes)")]
    RulesTooLarge {
        /// Actual size
        size: usize,
        /// Maximum allowed size
        max: usize,
    },

    // ============================================================================
    // Operational Errors
    // ============================================================================
    /// Operation not permitted
    #[error("Operation not permitted: {operation} requires {required}")]
    OperationNotPermitted {
        /// Operation attempted
        operation: String,
        /// Required permission/state
        required: String,
    },

    /// Conflicting operation
    #[error("Conflicting operation: {reason}")]
    Conflict {
        /// Description of conflict
        reason: String,
    },

    /// Resource exhausted
    #[error("Resource exhausted: {resource}")]
    ResourceExhausted {
        /// Resource that was exhausted
        resource: String,
    },
}

/// Result type alias for AION operations
///
/// # Examples
///
/// ```
/// use aion_context::error::{AionError, Result};
///
/// fn read_version(version: u64) -> Result<String> {
///     if version == 0 {
///         return Err(AionError::InvalidVersionNumber { version, current: 1 });
///     }
///     Ok("version data".to_string())
/// }
/// ```
pub type Result<T> = std::result::Result<T, AionError>;

// Implement Send + Sync for async compatibility
// (thiserror derives these automatically when possible)

#[cfg(test)]
#[allow(clippy::unwrap_used)] // Tests are allowed to panic
mod tests {
    use super::*;

    #[test]
    fn error_should_implement_error_trait() {
        let err = AionError::InvalidFormat {
            reason: "test".to_string(),
        };
        let _: &dyn std::error::Error = &err;
    }

    #[test]
    fn error_should_be_send_and_sync() {
        fn assert_send<T: Send>() {}
        fn assert_sync<T: Sync>() {}
        assert_send::<AionError>();
        assert_sync::<AionError>();
    }

    mod file_errors {
        use super::*;

        #[test]
        fn file_read_error_should_display_path_and_source() {
            let err = AionError::FileReadError {
                path: PathBuf::from("/test/file.aion"),
                source: std::io::Error::from(std::io::ErrorKind::NotFound),
            };
            let msg = format!("{err}");
            assert!(msg.contains("/test/file.aion"));
            assert!(msg.contains("Failed to read file"));
        }

        #[test]
        fn file_not_found_should_display_path() {
            let err = AionError::FileNotFound {
                path: PathBuf::from("/missing.aion"),
            };
            assert_eq!(format!("{err}"), "File not found: /missing.aion");
        }

        #[test]
        fn permission_denied_should_display_path() {
            let err = AionError::PermissionDenied {
                path: PathBuf::from("/protected.aion"),
            };
            assert_eq!(format!("{err}"), "Permission denied: /protected.aion");
        }
    }

    mod format_errors {
        use super::*;

        #[test]
        fn invalid_format_should_display_reason() {
            let err = AionError::InvalidFormat {
                reason: "missing magic number".to_string(),
            };
            assert_eq!(
                format!("{err}"),
                "Invalid file format: missing magic number"
            );
        }

        #[test]
        fn corrupted_file_should_display_checksums() {
            let err = AionError::CorruptedFile {
                expected: "abc123".to_string(),
                actual: "def456".to_string(),
            };
            let msg = format!("{err}");
            assert!(msg.contains("abc123"));
            assert!(msg.contains("def456"));
        }

        #[test]
        fn unsupported_version_should_display_versions() {
            let err = AionError::UnsupportedVersion {
                version: 99,
                supported: "1-2".to_string(),
            };
            assert_eq!(
                format!("{err}"),
                "Unsupported file version: 99 (supported: 1-2)"
            );
        }
    }

    mod crypto_errors {
        use super::*;

        #[test]
        fn signature_verification_failed_should_display_details() {
            let err = AionError::SignatureVerificationFailed {
                version: 42,
                author: AuthorId::new(1),
            };
            let msg = format!("{err}");
            assert!(msg.contains("42"));
            assert!(msg.contains('1'));
        }

        #[test]
        fn invalid_signature_should_display_reason() {
            let err = AionError::InvalidSignature {
                reason: "wrong length".to_string(),
            };
            assert_eq!(format!("{err}"), "Invalid signature: wrong length");
        }

        #[test]
        fn decryption_failed_should_display_reason() {
            let err = AionError::DecryptionFailed {
                reason: "wrong key".to_string(),
            };
            assert_eq!(format!("{err}"), "Decryption failed: wrong key");
        }
    }

    mod version_errors {
        use super::*;

        #[test]
        fn broken_version_chain_should_display_version() {
            let err = AionError::BrokenVersionChain { version: 5 };
            assert_eq!(
                format!("{err}"),
                "Version chain broken at version 5: parent hash mismatch"
            );
        }

        #[test]
        fn invalid_version_number_should_display_versions() {
            let err = AionError::InvalidVersionNumber {
                version: 10,
                current: 5,
            };
            assert_eq!(format!("{err}"), "Invalid version number: 10 (current: 5)");
        }

        #[test]
        fn version_overflow_should_display_max() {
            let err = AionError::VersionOverflow { max: u64::MAX };
            let msg = format!("{err}");
            assert!(msg.contains("overflow"));
            assert!(msg.contains(&u64::MAX.to_string()));
        }

        #[test]
        fn missing_version_should_display_version() {
            let err = AionError::MissingVersion { version: 3 };
            assert_eq!(format!("{err}"), "Missing version: 3");
        }
    }

    mod key_management_errors {
        use super::*;
        use crate::types::AuthorId;

        #[test]
        fn key_not_found_should_display_author_id() {
            let err = AionError::KeyNotFound {
                author_id: AuthorId::new(50001),
                reason: "not found".to_string(),
            };
            assert_eq!(
                format!("{err}"),
                "Key not found for author 50001: not found"
            );
        }

        #[test]
        fn keyring_access_denied_should_display_reason() {
            let err = AionError::KeyringAccessDenied {
                reason: "locked".to_string(),
            };
            assert_eq!(format!("{err}"), "Keyring access denied: locked");
        }

        #[test]
        fn keyring_error_should_display_operation() {
            let err = AionError::KeyringError {
                operation: "store".to_string(),
                reason: "permission denied".to_string(),
            };
            assert_eq!(format!("{err}"), "Keyring store failed: permission denied");
        }
    }

    mod validation_errors {
        use super::*;

        #[test]
        fn invalid_file_id_should_display_id() {
            let err = AionError::InvalidFileId { file_id: 0 };
            assert_eq!(format!("{err}"), "Invalid file ID: 0");
        }

        #[test]
        fn rules_too_large_should_display_sizes() {
            let err = AionError::RulesTooLarge {
                size: 2_000_000,
                max: 1_000_000,
            };
            let msg = format!("{err}");
            assert!(msg.contains("2000000"));
            assert!(msg.contains("1000000"));
        }

        #[test]
        fn invalid_action_code_should_display_code() {
            let err = AionError::InvalidActionCode { code: 99 };
            assert_eq!(format!("{err}"), "Invalid action code: 99");
        }

        #[test]
        fn broken_audit_chain_should_display_hashes() {
            let expected = [0xAB; 32];
            let actual = [0xCD; 32];
            let err = AionError::BrokenAuditChain { expected, actual };
            let msg = format!("{err}");
            assert!(msg.contains("Broken audit chain"));
        }

        #[test]
        fn invalid_timestamp_should_display_reason() {
            let err = AionError::InvalidTimestamp {
                reason: "timestamp is in the future".to_string(),
            };
            assert_eq!(
                format!("{err}"),
                "Invalid timestamp: timestamp is in the future"
            );
        }

        #[test]
        fn invalid_utf8_should_display_reason() {
            let err = AionError::InvalidUtf8 {
                reason: "invalid byte sequence at offset 10".to_string(),
            };
            assert_eq!(
                format!("{err}"),
                "Invalid UTF-8: invalid byte sequence at offset 10"
            );
        }
    }

    mod operational_errors {
        use super::*;

        #[test]
        fn operation_not_permitted_should_display_details() {
            let err = AionError::OperationNotPermitted {
                operation: "commit".to_string(),
                required: "write access".to_string(),
            };
            let msg = format!("{err}");
            assert!(msg.contains("commit"));
            assert!(msg.contains("write access"));
        }

        #[test]
        fn conflict_should_display_reason() {
            let err = AionError::Conflict {
                reason: "version already exists".to_string(),
            };
            assert_eq!(
                format!("{err}"),
                "Conflicting operation: version already exists"
            );
        }

        #[test]
        fn resource_exhausted_should_display_resource() {
            let err = AionError::ResourceExhausted {
                resource: "memory".to_string(),
            };
            assert_eq!(format!("{err}"), "Resource exhausted: memory");
        }
    }

    mod result_type {
        use super::*;

        #[test]
        fn result_should_work_with_ok() {
            let result: Result<i32> = Ok(42);
            assert!(result.is_ok());
            if let Ok(value) = result {
                assert_eq!(value, 42);
            }
        }

        #[test]
        fn result_should_work_with_err() {
            let result: Result<i32> = Err(AionError::InvalidFormat {
                reason: "test".to_string(),
            });
            assert!(result.is_err());
        }
    }
}