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/v0.7.7/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 binary data, strings, or
21//!    separate lexing and parsing phases
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, currently 1.64.0)
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(feature = "alloc")] {
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_auto_cfg))]
50#![cfg_attr(docsrs, feature(doc_cfg))]
51#![cfg_attr(docsrs, feature(extended_key_value_attributes))]
52#![cfg_attr(all(not(feature = "std"), not(test)), no_std)]
53#![warn(missing_docs)]
54#![warn(clippy::std_instead_of_core)]
55#![warn(clippy::std_instead_of_alloc)]
56#![warn(clippy::print_stderr)]
57#![warn(clippy::print_stdout)]
58
59#[cfg(feature = "alloc")]
60#[cfg_attr(test, macro_use)]
61#[allow(unused_extern_crates)]
62extern crate alloc;
63
64#[doc = include_str!("../README.md")]
65#[cfg(doctest)]
66pub struct ReadmeDoctests;
67
68/// Lib module to re-export everything needed from `std` or `core`/`alloc`. This is how `serde` does
69/// it, albeit there it is not public.
70#[doc(hidden)]
71pub(crate) mod lib {
72    #![allow(unused_imports)]
73
74    /// `std` facade allowing `std`/`core` to be interchangeable. Reexports `alloc` crate optionally,
75    /// as well as `core` or `std`
76    #[cfg(not(feature = "std"))]
77    /// internal std exports for no_std compatibility
78    pub(crate) mod std {
79        #[doc(hidden)]
80        #[cfg(not(feature = "alloc"))]
81        pub(crate) use core::borrow;
82
83        #[cfg(feature = "alloc")]
84        #[doc(hidden)]
85        pub(crate) use alloc::{borrow, boxed, collections, string, vec};
86
87        #[doc(hidden)]
88        pub(crate) use core::{
89            cmp, convert, fmt, hash, iter, mem, ops, option, result, slice, str,
90        };
91    }
92
93    #[cfg(feature = "std")]
94    /// internal std exports for `no_std` compatibility
95    pub(crate) mod std {
96        #![allow(clippy::std_instead_of_core)]
97        #![allow(clippy::std_instead_of_alloc)]
98        #[doc(hidden)]
99        pub(crate) use std::{
100            borrow, boxed, cmp, collections, convert, fmt, hash, iter, mem, ops, result, slice,
101            str, string, vec,
102        };
103    }
104}
105
106#[macro_use]
107mod macros;
108
109#[macro_use]
110pub mod error;
111
112mod parser;
113
114pub mod stream;
115
116pub mod ascii;
117pub mod binary;
118pub mod combinator;
119pub mod token;
120
121#[cfg(feature = "unstable-doc")]
122pub mod _topic;
123#[cfg(feature = "unstable-doc")]
124pub mod _tutorial;
125
126/// Core concepts available for glob import
127///
128/// Including
129/// - [`StreamIsPartial`][crate::stream::StreamIsPartial]
130/// - [`Parser`]
131///
132/// ## Example
133///
134/// ```rust
135/// use winnow::prelude::*;
136///
137/// fn parse_data(input: &mut &str) -> ModalResult<u64> {
138///     // ...
139/// #   winnow::ascii::dec_uint(input)
140/// }
141///
142/// fn main() {
143///   let result = parse_data.parse("100");
144///   assert_eq!(result, Ok(100));
145/// }
146/// ```
147pub mod prelude {
148    pub use crate::error::ModalError as _;
149    pub use crate::error::ParserError as _;
150    pub use crate::stream::AsChar as _;
151    pub use crate::stream::ContainsToken as _;
152    pub use crate::stream::Stream as _;
153    pub use crate::stream::StreamIsPartial as _;
154    pub use crate::ModalParser;
155    pub use crate::ModalResult;
156    pub use crate::Parser;
157    #[cfg(feature = "unstable-recover")]
158    #[cfg(feature = "std")]
159    pub use crate::RecoverableParser as _;
160
161    #[cfg(test)]
162    pub(crate) use crate::TestResult;
163}
164
165pub use error::ModalResult;
166pub use error::Result;
167pub use parser::*;
168pub use stream::BStr;
169pub use stream::Bytes;
170pub use stream::LocatingSlice;
171pub use stream::Partial;
172pub use stream::Stateful;
173pub use stream::Str;
174
175#[cfg(test)]
176pub(crate) use error::TestResult;