Skip to main content

domain_key/
error.rs

1//! Error types and error handling for domain-key
2//!
3//! This module provides comprehensive error handling for key validation and creation.
4//! All errors are designed to provide detailed information for debugging while maintaining
5//! performance in the happy path.
6
7use core::fmt;
8use thiserror::Error;
9
10#[cfg(not(feature = "std"))]
11use alloc::format;
12#[cfg(not(feature = "std"))]
13use alloc::string::String;
14
15use core::fmt::Write;
16
17// ============================================================================
18// CORE ERROR TYPES
19// ============================================================================
20
21/// Comprehensive error type for key parsing and validation failures
22///
23/// This enum covers all possible validation failures that can occur during
24/// key creation, providing detailed information for debugging and user feedback.
25///
26/// # Error Categories
27///
28/// - **Length Errors**: Empty keys or keys exceeding maximum length
29/// - **Character Errors**: Invalid characters at specific positions
30/// - **Structure Errors**: Invalid patterns like consecutive special characters
31/// - **Domain Errors**: Domain-specific validation failures
32/// - **Custom Errors**: Application-specific validation failures
33///
34/// # Examples
35///
36/// ```rust
37/// use domain_key::{KeyParseError, ErrorCategory};
38///
39/// // Handle different error types
40/// match KeyParseError::Empty {
41///     err => {
42///         println!("Error: {}", err);
43///         println!("Code: {}", err.code());
44///         println!("Category: {:?}", err.category());
45///     }
46/// }
47/// ```
48#[derive(Debug, Error, PartialEq, Eq, Clone)]
49#[non_exhaustive]
50pub enum KeyParseError {
51    /// Key cannot be empty or contain only whitespace
52    ///
53    /// This error occurs when attempting to create a key from an empty string
54    /// or a string containing only whitespace characters.
55    #[error("Key cannot be empty or whitespace")]
56    Empty,
57
58    /// Key contains a character that is not allowed at the specified position
59    ///
60    /// Each domain defines which characters are allowed. This error provides
61    /// the specific character, its position, and optionally what was expected.
62    #[error("Invalid character '{character}' at position {position}")]
63    InvalidCharacter {
64        /// The invalid character that was found
65        character: char,
66        /// Position where the invalid character was found (0-based)
67        position: usize,
68        /// Optional description of what characters are expected
69        expected: Option<&'static str>,
70    },
71
72    /// Key exceeds the maximum allowed length for the domain
73    ///
74    /// Each domain can specify a maximum length. This error provides both
75    /// the limit and the actual length that was attempted.
76    #[error("Key is too long (max {max_length} characters, got {actual_length})")]
77    TooLong {
78        /// The maximum allowed length for this domain
79        max_length: usize,
80        /// The actual length of the key that was attempted
81        actual_length: usize,
82    },
83
84    /// Key is shorter than the minimum allowed length for the domain
85    ///
86    /// Each domain can specify a minimum length. This error provides both
87    /// the required minimum and the actual length that was attempted.
88    #[error("Key is too short (min {min_length} characters, got {actual_length})")]
89    TooShort {
90        /// The minimum allowed length for this domain
91        min_length: usize,
92        /// The actual length of the key that was attempted
93        actual_length: usize,
94    },
95
96    /// Key has invalid structure (consecutive special chars, invalid start/end)
97    ///
98    /// This covers structural issues like:
99    /// - Starting or ending with special characters
100    /// - Consecutive special characters
101    /// - Invalid character sequences
102    #[error("Key has invalid structure: {reason}")]
103    InvalidStructure {
104        /// Description of the structural issue
105        reason: &'static str,
106    },
107
108    /// Domain-specific validation error
109    ///
110    /// This error is returned when domain-specific validation rules fail.
111    /// It includes the domain name and a descriptive message.
112    #[error("Domain '{domain}' validation failed: {message}")]
113    DomainValidation {
114        /// The domain name where validation failed
115        domain: &'static str,
116        /// The error message describing what validation failed
117        message: String,
118    },
119
120    /// Custom error for specific use cases
121    ///
122    /// Applications can define custom validation errors with numeric codes
123    /// for structured error handling.
124    #[error("Custom validation error (code: {code}): {message}")]
125    Custom {
126        /// Custom error code for programmatic handling
127        code: u32,
128        /// The custom error message
129        message: String,
130    },
131}
132
133impl KeyParseError {
134    /// Create a domain validation error with domain name
135    ///
136    /// This is the preferred way to create domain validation errors.
137    ///
138    /// # Examples
139    ///
140    /// ```rust
141    /// use domain_key::KeyParseError;
142    ///
143    /// let error = KeyParseError::domain_error("my_domain", "Custom validation failed");
144    /// // Verify it's the correct error type
145    /// match error {
146    ///     KeyParseError::DomainValidation { domain, message } => {
147    ///         assert_eq!(domain, "my_domain");
148    ///         assert!(message.contains("Custom validation failed"));
149    ///     },
150    ///     _ => panic!("Expected domain validation error"),
151    /// }
152    /// ```
153    pub fn domain_error(domain: &'static str, message: impl Into<String>) -> Self {
154        Self::DomainValidation {
155            domain,
156            message: message.into(),
157        }
158    }
159
160    /// Create a domain validation error without specifying domain (for internal use)
161    pub fn domain_error_generic(message: impl Into<String>) -> Self {
162        Self::DomainValidation {
163            domain: "unknown",
164            message: message.into(),
165        }
166    }
167
168    /// Create a domain validation error with source error information
169    ///
170    /// The source error's `Display` representation is appended to `message`,
171    /// separated by `": "`.  This is a **flattened** representation — the
172    /// resulting `KeyParseError` does **not** implement `std::error::Error::source()`
173    /// chaining back to the original error.  In other words, `error.source()`
174    /// will return `None`, and tools such as `anyhow` / `tracing` that walk the
175    /// error chain will not see the wrapped cause.
176    ///
177    /// If you need a proper causal chain, wrap the original error in your own
178    /// error type (e.g. via `anyhow::Error` or a `thiserror` wrapper) before
179    /// converting it to a `KeyParseError`.
180    ///
181    /// # Limitation
182    ///
183    /// Properly storing a `Box<dyn Error + Send + Sync>` inside `KeyParseError`
184    /// variants would require either a new variant or a breaking field change.
185    /// Until that refactor is done, the full error context is preserved only in
186    /// the formatted message string.
187    #[cfg(feature = "std")]
188    pub fn domain_error_with_source(
189        domain: &'static str,
190        message: impl Into<String>,
191        source: &(dyn std::error::Error + Send + Sync),
192    ) -> Self {
193        let full_message = format!("{}: {}", message.into(), source);
194        Self::DomainValidation {
195            domain,
196            message: full_message,
197        }
198    }
199
200    /// Create a custom validation error
201    ///
202    /// Custom errors allow applications to define their own error codes
203    /// for structured error handling.
204    ///
205    /// # Examples
206    ///
207    /// ```rust
208    /// use domain_key::KeyParseError;
209    ///
210    /// let error = KeyParseError::custom(1001, "Business rule violation");
211    /// assert_eq!(error.code(), 1001);
212    /// ```
213    pub fn custom(code: u32, message: impl Into<String>) -> Self {
214        Self::Custom {
215            code,
216            message: message.into(),
217        }
218    }
219
220    /// Create a custom validation error with source error information
221    ///
222    /// The source error's `Display` representation is appended to `message`,
223    /// separated by `": "`.  This is a **flattened** representation — the
224    /// resulting `KeyParseError` does **not** implement `std::error::Error::source()`
225    /// chaining back to the original error.  In other words, `error.source()`
226    /// will return `None`, and tools such as `anyhow` / `tracing` that walk the
227    /// error chain will not see the wrapped cause.
228    ///
229    /// If you need a proper causal chain, wrap the original error in your own
230    /// error type (e.g. via `anyhow::Error` or a `thiserror` wrapper) before
231    /// converting it to a `KeyParseError`.
232    ///
233    /// # Limitation
234    ///
235    /// Properly storing a `Box<dyn Error + Send + Sync>` inside `KeyParseError`
236    /// variants would require either a new variant or a breaking field change.
237    /// Until that refactor is done, the full error context is preserved only in
238    /// the formatted message string.
239    #[cfg(feature = "std")]
240    pub fn custom_with_source(
241        code: u32,
242        message: impl Into<String>,
243        source: &(dyn std::error::Error + Send + Sync),
244    ) -> Self {
245        let full_message = format!("{}: {}", message.into(), source);
246        Self::Custom {
247            code,
248            message: full_message,
249        }
250    }
251
252    /// Get the error code for machine processing
253    ///
254    /// Returns a numeric code that can be used for programmatic error handling.
255    /// This is useful for APIs that need to return structured error responses.
256    ///
257    /// # Error Codes
258    ///
259    /// - `1001`: Empty key
260    /// - `1002`: Invalid character
261    /// - `1003`: Key too long
262    /// - `1004`: Invalid structure
263    /// - `2000`: Domain validation (base code)
264    /// - Custom codes: As specified in `Custom` errors
265    ///
266    /// # Examples
267    ///
268    /// ```rust
269    /// use domain_key::KeyParseError;
270    ///
271    /// assert_eq!(KeyParseError::Empty.code(), 1001);
272    /// assert_eq!(KeyParseError::custom(42, "test").code(), 42);
273    /// ```
274    #[must_use]
275    pub const fn code(&self) -> u32 {
276        match self {
277            Self::Empty => 1001,
278            Self::InvalidCharacter { .. } => 1002,
279            Self::TooLong { .. } => 1003,
280            Self::InvalidStructure { .. } => 1004,
281            Self::TooShort { .. } => 1005,
282            Self::DomainValidation { .. } => 2000,
283            Self::Custom { code, .. } => *code,
284        }
285    }
286
287    /// Get the error category for classification
288    ///
289    /// Returns the general category of this error for higher-level error handling.
290    /// This allows applications to handle broad categories of errors uniformly.
291    ///
292    /// # Examples
293    ///
294    /// ```rust
295    /// use domain_key::{KeyParseError, ErrorCategory};
296    ///
297    /// match KeyParseError::Empty.category() {
298    ///     ErrorCategory::Length => println!("Length-related error"),
299    ///     ErrorCategory::Character => println!("Character-related error"),
300    ///     _ => println!("Other error type"),
301    /// }
302    /// ```
303    #[must_use]
304    pub const fn category(&self) -> ErrorCategory {
305        match self {
306            Self::Empty | Self::TooLong { .. } | Self::TooShort { .. } => ErrorCategory::Length,
307            Self::InvalidCharacter { .. } => ErrorCategory::Character,
308            Self::InvalidStructure { .. } => ErrorCategory::Structure,
309            Self::DomainValidation { .. } => ErrorCategory::Domain,
310            Self::Custom { code, .. } => match code {
311                1002 => ErrorCategory::Character,
312                1003 => ErrorCategory::Length,
313                1004 => ErrorCategory::Structure,
314                _ => ErrorCategory::Custom,
315            },
316        }
317    }
318
319    /// Get a human-readable description of what went wrong
320    ///
321    /// This provides additional context beyond the basic error message,
322    /// useful for user-facing error messages or debugging.
323    #[must_use]
324    pub fn description(&self) -> &'static str {
325        match self {
326            Self::Empty => "Key cannot be empty or contain only whitespace characters",
327            Self::InvalidCharacter { .. } => {
328                "Key contains characters that are not allowed by the domain"
329            }
330            Self::TooLong { .. } => "Key exceeds the maximum length allowed by the domain",
331            Self::TooShort { .. } => {
332                "Key is shorter than the minimum length required by the domain"
333            }
334            Self::InvalidStructure { .. } => "Key has invalid structure or formatting",
335            Self::DomainValidation { .. } => "Key fails domain-specific validation rules",
336            Self::Custom { .. } => "Key fails custom validation rules",
337        }
338    }
339
340    /// Get suggested actions for fixing this error
341    ///
342    /// Returns helpful suggestions for how to fix the validation error.
343    #[must_use]
344    pub fn suggestions(&self) -> &'static [&'static str] {
345        match self {
346            Self::Empty => &[
347                "Provide a non-empty key",
348                "Remove leading/trailing whitespace",
349            ],
350            Self::InvalidCharacter { .. } => &[
351                "Use only allowed characters (check domain rules)",
352                "Remove or replace invalid characters",
353            ],
354            Self::TooLong { .. } => &[
355                "Shorten the key to fit within length limits",
356                "Consider using abbreviated forms",
357            ],
358            Self::TooShort { .. } => &["Lengthen the key to meet the minimum length requirement"],
359            Self::InvalidStructure { .. } => &[
360                "Avoid consecutive special characters",
361                "Don't start or end with special characters",
362                "Follow the expected key format",
363            ],
364            Self::DomainValidation { .. } => &[
365                "Check domain-specific validation rules",
366                "Refer to domain documentation",
367            ],
368            Self::Custom { .. } => &[
369                "Check application-specific validation rules",
370                "Contact system administrator if needed",
371            ],
372        }
373    }
374
375    /// Check if this error is recoverable through user action
376    ///
377    /// Returns `true` if the user can potentially fix this error by modifying
378    /// their input, `false` if it represents a programming error or system issue.
379    #[must_use]
380    pub const fn is_recoverable(&self) -> bool {
381        match self {
382            Self::Empty
383            | Self::InvalidCharacter { .. }
384            | Self::TooLong { .. }
385            | Self::TooShort { .. }
386            | Self::InvalidStructure { .. }
387            | Self::DomainValidation { .. } => true,
388            Self::Custom { .. } => false, // Depends on the specific custom error
389        }
390    }
391}
392
393// ============================================================================
394// ERROR CATEGORIES
395// ============================================================================
396
397/// Error category for classification of validation errors
398///
399/// These categories allow applications to handle broad types of validation
400/// errors uniformly, regardless of the specific error details.
401#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
402#[non_exhaustive]
403pub enum ErrorCategory {
404    /// Length-related errors (empty, too long)
405    Length,
406    /// Character-related errors (invalid characters)
407    Character,
408    /// Structure-related errors (invalid format, consecutive special chars)
409    Structure,
410    /// Domain-specific validation errors
411    Domain,
412    /// Custom validation errors
413    Custom,
414}
415
416impl ErrorCategory {
417    /// Get a human-readable name for this category
418    #[must_use]
419    pub const fn name(self) -> &'static str {
420        match self {
421            Self::Length => "Length",
422            Self::Character => "Character",
423            Self::Structure => "Structure",
424            Self::Domain => "Domain",
425            Self::Custom => "Custom",
426        }
427    }
428
429    /// Get a description of what this category represents
430    #[must_use]
431    pub const fn description(self) -> &'static str {
432        match self {
433            Self::Length => "Errors related to key length (empty, too long, etc.)",
434            Self::Character => "Errors related to invalid characters in the key",
435            Self::Structure => "Errors related to key structure and formatting",
436            Self::Domain => "Errors from domain-specific validation rules",
437            Self::Custom => "Custom application-specific validation errors",
438        }
439    }
440}
441
442impl fmt::Display for ErrorCategory {
443    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444        write!(f, "{}", self.name())
445    }
446}
447
448// ============================================================================
449// ID PARSE ERROR
450// ============================================================================
451
452/// Error type for numeric ID parsing failures
453///
454/// This error is returned when a string cannot be parsed as a valid `Id<D>`.
455///
456/// # Examples
457///
458/// ```rust
459/// use domain_key::IdParseError;
460///
461/// let result = "not_a_number".parse::<u64>();
462/// assert!(result.is_err());
463/// ```
464#[derive(Debug, Error, PartialEq, Eq, Clone)]
465#[non_exhaustive]
466pub enum IdParseError {
467    /// The value is zero, which is not a valid identifier
468    #[error("ID cannot be zero")]
469    Zero,
470
471    /// The string could not be parsed as a valid non-zero u64 number
472    #[error("Invalid numeric ID: {0}")]
473    InvalidNumber(#[from] core::num::ParseIntError),
474}
475
476impl IdParseError {
477    /// Returns a user-friendly error message suitable for display.
478    #[must_use]
479    pub fn user_message(&self) -> &'static str {
480        match self {
481            Self::Zero => "Identifier cannot be zero",
482            Self::InvalidNumber(_) => "Value must be a positive integer",
483        }
484    }
485}
486
487// ============================================================================
488// UUID PARSE ERROR
489// ============================================================================
490
491/// Error type for UUID parsing failures
492///
493/// This error is returned when a string cannot be parsed as a valid `Uuid<D>`.
494/// Only available when the `uuid` feature is enabled.
495///
496/// Note: `UuidParseError` does not implement `PartialEq` because
497/// `uuid::Error` does not implement it.
498#[cfg(feature = "uuid")]
499#[derive(Debug, Error, Clone)]
500#[non_exhaustive]
501pub enum UuidParseError {
502    /// The string could not be parsed as a valid UUID
503    #[error("Invalid UUID: {0}")]
504    InvalidUuid(#[from] ::uuid::Error),
505}
506
507// ============================================================================
508// ULID PARSE ERROR
509// ============================================================================
510
511/// Error type for prefixed ULID parsing failures
512///
513/// Returned when a string cannot be parsed as [`crate::Ulid<D>`](crate::Ulid): wrong
514/// `{PREFIX}_` prefix or invalid Crockford Base32 body.
515///
516/// Only available when the `ulid` feature is enabled.
517#[cfg(feature = "ulid")]
518#[derive(Debug, Error, PartialEq, Eq, Clone)]
519#[non_exhaustive]
520pub enum UlidParseError {
521    /// The string does not start with `"{PREFIX}_"` for this domain
522    #[error("invalid ULID prefix: expected `{expected_prefix}_...`")]
523    WrongPrefix {
524        /// Expected [`UlidDomain::PREFIX`](crate::UlidDomain::PREFIX) value
525        expected_prefix: &'static str,
526    },
527    /// The ULID body (after the prefix) is not valid Crockford Base32
528    #[error("invalid ULID: {0}")]
529    InvalidUlid(::ulid::DecodeError),
530}
531
532// ============================================================================
533// ERROR UTILITIES
534// ============================================================================
535
536/// Builder for creating detailed validation errors
537///
538/// This builder provides a fluent interface for creating complex validation
539/// errors with additional context and suggestions.
540#[derive(Debug)]
541pub struct ErrorBuilder {
542    category: ErrorCategory,
543    code: Option<u32>,
544    domain: Option<&'static str>,
545    message: String,
546    context: Option<String>,
547}
548
549impl ErrorBuilder {
550    /// Create a new error builder for the given category
551    #[must_use]
552    pub fn new(category: ErrorCategory) -> Self {
553        Self {
554            category,
555            code: None,
556            domain: None,
557            message: String::new(),
558            context: None,
559        }
560    }
561
562    /// Set the error message
563    #[must_use]
564    pub fn message(mut self, message: impl Into<String>) -> Self {
565        self.message = message.into();
566        self
567    }
568
569    /// Set a custom error code (used when category is `Custom`)
570    #[must_use]
571    pub fn code(mut self, code: u32) -> Self {
572        self.code = Some(code);
573        self
574    }
575
576    /// Set the domain name (used when category is `Domain`)
577    #[must_use]
578    pub fn domain(mut self, domain: &'static str) -> Self {
579        self.domain = Some(domain);
580        self
581    }
582
583    /// Set additional context information
584    #[must_use]
585    pub fn context(mut self, context: impl Into<String>) -> Self {
586        self.context = Some(context.into());
587        self
588    }
589
590    /// Build the final error
591    #[must_use]
592    pub fn build(self) -> KeyParseError {
593        let message = if let Some(context) = self.context {
594            format!("{} (Context: {})", self.message, context)
595        } else {
596            self.message
597        };
598
599        match self.category {
600            ErrorCategory::Custom => KeyParseError::custom(self.code.unwrap_or(0), message),
601            ErrorCategory::Domain => {
602                KeyParseError::domain_error(self.domain.unwrap_or("unknown"), message)
603            }
604            // Use the reserved structural codes so that category() round-trips
605            // correctly back to the originally-specified category.
606            // 1004 → ErrorCategory::Structure, 1003 → ErrorCategory::Length,
607            // 1002 → ErrorCategory::Character  (mirroring code() for each variant)
608            ErrorCategory::Structure => KeyParseError::custom(1004, message),
609            ErrorCategory::Length => KeyParseError::custom(1003, message),
610            ErrorCategory::Character => KeyParseError::custom(1002, message),
611        }
612    }
613}
614
615// ============================================================================
616// ERROR FORMATTING UTILITIES
617// ============================================================================
618
619/// Format an error for display to end users
620///
621/// This function provides a user-friendly representation of validation errors,
622/// including suggestions for how to fix them.
623#[must_use]
624pub fn format_user_error(error: &KeyParseError) -> String {
625    let mut output = format!("Error: {error}");
626
627    let suggestions = error.suggestions();
628    if !suggestions.is_empty() {
629        output.push_str("\n\nSuggestions:");
630        for suggestion in suggestions {
631            write!(output, "\n  - {suggestion}").unwrap();
632        }
633    }
634
635    output
636}
637
638/// Format an error for logging or debugging
639///
640/// This function provides a detailed representation suitable for logs,
641/// including error codes and categories.
642#[must_use]
643pub fn format_debug_error(error: &KeyParseError) -> String {
644    format!(
645        "[{}:{}] {} (Category: {})",
646        error.code(),
647        error.category().name(),
648        error,
649        error.description()
650    )
651}
652
653// ============================================================================
654// TESTS
655// ============================================================================
656
657#[cfg(test)]
658mod tests {
659    use super::*;
660
661    #[cfg(not(feature = "std"))]
662    use alloc::string::ToString;
663
664    #[test]
665    fn each_variant_has_unique_error_code() {
666        assert_eq!(KeyParseError::Empty.code(), 1001);
667        assert_eq!(
668            KeyParseError::InvalidCharacter {
669                character: 'x',
670                position: 0,
671                expected: None
672            }
673            .code(),
674            1002
675        );
676        assert_eq!(
677            KeyParseError::TooLong {
678                max_length: 10,
679                actual_length: 20
680            }
681            .code(),
682            1003
683        );
684        assert_eq!(
685            KeyParseError::InvalidStructure { reason: "test" }.code(),
686            1004
687        );
688        assert_eq!(
689            KeyParseError::TooShort {
690                min_length: 5,
691                actual_length: 2
692            }
693            .code(),
694            1005
695        );
696        assert_eq!(
697            KeyParseError::DomainValidation {
698                domain: "test",
699                message: "msg".to_string()
700            }
701            .code(),
702            2000
703        );
704        assert_eq!(
705            KeyParseError::Custom {
706                code: 42,
707                message: "msg".to_string()
708            }
709            .code(),
710            42
711        );
712    }
713
714    #[test]
715    fn variants_map_to_correct_category() {
716        assert_eq!(KeyParseError::Empty.category(), ErrorCategory::Length);
717        assert_eq!(
718            KeyParseError::InvalidCharacter {
719                character: 'x',
720                position: 0,
721                expected: None
722            }
723            .category(),
724            ErrorCategory::Character
725        );
726        assert_eq!(
727            KeyParseError::TooLong {
728                max_length: 10,
729                actual_length: 20
730            }
731            .category(),
732            ErrorCategory::Length
733        );
734        assert_eq!(
735            KeyParseError::InvalidStructure { reason: "test" }.category(),
736            ErrorCategory::Structure
737        );
738        assert_eq!(
739            KeyParseError::TooShort {
740                min_length: 5,
741                actual_length: 2
742            }
743            .category(),
744            ErrorCategory::Length
745        );
746        assert_eq!(
747            KeyParseError::DomainValidation {
748                domain: "test",
749                message: "msg".to_string()
750            }
751            .category(),
752            ErrorCategory::Domain
753        );
754        assert_eq!(
755            KeyParseError::Custom {
756                code: 42,
757                message: "msg".to_string()
758            }
759            .category(),
760            ErrorCategory::Custom
761        );
762    }
763
764    #[test]
765    fn empty_error_provides_recovery_suggestions() {
766        let error = KeyParseError::Empty;
767        let suggestions = error.suggestions();
768        assert!(!suggestions.is_empty());
769        assert!(suggestions.iter().any(|s| s.contains("non-empty")));
770    }
771
772    #[test]
773    fn builder_produces_custom_error_with_code_and_context() {
774        let error = ErrorBuilder::new(ErrorCategory::Custom)
775            .message("Test error")
776            .code(1234)
777            .context("In test function")
778            .build();
779
780        assert_eq!(error.code(), 1234);
781        assert_eq!(error.category(), ErrorCategory::Custom);
782    }
783
784    #[test]
785    fn variants_carry_correct_payloads() {
786        let error1 = KeyParseError::InvalidCharacter {
787            character: '!',
788            position: 5,
789            expected: Some("alphanumeric"),
790        };
791        assert!(matches!(
792            error1,
793            KeyParseError::InvalidCharacter {
794                character: '!',
795                position: 5,
796                ..
797            }
798        ));
799
800        let error2 = KeyParseError::TooLong {
801            max_length: 32,
802            actual_length: 64,
803        };
804        assert!(matches!(
805            error2,
806            KeyParseError::TooLong {
807                max_length: 32,
808                actual_length: 64
809            }
810        ));
811
812        let error3 = KeyParseError::InvalidStructure {
813            reason: "consecutive underscores",
814        };
815        assert!(matches!(
816            error3,
817            KeyParseError::InvalidStructure {
818                reason: "consecutive underscores"
819            }
820        ));
821
822        let error4 = KeyParseError::domain_error("test", "Invalid format");
823        assert!(matches!(
824            error4,
825            KeyParseError::DomainValidation { domain: "test", .. }
826        ));
827    }
828
829    #[test]
830    fn user_and_debug_formats_include_expected_sections() {
831        let error = KeyParseError::Empty;
832        let user_format = format_user_error(&error);
833        let debug_format = format_debug_error(&error);
834
835        assert!(user_format.contains("Error:"));
836        assert!(user_format.contains("Suggestions:"));
837        assert!(debug_format.contains("1001"));
838        assert!(debug_format.contains("Length"));
839    }
840
841    #[test]
842    fn recoverable_errors_distinguished_from_non_recoverable() {
843        assert!(KeyParseError::Empty.is_recoverable());
844        assert!(KeyParseError::InvalidCharacter {
845            character: 'x',
846            position: 0,
847            expected: None
848        }
849        .is_recoverable());
850        assert!(KeyParseError::TooShort {
851            min_length: 5,
852            actual_length: 2
853        }
854        .is_recoverable());
855        assert!(!KeyParseError::Custom {
856            code: 42,
857            message: "msg".to_string()
858        }
859        .is_recoverable());
860    }
861
862    #[test]
863    fn category_display_and_description_are_populated() {
864        assert_eq!(ErrorCategory::Length.to_string(), "Length");
865        assert_eq!(ErrorCategory::Character.name(), "Character");
866        assert!(ErrorCategory::Domain
867            .description()
868            .contains("domain-specific"));
869    }
870}