pub enum BuildError {
MissingRequired {
field: &'static str,
},
InvalidValue {
field: &'static str,
reason: String,
},
InvalidRange {
field: &'static str,
min: u64,
max: u64,
},
ConflictingOptions {
option_a: &'static str,
option_b: &'static str,
},
InvalidProbability {
field: &'static str,
value: f64,
},
InvalidDuration {
field: &'static str,
reason: String,
},
DependencyMissing {
feature: &'static str,
dependency: &'static str,
},
Custom {
message: String,
},
}Expand description
Errors that can occur when building a configuration.
BuildError captures validation failures, constraint violations, and
configuration errors that can occur during the build() call.
§Error Categories
| Category | Description | Example |
|---|---|---|
MissingRequired | Required field not set | name field is None |
InvalidValue | Value fails validation | threads = 0 |
InvalidRange | Range constraints violated | min > max |
ConflictingOptions | Mutually exclusive options | sync and async both set |
InvalidProbability | Probability not in [0.0, 1.0] | 0.5..=1.5 |
InvalidDuration | Duration constraint violated | timeout = 0 |
DependencyMissing | Required dependency not configured | tls without certificates |
Custom | Domain-specific validation errors | Application-specific rules |
§Usage
fn build(self) -> BuildResult<Config> {
// Check required fields
let name = self.name.ok_or_else(||
BuildError::missing_required("name")
)?;
// Validate values
if self.threads == 0 {
return Err(BuildError::invalid_value("threads", "must be >= 1"));
}
// Check ranges
if self.min_connections > self.max_connections {
return Err(BuildError::invalid_range(
"connections",
self.min_connections,
self.max_connections,
));
}
Ok(Config { name, threads: self.threads, ... })
}Variants§
MissingRequired
InvalidValue
A field value failed validation.
§Example
BuildError::InvalidValue {
field: "worker_threads",
reason: "must be >= 1".to_string(),
}Fields
InvalidRange
A range constraint was violated (min > max).
§Example
BuildError::InvalidRange {
field: "connections",
min: 100,
max: 10,
}Fields
ConflictingOptions
Two options that cannot both be enabled were set.
§Example
BuildError::ConflictingOptions {
option_a: "single_threaded",
option_b: "work_stealing",
}Fields
InvalidProbability
A probability value was not in [0.0, 1.0].
§Example
BuildError::InvalidProbability {
field: "cancel_probability",
value: 1.5,
}Fields
InvalidDuration
A duration value violated constraints.
§Example
BuildError::InvalidDuration {
field: "timeout",
reason: "must be non-zero".to_string(),
}Fields
DependencyMissing
A dependency required by the configuration is missing.
§Example
BuildError::DependencyMissing {
feature: "tls",
dependency: "certificate",
}Fields
Custom
A custom validation error with arbitrary message.
Use this for domain-specific validation that doesn’t fit other variants.
Implementations§
Source§impl BuildError
impl BuildError
Sourcepub const fn missing_required(field: &'static str) -> Self
pub const fn missing_required(field: &'static str) -> Self
Creates a MissingRequired error.
Sourcepub fn invalid_value(field: &'static str, reason: impl Into<String>) -> Self
pub fn invalid_value(field: &'static str, reason: impl Into<String>) -> Self
Creates an InvalidValue error.
Sourcepub const fn invalid_range(field: &'static str, min: u64, max: u64) -> Self
pub const fn invalid_range(field: &'static str, min: u64, max: u64) -> Self
Creates an InvalidRange error.
Sourcepub const fn conflicting_options(
option_a: &'static str,
option_b: &'static str,
) -> Self
pub const fn conflicting_options( option_a: &'static str, option_b: &'static str, ) -> Self
Creates a ConflictingOptions error.
Sourcepub const fn invalid_probability(field: &'static str, value: f64) -> Self
pub const fn invalid_probability(field: &'static str, value: f64) -> Self
Creates an InvalidProbability error.
Sourcepub fn invalid_duration(field: &'static str, reason: impl Into<String>) -> Self
pub fn invalid_duration(field: &'static str, reason: impl Into<String>) -> Self
Creates an InvalidDuration error.
Sourcepub const fn dependency_missing(
feature: &'static str,
dependency: &'static str,
) -> Self
pub const fn dependency_missing( feature: &'static str, dependency: &'static str, ) -> Self
Creates a DependencyMissing error.
Sourcepub const fn field(&self) -> Option<&'static str>
pub const fn field(&self) -> Option<&'static str>
Returns the field name associated with this error, if any.
Sourcepub const fn is_missing_required(&self) -> bool
pub const fn is_missing_required(&self) -> bool
Returns true if this is a missing required field error.
Sourcepub const fn is_invalid_value(&self) -> bool
pub const fn is_invalid_value(&self) -> bool
Returns true if this is an invalid value error.
Sourcepub const fn is_conflicting_options(&self) -> bool
pub const fn is_conflicting_options(&self) -> bool
Returns true if this is a conflicting options error.
Trait Implementations§
Source§impl Clone for BuildError
impl Clone for BuildError
Source§fn clone(&self) -> BuildError
fn clone(&self) -> BuildError
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for BuildError
impl Debug for BuildError
Source§impl Display for BuildError
impl Display for BuildError
Source§impl Error for BuildError
impl Error for BuildError
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()