Skip to main content

KeyParseError

Enum KeyParseError 

Source
#[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
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

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

§character: char

The invalid character that was found

§position: usize

Position where the invalid character was found (0-based)

§expected: Option<&'static str>

Optional description of what characters are expected

§

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

§max_length: usize

The maximum allowed length for this domain

§actual_length: usize

The actual length of the key that was attempted

§

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

Fields

§reason: &'static str

Description of the structural issue

§

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

§domain: &'static str

The domain name where validation failed

§message: String

The error message describing what validation failed

§

Custom

Custom error for specific use cases

Applications can define custom validation errors with numeric codes for structured error handling.

Fields

§code: u32

Custom error code for programmatic handling

§message: String

The custom error message

Implementations§

Source§

impl KeyParseError

Source

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"),
}
Source

pub fn domain_error_generic(message: impl Into<String>) -> Self

Create a domain validation error without specifying domain (for internal use)

Source

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

Source

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);
Source

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

Source

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 key
  • 1002: Invalid character
  • 1003: Key too long
  • 1004: Invalid structure
  • 2000: Domain validation (base code)
  • Custom codes: As specified in Custom errors
§Examples
use domain_key::KeyParseError;

assert_eq!(KeyParseError::Empty.code(), 1001);
assert_eq!(KeyParseError::custom(42, "test").code(), 42);
Source

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"),
}
Source

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.

Source

pub fn suggestions(&self) -> Vec<&'static str>

Get suggested actions for fixing this error

Returns helpful suggestions for how to fix the validation error.

Source

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

Source§

fn clone(&self) -> KeyParseError

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KeyParseError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for KeyParseError

Source§

fn fmt(&self, __formatter: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for KeyParseError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl PartialEq for KeyParseError

Source§

fn eq(&self, other: &KeyParseError) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for KeyParseError

Source§

impl StructuralPartialEq for KeyParseError

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.