Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate: add .unwrap()-like functions #42

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions garde/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,43 @@ pub trait Validate {
/// Validates `Self`, returning an `Err` with an aggregate of all errors if
/// the validation failed.
fn validate(&self, ctx: &Self::Context) -> Result<(), Errors>;

/// Consumes and validates `self`, returning it un-touched if it
/// passes validation or the provided `other` version on failure.
fn validate_or(self, other: Self, ctx: &Self::Context) -> Self
where
Self: Sized,
{
match self.validate(ctx) {
Ok(_) => self,
Err(_) => other,
}
}

/// Consumes and validates `self`, returning it un-touched if it
/// passes validation or computes a new `Self` from closure `f` on failure.
fn validate_or_else<F>(self, f: F, ctx: &Self::Context) -> Self
where
Self: Sized,
F: FnOnce() -> Self,
{
match self.validate(ctx) {
Ok(_) => self,
Err(_) => f(),
}
}

/// Consumes and validates `self`, returning it un-touched if it
/// passes validation or a [`Default`] version on failure.
fn validate_or_default(self, ctx: &Self::Context) -> Self
where
Self: Default,
{
match self.validate(ctx) {
Ok(_) => self,
Err(_) => Default::default(),
}
}
}

/// A struct which wraps a valid instance of some `T`.
Expand Down Expand Up @@ -62,6 +99,43 @@ impl<T: Validate> Unvalidated<T> {
self.0.validate(ctx)?;
Ok(Valid(self.0))
}

/// Validates `self`, wrapped in [`Valid`].
///
/// - `self` is returned if it passes validation
/// - the provided `other` is returned on failure
pub fn validate_or(self, other: T, ctx: &<T as Validate>::Context) -> Valid<T> {
match self.0.validate(ctx) {
Ok(_) => Valid(self.0),
Err(_) => Valid(other),
}
}

/// Validates `self`, wrapped in [`Valid`].
///
/// - `self` is returned if it passes validation
/// - the output of closure `f` is returned on failure
pub fn validate_or_else<F>(self, f: F, ctx: &<T as Validate>::Context) -> Valid<T>
where
F: FnOnce() -> T,
{
match self.0.validate(ctx) {
Ok(_) => Valid(self.0),
Err(_) => Valid(f()),
}
}

/// Validates `self`, transforming it into a `Valid<T>` on sucesss
/// or a `Valid<T>` with [`Default`] on failure.
pub fn validate_or_default(self, ctx: &<T as Validate>::Context) -> Valid<T>
where
T: Default,
{
match self.0.validate(ctx) {
Ok(_) => Valid(self.0),
Err(_) => Valid(Default::default()),
}
}
}

impl<T: Validate> From<T> for Unvalidated<T> {
Expand Down