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
//! # Error handling for the `age_crypto` library
//!
//! This module defines the unified error type and the alias `Result` used
//! across the entire library. It re‑exports the sub‑module errors so that
//! users can conveniently match on specific failure reasons if they wish.
//!
//! # Structure
//!
//! ```text
//! errors
//! ├── decrypt.rs → DecryptError (four variants for decrypt-specific failures)
//! ├── encrypt.rs → EncryptError (four variants for encrypt-specific failures)
//! └── mod.rs → Error (top-level enum), Result type alias
//! ```
//!
//! The top‑level [`Error`] is an enum with two variants:
//! - `Error::Encrypt(EncryptError)`
//! - `Error::Decrypt(DecryptError)`
//!
//! Both variants are created automatically from their respective
//! sub‑errors thanks to the `#[from]` attribute. This means:
//!
//! 1. Functions that work internally with `DecryptError` (e.g., the
//! `parse_identity` helper) can return `DecryptError` without worrying
//! about the outer type.
//! 2. The public API functions (e.g., `crate::decrypt`) have a return type
//! of `Result<T>` (i.e., `std::result::Result<T, Error>`). When they
//! use `?` on an expression that returns `Result<_, DecryptError>`, the
//! conversion to `Error::Decrypt` happens automatically.
//!
//! # Usage for library consumers
//!
//! ## Basic error handling
//!
//! ```rust
//! use age_crypto::{decrypt_with_passphrase, Error};
//!
//! // Contoh: ciphertext dummy untuk demonstrasi
//! let ciphertext = b"AGE-ENC..."; // hasil dari encrypt_with_passphrase
//! let passphrase = "my-secret-pass";
//!
//! match decrypt_with_passphrase(ciphertext, passphrase) {
//! Ok(plaintext) => println!("Decrypted: {:?}", std::str::from_utf8(&plaintext)),
//! Err(Error::Decrypt(e)) => eprintln!("Decrypt error: {}", e),
//! Err(Error::Encrypt(e)) => eprintln!("Unexpected encrypt error: {}", e),
//! }
//! ```
//!
//! ## Distinguishing error kinds
//!
//! ```rust
//! use age_crypto::{decrypt_with_passphrase, Error};
//! use age_crypto::errors::DecryptError;
//!
//! let ciphertext = b"invalid-ciphertext";
//! let passphrase = "wrong-pass";
//!
//! match decrypt_with_passphrase(ciphertext, passphrase) {
//! Ok(plain) => {
//! // Handle successful decryption
//! let _ = plain;
//! }
//! Err(Error::Decrypt(DecryptError::InvalidCiphertext(_))) => {
//! // Handle corrupted or malformed ciphertext
//! eprintln!("Ciphertext is invalid or corrupted");
//! }
//! Err(Error::Decrypt(DecryptError::Failed(_))) => {
//! // Handle wrong passphrase or authentication failure
//! eprintln!("Wrong passphrase or integrity check failed");
//! }
//! Err(Error::Decrypt(DecryptError::Io(e))) => {
//! // Handle I/O errors during decryption
//! eprintln!("I/O error: {}", e);
//! }
//! Err(other) => eprintln!("Unexpected error: {}", other),
//! }
//! ```
//!
//! # Why two‑level error design?
//!
//! - **Separation of concerns**: Encryption and decryption are distinct
//! operations with different failure modes. Keeping them in separate
//! enums makes the code self‑documenting.
//! - **Extensibility**: In the future, we could add a `ConfigError` or
//! `KeyGenerationError` variant to the top‑level `Error` without
//! touching the encrypt/decrypt types.
//! - **Backward compatibility**: Adding a new variant to a public enum is
//! a breaking change. With the two‑level approach, we can sometimes
//! add variants to `DecryptError` without directly breaking the top‑level
//! `Error` ABI (though it still breaks exhaustive matches on
//! `DecryptError`). The outer `Error` remains stable as long as we
//! don't add a new variant there.
pub use DecryptError;
pub use EncryptError;
use Error;
/// The universal error type for this crate.
///
/// This enum represents every possible error that a function in this
/// library can return. It is a thin wrapper around the domain‑specific
/// error types:
///
/// - [`EncryptError`] for encryption‑related failures.
/// - [`DecryptError`] for decryption‑related failures.
///
/// The `#[from]` attribute generates `From<EncryptError>` and
/// `From<DecryptError>` implementations, so conversions are automatic
/// (see [module documentation](self) for details). This keeps the
/// public API clean – all functions return a single error type, yet
/// callers can still inspect the underlying cause.
///
/// # Display format
///
/// Each variant prefixes the error message with the domain:
///
/// ```text
/// Encrypt error: Invalid recipient 'abc': reason
/// Decrypt error: Invalid identity: something
/// ```
///
/// # Example of error propagation with `?`
///
/// ```rust
/// use age_crypto::{encrypt_with_passphrase, Error};
///
/// fn safe_encrypt(plaintext: &[u8], passphrase: &str) -> Result<Vec<u8>, Error> {
/// // encrypt_with_passphrase already returns Result<_, Error>
/// // so we can use `?` directly for propagation
/// let encrypted = encrypt_with_passphrase(plaintext, passphrase)?;
/// Ok(encrypted.as_bytes().to_vec())
/// }
/// ```
/// Convenient alias for `std::result::Result<T, crate::errors::Error>`.
///
/// All public API functions that can fail use this type. By importing
/// `crate::errors::Result`, you can write:
///
/// ```rust
/// use age_crypto::errors::Result;
///
/// fn my_function() -> Result<String> {
/// // Return Ok with a value
/// Ok(String::from("success"))
/// }
/// ```
///
/// without needing to specify `Error` explicitly. The type is re‑exported
/// from the crate root, so `age_crypto::Result` is also available.
///
/// # Example with error propagation
///
/// ```rust
/// use age_crypto::{encrypt_with_passphrase, errors::Result};
///
/// fn process_and_encrypt(data: &str, pass: &str) -> Result<Vec<u8>> {
/// // Any error from encrypt_with_passphrase is automatically
/// // converted to our crate::errors::Error via the `?` operator
/// let encrypted = encrypt_with_passphrase(data.as_bytes(), pass)?;
/// Ok(encrypted.as_bytes().to_vec())
/// }
/// ```
pub type Result<T> = Result;