Skip to main content

ValidationError

Enum ValidationError 

Source
pub enum ValidationError {
    FileNotFound {
        path: PathBuf,
        arg_name: String,
        suggestion: Option<String>,
    },
    InvalidExtension {
        arg_name: String,
        path: PathBuf,
        expected: Vec<String>,
    },
    OutOfRange {
        arg_name: String,
        value: f64,
        min: f64,
        max: f64,
        suggestion: Option<String>,
    },
    CustomConstraint {
        arg_name: String,
        reason: String,
        suggestion: Option<String>,
    },
    MissingDependency {
        arg_name: String,
        required_arg: String,
        suggestion: Option<String>,
    },
    MutuallyExclusive {
        arg1: String,
        arg2: String,
        suggestion: Option<String>,
    },
}
Expand description

Errors during argument validation

These errors occur after parsing, during validation of constraints defined in the configuration.

Variants§

§

FileNotFound

Required file doesn’t exist

§Example

use dynamic_cli::error::ValidationError;
use std::path::PathBuf;

let error = ValidationError::FileNotFound {
    path: PathBuf::from("data.csv"),
    arg_name: "input".to_string(),
    suggestion: Some("Check that the file exists and the path is correct.".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("data.csv"));

Fields

§path: PathBuf
§arg_name: String
§suggestion: Option<String>

Actionable hint surfaced to the user (not part of the Display string)

§

InvalidExtension

Invalid file extension

Fields

§arg_name: String
§path: PathBuf
§expected: Vec<String>
§

OutOfRange

Value out of allowed range

§Example

use dynamic_cli::error::ValidationError;

let error = ValidationError::OutOfRange {
    arg_name: "percentage".to_string(),
    value: 150.0,
    min: 0.0,
    max: 100.0,
    suggestion: Some("Value must be between 0 and 100.".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("150"));

Fields

§arg_name: String
§value: f64
§min: f64
§max: f64
§suggestion: Option<String>

Actionable hint surfaced to the user (not part of the Display string)

§

CustomConstraint

Custom constraint not met

§Example

use dynamic_cli::error::ValidationError;

let error = ValidationError::CustomConstraint {
    arg_name: "email".to_string(),
    reason: "not a valid email address".to_string(),
    suggestion: Some("Provide a valid email address (e.g. user@example.com).".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("email"));

Fields

§arg_name: String
§reason: String
§suggestion: Option<String>

Actionable hint surfaced to the user (not part of the Display string)

§

MissingDependency

Dependency between arguments not satisfied

Some arguments require the presence of other arguments.

§Example

use dynamic_cli::error::ValidationError;

let error = ValidationError::MissingDependency {
    arg_name: "output-format".to_string(),
    required_arg: "output".to_string(),
    suggestion: Some("Add --output to your command.".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("output-format"));

Fields

§arg_name: String
§required_arg: String
§suggestion: Option<String>

Actionable hint surfaced to the user (not part of the Display string)

§

MutuallyExclusive

Mutually exclusive arguments

Some arguments cannot be used together.

§Example

use dynamic_cli::error::ValidationError;

let error = ValidationError::MutuallyExclusive {
    arg1: "--verbose".to_string(),
    arg2: "--quiet".to_string(),
    suggestion: Some("Remove one of the two conflicting options.".to_string()),
};
let msg = format!("{}", error);
assert!(msg.contains("--verbose"));

Fields

§arg1: String
§arg2: String
§suggestion: Option<String>

Actionable hint surfaced to the user (not part of the Display string)

Trait Implementations§

Source§

impl Debug for ValidationError

Source§

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

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

impl Display for ValidationError

Source§

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

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

impl Error for ValidationError

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 From<ValidationError> for DynamicCliError

Source§

fn from(source: ValidationError) -> Self

Converts to this type from the input type.

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> 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> 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.