OkMaybe

Struct OkMaybe 

Source
pub struct OkMaybe<T, E>(pub T, pub Option<E>);
Expand description

Represents generic data and maybe an error

  • Full data when err is empty OkMaybe(_, None).
  • Maybe partial data when err is present: OkMaybe(_, Some(_)).

§Why

When parsing, we want to accumulate as many errors as possible. That means that even partial data may be useful. For example, this code has the same attribute specified twice and also a syntax error that will prevent it from parsing additional attributes:

#[macro_name(unknown attribute here, ignore)]
#[macro_name(rename = "First", rename = "Again")]

In this situation unknown attribute here constitutes a syntax error that would prevent parsing ignore. However, the second line can be fully parsed but we want to error on the accidental duplicate rename attribute. By returning partial data along side of errors we can report

§Examples

If E implements IntoIterator and Extend (as syn::Error or does) then you can accumulate errors using OkMaybe::push_unwrap. Use this when partial data returned might hold additional errors that you wish to accumulate before returning.

For example:

use proc_micro::{OkMaybe, MaybeError};

let mut errors = MaybeError::new();

let _out: () =
    OkMaybe((), Some(syn::Error::new(span, "message".to_string())))
        .push_unwrap(&mut errors);

assert!(errors.has_err());
assert!(matches!(errors.maybe(), Some(_)));

For situations where partial data is not usable, convert into a result:

use proc_micro::OkMaybe;

let result: Result<(), syn::Error> =
    OkMaybe((), Some(syn::Error::new(span, "message".to_string())))
        .to_result();

assert!(result.is_err(), "Expected an error, got {:?}", result);

let result: Result<(), syn::Error> =
    OkMaybe((), None)
        .to_result();
assert!(result.is_ok(), "Expected Ok, got {:?}", result);

Tuple Fields§

§0: T§1: Option<E>

Implementations§

Source§

impl<T, E> OkMaybe<T, E>

Source

pub fn to_result(self) -> Result<T, E>

Source§

impl<T, E> OkMaybe<T, E>
where E: IntoIterator<Item = E>,

Source

pub fn push_unwrap(self, push_to: impl Extend<E>) -> T

If E implements IntoIterator and Extend (like syn::Error does) then the error can be pushed into an accumulator (such as a MaybeError in order to return the original value.

Trait Implementations§

Source§

impl<T: Debug, E: Debug> Debug for OkMaybe<T, E>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T, E> Freeze for OkMaybe<T, E>
where T: Freeze, E: Freeze,

§

impl<T, E> RefUnwindSafe for OkMaybe<T, E>

§

impl<T, E> Send for OkMaybe<T, E>
where T: Send, E: Send,

§

impl<T, E> Sync for OkMaybe<T, E>
where T: Sync, E: Sync,

§

impl<T, E> Unpin for OkMaybe<T, E>
where T: Unpin, E: Unpin,

§

impl<T, E> UnwindSafe for OkMaybe<T, E>
where T: UnwindSafe, E: UnwindSafe,

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