Skip to content

Commit

Permalink
docs(macros): Show more features for seq
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 18, 2023
1 parent cb26aea commit 3440c97
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions src/macros/seq.rs
Original file line number Diff line number Diff line change
@@ -1,41 +1,55 @@
/// Sequences multiple parsers and builds a struct out of them.
/// Initialize a struct or tuple out of a sequences of parsers
///
///# Example
///
/// ```
/// # use winnow::prelude::*;
/// # use winnow::ascii::{alphanumeric1, dec_uint, space0};
/// # use winnow::combinator::delimited;
/// # use winnow::combinator::success;
/// # use winnow::error::ContextError;
/// use winnow::combinator::seq;
///
/// #[derive(Debug, PartialEq)]
/// #[derive(Default, Debug, PartialEq)]
/// struct Field {
/// namespace: u32,
/// name: Vec<u8>,
/// value: Vec<u8>,
/// point: (u32, u32),
/// metadata: Vec<u8>,
/// }
///
/// let num = dec_uint::<_, u32, ContextError>;
/// let spaced = |b| delimited(space0, b, space0);
/// let mut parser = seq!{
/// Field {
/// // Parse into structs / tuple-structs
/// fn field(input: &mut &[u8]) -> PResult<Field> {
/// seq!{Field {
/// namespace: success(5),
/// name: alphanumeric1.map(|s: &[u8]| s.to_owned()),
/// // `_` fields are ignored when building the struct
/// _: spaced(b':'),
/// _: (space0, b':', space0),
/// value: alphanumeric1.map(|s: &[u8]| s.to_owned()),
/// _: spaced(b':'),
/// point: seq!(num, _: spaced(b','), num),
/// }
/// };
/// _: (space0, b':', space0),
/// point: point,
/// // default initialization also works
/// ..Default::default()
/// }}.parse_next(input)
/// }
///
/// // Or parse into tuples
/// fn point(input: &mut &[u8]) -> PResult<(u32, u32)> {
/// let num = dec_uint::<_, u32, ContextError>;
/// seq!(num, _: (space0, b',', space0), num).parse_next(input)
/// }
///
/// assert_eq!(
/// parser.parse_peek(&b"test: data: 123 , 4"[..]),
/// field.parse_peek(&b"test: data: 123 , 4"[..]),
/// Ok((
/// &b""[..],
/// Field {
/// namespace: 5,
/// name: b"test"[..].to_owned(),
/// value: b"data"[..].to_owned(),
/// point: (123, 4),
/// metadata: Default::default(),
/// },
/// )),
/// );
Expand Down

0 comments on commit 3440c97

Please sign in to comment.