UnionParser

Struct UnionParser 

Source
pub struct UnionParser<'doc, T> { /* private fields */ }
Expand description

Helper for parsing union types from Eure documents.

Implements oneOf semantics:

  • Exactly one variant must match
  • Multiple matches resolved by registration order (priority)
  • Short-circuits on first priority variant match
  • When $variant extension is specified, matches by name directly

§Example

impl ParseDocument<'_> for Description {
    fn parse(doc: &EureDocument, node_id: NodeId) -> Result<Self, ParseError> {
        doc.parse_union(node_id)
            .variant("string", |doc, id| {
                let text: String = doc.parse(id)?;
                Ok(Description::String(text))
            })
            .variant("markdown", |doc, id| {
                let text: String = doc.parse(id)?;
                Ok(Description::Markdown(text))
            })
            .parse()
    }
}

Implementations§

Source§

impl<'doc, T> UnionParser<'doc, T>

Source

pub fn variant<P>(self, name: &str, parser: P) -> Self
where P: DocumentParser<'doc, Output = T> + 'doc,

Register a priority variant.

Priority variants are tried in registration order. When a priority variant matches, parsing short-circuits and returns immediately.

Source

pub fn other<P>(self, name: &str, parser: P) -> Self
where P: DocumentParser<'doc, Output = T> + 'doc,

Register a non-priority variant.

Non-priority variants are only tried if no priority variant matches. All non-priority variants are tried to detect ambiguity.

Source

pub fn parse(self) -> Result<T, ParseError>

Execute the union parse with oneOf semantics.

Returns:

  • Ok(T) if exactly one variant matches (or priority resolves ambiguity)
  • Err(UnknownVariant) if $variant specifies an unregistered name
  • Err(NoMatchingVariant) if no variants match
  • Err(AmbiguousUnion) if multiple non-priority variants match

Auto Trait Implementations§

§

impl<'doc, T> Freeze for UnionParser<'doc, T>
where T: Freeze,

§

impl<'doc, T> RefUnwindSafe for UnionParser<'doc, T>
where T: RefUnwindSafe,

§

impl<'doc, T> Send for UnionParser<'doc, T>
where T: Send,

§

impl<'doc, T> Sync for UnionParser<'doc, T>
where T: Sync,

§

impl<'doc, T> Unpin for UnionParser<'doc, T>
where T: Unpin,

§

impl<'doc, T> UnwindSafe for UnionParser<'doc, T>
where T: 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.