Skip to content

Commit f0170a6

Browse files
committed
docs: add more
1 parent 9f1e41e commit f0170a6

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

src/enclosed/parser.rs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ use crate::{IntoSkipOrFatal, Parse};
33
use derive_more::{Display, Error};
44
use split_first_char::split_first_char;
55

6+
/// Parse a template string whose queries are placed between an opening bracket character and a closing bracket character,
7+
/// (such as [curly braces](crate::simple_curly_braces)).
68
#[derive(Debug, Clone, Copy)]
79
pub struct EnclosedTemplateParser<EscapeParser, QueryParser> {
810
pub config: ParserConfig,
@@ -13,11 +15,13 @@ pub struct EnclosedTemplateParser<EscapeParser, QueryParser> {
1315
pub type Parser<EscapeParser, QueryParser> = EnclosedTemplateParser<EscapeParser, QueryParser>;
1416

1517
impl<EscapeParser, QueryParser> Parser<EscapeParser, QueryParser> {
18+
/// Replace [`Parser::config`].
1619
pub fn with_config(mut self, config: ParserConfig) -> Self {
1720
self.config = config;
1821
self
1922
}
2023

24+
/// Replace [`Parser::escape_parser`].
2125
pub fn with_escape_parser<NewEscapeParser>(
2226
self,
2327
escape_parser: NewEscapeParser,
@@ -34,6 +38,7 @@ impl<EscapeParser, QueryParser> Parser<EscapeParser, QueryParser> {
3438
}
3539
}
3640

41+
/// Replace [`Parser::query_parser`].
3742
pub fn with_query_parser<NewQueryParser>(
3843
self,
3944
query_parser: NewQueryParser,
@@ -52,6 +57,38 @@ impl<EscapeParser, QueryParser> Parser<EscapeParser, QueryParser> {
5257
}
5358

5459
impl Parser<(), ()> {
60+
/// Create a builder of an [`EnclosedTemplateParser`] of templates whose queries should be placed between curly braces.
61+
///
62+
/// The curly braces can be replaced by [replacing the config][Parser::with_config].
63+
///
64+
/// The value returned from this function is not useful immediately. The [query parser][Parser::with_query_parser] and the
65+
/// [escape parser][Parser::with_escape_parser] must be replaced first.
66+
///
67+
/// **Usage example:**
68+
///
69+
/// ```
70+
/// # #[cfg(not(feature = "std"))] fn main() {}
71+
/// # #[cfg(feature = "std")] fn main() {
72+
/// # use pretty_assertions::assert_eq;
73+
/// use lazy_template::{
74+
/// enclosed::{Parser, SimpleEscapeParser, SimpleQuery, SimpleQueryParser},
75+
/// IntoTemplateSystem,
76+
/// };
77+
/// let output = Parser::curly_braces()
78+
/// .with_escape_parser(SimpleEscapeParser)
79+
/// .with_query_parser(SimpleQueryParser)
80+
/// .into_template_system::<SimpleQuery>()
81+
/// .lazy_parse("{name} is a {age} years old {gender}")
82+
/// .to_string(|query| match query {
83+
/// "name" => Ok("Alice"),
84+
/// "age" => Ok("20"),
85+
/// "gender" => Ok("girl"),
86+
/// _ => Err(format!("Can't answer {query:?}")),
87+
/// })
88+
/// .unwrap();
89+
/// assert_eq!(output, "Alice is a 20 years old girl");
90+
/// # }
91+
/// ```
5592
pub fn curly_braces() -> Self {
5693
Parser {
5794
config: ParserConfig::curly_braces(),
@@ -61,6 +98,7 @@ impl Parser<(), ()> {
6198
}
6299
}
63100

101+
/// Error type of [`Parse`] on [`EnclosedTemplateParser`].
64102
#[derive(Debug, Display, Error, Clone, Copy)]
65103
pub enum ParseError<ParseEscapeError, ParseQueryError> {
66104
#[display("Unexpected token {_0:?}")]

0 commit comments

Comments
 (0)