#[non_exhaustive]pub enum KeyParseError {
Empty,
InvalidCharacter {
character: char,
position: usize,
expected: Option<&'static str>,
},
TooLong {
max_length: usize,
actual_length: usize,
},
InvalidStructure {
reason: &'static str,
},
DomainValidation {
domain: &'static str,
message: String,
},
Custom {
code: u32,
message: String,
},
}Expand description
Comprehensive error type for key parsing and validation failures
This enum covers all possible validation failures that can occur during key creation, providing detailed information for debugging and user feedback.
§Error Categories
- Length Errors: Empty keys or keys exceeding maximum length
- Character Errors: Invalid characters at specific positions
- Structure Errors: Invalid patterns like consecutive special characters
- Domain Errors: Domain-specific validation failures
- Custom Errors: Application-specific validation failures
§Examples
use domain_key::{KeyParseError, ErrorCategory};
// Handle different error types
match KeyParseError::Empty {
err => {
println!("Error: {}", err);
println!("Code: {}", err.code());
println!("Category: {:?}", err.category());
}
}Variants (Non-exhaustive)§
This enum is marked as non-exhaustive
Empty
Key cannot be empty or contain only whitespace
This error occurs when attempting to create a key from an empty string or a string containing only whitespace characters.
InvalidCharacter
Key contains a character that is not allowed at the specified position
Each domain defines which characters are allowed. This error provides the specific character, its position, and optionally what was expected.
Fields
TooLong
Key exceeds the maximum allowed length for the domain
Each domain can specify a maximum length. This error provides both the limit and the actual length that was attempted.
Fields
InvalidStructure
Key has invalid structure (consecutive special chars, invalid start/end)
This covers structural issues like:
- Starting or ending with special characters
- Consecutive special characters
- Invalid character sequences
DomainValidation
Domain-specific validation error
This error is returned when domain-specific validation rules fail. It includes the domain name and a descriptive message.
Fields
Custom
Custom error for specific use cases
Applications can define custom validation errors with numeric codes for structured error handling.
Implementations§
Source§impl KeyParseError
impl KeyParseError
Sourcepub fn domain_error(domain: &'static str, message: impl Into<String>) -> Self
pub fn domain_error(domain: &'static str, message: impl Into<String>) -> Self
Create a domain validation error with domain name
This is the preferred way to create domain validation errors.
§Examples
use domain_key::KeyParseError;
let error = KeyParseError::domain_error("my_domain", "Custom validation failed");
// Verify it's the correct error type
match error {
KeyParseError::DomainValidation { domain, message } => {
assert_eq!(domain, "my_domain");
assert!(message.contains("Custom validation failed"));
},
_ => panic!("Expected domain validation error"),
}Sourcepub fn domain_error_generic(message: impl Into<String>) -> Self
pub fn domain_error_generic(message: impl Into<String>) -> Self
Create a domain validation error without specifying domain (for internal use)
Sourcepub fn domain_error_with_source(
domain: &'static str,
message: impl Into<String>,
source: &(dyn Error + Send + Sync),
) -> Self
pub fn domain_error_with_source( domain: &'static str, message: impl Into<String>, source: &(dyn Error + Send + Sync), ) -> Self
Create a domain validation error with source error information
Sourcepub fn custom(code: u32, message: impl Into<String>) -> Self
pub fn custom(code: u32, message: impl Into<String>) -> Self
Create a custom validation error
Custom errors allow applications to define their own error codes for structured error handling.
§Examples
use domain_key::KeyParseError;
let error = KeyParseError::custom(1001, "Business rule violation");
assert_eq!(error.code(), 1001);Sourcepub fn custom_with_source(
code: u32,
message: impl Into<String>,
source: &(dyn Error + Send + Sync),
) -> Self
pub fn custom_with_source( code: u32, message: impl Into<String>, source: &(dyn Error + Send + Sync), ) -> Self
Create a custom validation error with source error information
Sourcepub const fn code(&self) -> u32
pub const fn code(&self) -> u32
Get the error code for machine processing
Returns a numeric code that can be used for programmatic error handling. This is useful for APIs that need to return structured error responses.
§Error Codes
1001: Empty key1002: Invalid character1003: Key too long1004: Invalid structure2000: Domain validation (base code)- Custom codes: As specified in
Customerrors
§Examples
use domain_key::KeyParseError;
assert_eq!(KeyParseError::Empty.code(), 1001);
assert_eq!(KeyParseError::custom(42, "test").code(), 42);Sourcepub const fn category(&self) -> ErrorCategory
pub const fn category(&self) -> ErrorCategory
Get the error category for classification
Returns the general category of this error for higher-level error handling. This allows applications to handle broad categories of errors uniformly.
§Examples
use domain_key::{KeyParseError, ErrorCategory};
match KeyParseError::Empty.category() {
ErrorCategory::Length => println!("Length-related error"),
ErrorCategory::Character => println!("Character-related error"),
_ => println!("Other error type"),
}Sourcepub fn description(&self) -> &'static str
pub fn description(&self) -> &'static str
Get a human-readable description of what went wrong
This provides additional context beyond the basic error message, useful for user-facing error messages or debugging.
Sourcepub fn suggestions(&self) -> Vec<&'static str>
pub fn suggestions(&self) -> Vec<&'static str>
Get suggested actions for fixing this error
Returns helpful suggestions for how to fix the validation error.
Sourcepub const fn is_recoverable(&self) -> bool
pub const fn is_recoverable(&self) -> bool
Check if this error is recoverable through user action
Returns true if the user can potentially fix this error by modifying
their input, false if it represents a programming error or system issue.
Trait Implementations§
Source§impl Clone for KeyParseError
impl Clone for KeyParseError
Source§fn clone(&self) -> KeyParseError
fn clone(&self) -> KeyParseError
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more