UnionParser

Struct UnionParser 

Source
pub struct UnionParser<'doc, 'ctx, T, E = ParseError> { /* 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 or repr is specified, matches by name directly

§Variant Resolution

Variant is determined by combining $variant extension and VariantRepr:

  • Both agree on same name → use repr’s context (with tag excluded for Internal)
  • $variant only (repr didn’t extract) → use original context
  • Repr only → use repr’s context
  • Conflict (different names) → ConflictingVariantTags error
  • Neither → Untagged parsing (try all variants)

§Example

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

Implementations§

Source§

impl<'doc, 'ctx, T, E> UnionParser<'doc, 'ctx, T, E>
where E: From<ParseError>,

Source

pub fn variant<P: DocumentParser<'doc, Output = T, Error = E>>( self, name: &str, f: P, ) -> Self

Register a variant with short-circuit semantics (default).

When this variant matches in untagged mode, parsing succeeds immediately without checking other variants. Use definition order to express priority.

Source

pub fn parse_variant<V: ParseDocument<'doc, Error = E>>( self, name: &str, then: impl FnMut(V) -> Result<T, E>, ) -> Self

Register a variant with short-circuit semantics using ParseDocument.

Source

pub fn variant_unambiguous<P: DocumentParser<'doc, Output = T, Error = E>>( self, name: &str, f: P, ) -> Self

Register a variant with unambiguous semantics.

All unambiguous variants are tried to detect conflicts. If multiple unambiguous variants match, an AmbiguousUnion error is returned. Use for catch-all variants or when you need conflict detection.

Source

pub fn parse_variant_unambiguous<V: ParseDocument<'doc, Error = E>>( self, name: &str, then: impl FnMut(V) -> Result<T, E>, ) -> Self

Register a variant with unambiguous semantics using ParseDocument.

Source

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

Execute the union parse with oneOf semantics.

Auto Trait Implementations§

§

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

§

impl<'doc, 'ctx, T, E = ParseError> !RefUnwindSafe for UnionParser<'doc, 'ctx, T, E>

§

impl<'doc, 'ctx, T, E = ParseError> !Send for UnionParser<'doc, 'ctx, T, E>

§

impl<'doc, 'ctx, T, E = ParseError> !Sync for UnionParser<'doc, 'ctx, T, E>

§

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

§

impl<'doc, 'ctx, T, E = ParseError> !UnwindSafe for UnionParser<'doc, 'ctx, T, E>

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.