pub enum MaybeValidOwned<V: Validated, P> {
Valid(V),
Invalid(P, V::InvalidReason),
}Expand description
The outcome of consuming a value into a validated form of type V.
Returned by IntoValidated::into_validated. The Valid variant
holds the constructed V; the Invalid variant holds the original
precursor (returned unchanged, by move) alongside the diagnostic.
§Why both variants matter
MaybeValidOwned is deliberately not a Result. The Invalid
variant is not an error to handle and discard: it returns the
caller’s input to them, intact, so they can retry, repair, log,
or fall through to an alternative. Routing this through Result
would frame precursor recovery as error-handling boilerplate;
MaybeValidOwned frames it as a first-class outcome.
The precursor is returned by move, not by clone. The trait does
not require Self: Clone, and no allocation or duplication
occurs on the invalid path beyond what constructing the
diagnostic requires.
§Examples
let bytes = vec![0xff, 0xfe];
let validated: MaybeValidOwned<String, Vec<u8>> = bytes.into_validated();
match validated {
MaybeValidOwned::Valid(s) => println!("got: {}", s),
MaybeValidOwned::Invalid(bytes, reason) => {
// `bytes` is the original Vec<u8>, moved back to us.
assert_eq!(bytes, vec![0xff, 0xfe]);
eprintln!("invalid at byte {}", reason.valid_up_to());
}
}§Construction
MaybeValidOwned values are produced by IntoValidated
implementations. Direct construction is public and unrestricted:
V’s own invariants are enforced by V, not by this enum.
Variants§
Valid(V)
The precursor satisfied V’s predicate.
Holds the constructed validated value.
Invalid(P, V::InvalidReason)
The precursor did not satisfy V’s predicate.
Holds the original precursor (returned unchanged, by move) and the diagnostic reason.
Implementations§
Source§impl<V: Validated, P> MaybeValidOwned<V, P>
impl<V: Validated, P> MaybeValidOwned<V, P>
Sourcepub fn is_invalid(&self) -> bool
pub fn is_invalid(&self) -> bool
Returns true if this is the Invalid variant.
Sourcepub fn valid(self) -> Option<V>
pub fn valid(self) -> Option<V>
Returns the validated value, or None if invalid.
Discards the precursor on the invalid path; use
invalid_parts to retain both the precursor and the reason.
Sourcepub fn invalid_precursor(self) -> Option<P>
pub fn invalid_precursor(self) -> Option<P>
Returns the precursor on the invalid path, or None if valid.
Discards the reason; use invalid_parts to retain both.
Sourcepub fn invalid_reason(self) -> Option<V::InvalidReason>
pub fn invalid_reason(self) -> Option<V::InvalidReason>
Returns the invalid reason, or None if valid.
Discards the precursor; use invalid_parts to retain both.
Sourcepub fn invalid_parts(self) -> Option<(P, V::InvalidReason)>
pub fn invalid_parts(self) -> Option<(P, V::InvalidReason)>
Returns the precursor and reason on the invalid path, or
None if valid.
Sourcepub fn as_ref(&self) -> MaybeValidRef<'_, V, P>where
V::InvalidReason: Clone,
pub fn as_ref(&self) -> MaybeValidRef<'_, V, P>where
V::InvalidReason: Clone,
Returns a MaybeValidRef that borrows from this one, with the
same variant structure.
Useful for inspecting an owned outcome without consuming it.
The returned value borrows V as &V and the precursor as
&P, and clones the InvalidReason.
Sourcepub fn into_result(self) -> Result<V, (P, V::InvalidReason)>
pub fn into_result(self) -> Result<V, (P, V::InvalidReason)>
Converts into a Result, discarding the peer framing and
bundling the precursor into the error.
Useful when integrating with code written against Result and
?, at the cost of the explicit-match ergonomics
MaybeValidOwned encourages.
Sourcepub fn into_result_reason_only(self) -> Result<V, V::InvalidReason>
pub fn into_result_reason_only(self) -> Result<V, V::InvalidReason>
Converts into a Result that carries only the reason on the
error path, discarding the precursor.
Prefer into_result when the precursor is still useful to
the caller; this method is a convenience for call sites that
only need the diagnostic.