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
$variantextension 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>
impl<'doc, T> UnionParser<'doc, T>
Sourcepub fn variant<P>(self, name: &str, parser: P) -> Selfwhere
P: DocumentParser<'doc, Output = T> + 'doc,
pub fn variant<P>(self, name: &str, parser: P) -> Selfwhere
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.
Sourcepub fn other<P>(self, name: &str, parser: P) -> Selfwhere
P: DocumentParser<'doc, Output = T> + 'doc,
pub fn other<P>(self, name: &str, parser: P) -> Selfwhere
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.
Sourcepub fn parse(self) -> Result<T, ParseError>
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$variantspecifies an unregistered nameErr(NoMatchingVariant)if no variants matchErr(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> 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