Skip to main content

winnow/
lib.rs

1//! > winnow, making parsing a breeze
2//!
3//! `winnow` is a parser combinator library
4//!
5//! Quick links:
6//! - [List of combinators][crate::combinator]
7//! - [Tutorial][_tutorial::chapter_0]
8//! - [Special Topics][_topic]
9//! - [Discussions](https://github.com/winnow-rs/winnow/discussions)
10//! - [CHANGELOG](https://github.com/winnow-rs/winnow/blob/v1.0.1/CHANGELOG.md) (includes major version migration
11//!   guides)
12//!
13//! ## Aspirations
14//!
15//! `winnow` aims to be your "do everything" parser, much like people treat regular expressions.
16//!
17//! In roughly priority order:
18//! 1. Support writing parser declaratively while not getting in the way of imperative-style
19//!    parsing when needed, working as an open-ended toolbox rather than a close-ended framework.
20//! 2. Flexible enough to be used for any application, including parsing strings, binary data,
21//!    or separate [lexing and parsing phases][_topic::lexing]
22//! 3. Zero-cost abstractions, making it easy to write high performance parsers
23//! 4. Easy to use, making it trivial for one-off uses
24//!
25//! In addition:
26//! - Resilient maintainership, including
27//!   - Willing to break compatibility rather than batching up breaking changes in large releases
28//!   - Leverage feature flags to keep one active branch
29//! - We will support the last 6 months of rust releases (MSRV)
30//!
31//! See also [Special Topic: Why winnow?][crate::_topic::why]
32//!
33//! ## Example
34//!
35//! Run
36//! ```console
37//! $ cargo add winnow
38//! ```
39//!
40//! Then use it to parse:
41//! ```rust
42//! # #[cfg(all(feature = "alloc", feature = "parser"))] {
43#![doc = include_str!("../examples/css/parser.rs")]
44//! # }
45//! ```
46//!
47//! See also the [Tutorial][_tutorial::chapter_0] and [Special Topics][_topic]
48
49#![cfg_attr(docsrs, feature(doc_cfg))]
50#![cfg_attr(docsrs, feature(extended_key_value_attributes))]
51#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
52#![warn(missing_docs)]
53#![warn(clippy::std_instead_of_core)]
54#![warn(clippy::std_instead_of_alloc)]
55#![warn(clippy::print_stderr)]
56#![warn(clippy::print_stdout)]
57
58#[cfg(feature = "alloc")]
59#[cfg_attr(test, macro_use)]
60#[allow(unused_extern_crates)]
61extern crate alloc;
62
63#[doc = include_str!("../README.md")]
64#[cfg(doctest)]
65pub struct ReadmeDoctests;
66
67pub(crate) mod util {
68    #[allow(dead_code)]
69    pub(crate) fn from_fn<F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result>(
70        f: F,
71    ) -> FromFn<F> {
72        FromFn(f)
73    }
74
75    pub(crate) struct FromFn<F>(F)
76    where
77        F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result;
78
79    impl<F> core::fmt::Debug for FromFn<F>
80    where
81        F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result,
82    {
83        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
84            (self.0)(f)
85        }
86    }
87
88    impl<F> core::fmt::Display for FromFn<F>
89    where
90        F: Fn(&mut core::fmt::Formatter<'_>) -> core::fmt::Result,
91    {
92        fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
93            (self.0)(f)
94        }
95    }
96}
97
98#[macro_use]
99mod macros;
100
101#[macro_use]
102#[cfg(feature = "parser")]
103pub mod error;
104
105#[cfg(feature = "parser")]
106mod parser;
107
108pub mod stream;
109
110#[cfg(feature = "ascii")]
111pub mod ascii;
112#[cfg(feature = "binary")]
113pub mod binary;
114#[cfg(feature = "parser")]
115pub mod combinator;
116#[cfg(feature = "parser")]
117pub mod token;
118
119#[cfg(feature = "unstable-doc")]
120pub mod _topic;
121#[cfg(feature = "unstable-doc")]
122pub mod _tutorial;
123
124/// Core concepts available for glob import
125///
126/// Including
127/// - [`StreamIsPartial`][crate::stream::StreamIsPartial]
128/// - [`Parser`]
129///
130/// ## Example
131///
132/// ```rust
133/// # #[cfg(feature = "ascii")] {
134/// use winnow::prelude::*;
135///
136/// fn parse_data(input: &mut &str) -> ModalResult<u64> {
137///     // ...
138/// #   winnow::ascii::dec_uint(input)
139/// }
140///
141/// fn main() {
142///   let result = parse_data.parse("100");
143///   assert_eq!(result, Ok(100));
144/// }
145/// # }
146/// ```
147pub mod prelude {
148    #[cfg(feature = "parser")]
149    pub use crate::error::ModalError as _;
150    #[cfg(feature = "parser")]
151    pub use crate::error::ParserError as _;
152    pub use crate::stream::AsChar as _;
153    pub use crate::stream::ContainsToken as _;
154    pub use crate::stream::Stream as _;
155    pub use crate::stream::StreamIsPartial as _;
156    #[cfg(feature = "parser")]
157    pub use crate::ModalParser;
158    #[cfg(feature = "parser")]
159    pub use crate::ModalResult;
160    #[cfg(feature = "parser")]
161    pub use crate::Parser;
162    #[cfg(feature = "unstable-recover")]
163    #[cfg(feature = "std")]
164    #[cfg(feature = "parser")]
165    pub use crate::RecoverableParser as _;
166
167    #[cfg(all(test, feature = "parser"))]
168    pub(crate) use crate::TestResult;
169}
170
171#[cfg(feature = "parser")]
172pub use error::ModalResult;
173#[cfg(feature = "parser")]
174pub use error::Result;
175#[cfg(feature = "unstable-recover")]
176#[cfg(feature = "std")]
177#[cfg(feature = "parser")]
178pub use parser::RecoverableParser;
179#[cfg(feature = "parser")]
180pub use parser::{ModalParser, Parser};
181pub use stream::BStr;
182pub use stream::Bytes;
183pub use stream::LocatingSlice;
184pub use stream::Partial;
185pub use stream::Stateful;
186pub use stream::Str;
187
188#[cfg(all(test, feature = "parser"))]
189pub(crate) use error::TestResult;