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 is specified, matches by name directly
§Variant Resolution
Variant is determined by $variant extension when present.
Without $variant, the parser falls back to untagged matching.
§Example
impl<'doc> FromEure<'doc> for Description {
fn parse(ctx: &ParseContext<'doc>) -> Result<Self, ParseError> {
ctx.parse_union()?
.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: UnionParseError,
impl<'doc, 'ctx, T, E> UnionParser<'doc, 'ctx, T, E>where
E: UnionParseError,
Sourcepub fn variant<P>(self, name: &str, f: P) -> UnionParser<'doc, 'ctx, T, E>where
P: DocumentParser<'doc, Output = T, Error = E>,
pub fn variant<P>(self, name: &str, f: P) -> UnionParser<'doc, 'ctx, T, E>where
P: DocumentParser<'doc, Output = T, Error = E>,
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>(
self,
name: &str,
then: impl FnMut(V) -> Result<T, E>,
) -> UnionParser<'doc, 'ctx, T, E>where
V: FromEure<'doc, Error = E>,
pub fn parse_variant<V>(
self,
name: &str,
then: impl FnMut(V) -> Result<T, E>,
) -> UnionParser<'doc, 'ctx, T, E>where
V: FromEure<'doc, Error = E>,
Register a variant with short-circuit semantics using FromEure.
Sourcepub fn variant_unambiguous<P>(
self,
name: &str,
f: P,
) -> UnionParser<'doc, 'ctx, T, E>where
P: DocumentParser<'doc, Output = T, Error = E>,
pub fn variant_unambiguous<P>(
self,
name: &str,
f: P,
) -> UnionParser<'doc, 'ctx, T, E>where
P: DocumentParser<'doc, Output = T, Error = E>,
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>(
self,
name: &str,
then: impl FnMut(V) -> Result<T, E>,
) -> UnionParser<'doc, 'ctx, T, E>where
V: FromEure<'doc, Error = E>,
pub fn parse_variant_unambiguous<V>(
self,
name: &str,
then: impl FnMut(V) -> Result<T, E>,
) -> UnionParser<'doc, 'ctx, T, E>where
V: FromEure<'doc, Error = E>,
Register a variant with unambiguous semantics using FromEure.