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
$variantextension 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)
$variantonly (repr didn’t extract) → use original context- Repr only → use repr’s context
- Conflict (different names) →
ConflictingVariantTagserror - 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>,
impl<'doc, 'ctx, T, E> UnionParser<'doc, 'ctx, T, E>where
E: From<ParseError>,
Sourcepub fn variant<P: DocumentParser<'doc, Output = T, Error = E>>(
self,
name: &str,
f: P,
) -> Self
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.
Sourcepub fn parse_variant<V: ParseDocument<'doc, Error = E>>(
self,
name: &str,
then: impl FnMut(V) -> Result<T, E>,
) -> Self
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.
Sourcepub fn variant_unambiguous<P: DocumentParser<'doc, Output = T, Error = E>>(
self,
name: &str,
f: P,
) -> Self
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.
Sourcepub fn parse_variant_unambiguous<V: ParseDocument<'doc, Error = E>>(
self,
name: &str,
then: impl FnMut(V) -> Result<T, E>,
) -> Self
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.
Auto Trait Implementations§
impl<'doc, 'ctx, T, E> Freeze for UnionParser<'doc, 'ctx, T, E>
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>
impl<'doc, 'ctx, T, E = ParseError> !UnwindSafe for UnionParser<'doc, 'ctx, T, E>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more