@@ -3,6 +3,8 @@ use crate::{IntoSkipOrFatal, Parse};
3
3
use derive_more:: { Display , Error } ;
4
4
use split_first_char:: split_first_char;
5
5
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)).
6
8
#[ derive( Debug , Clone , Copy ) ]
7
9
pub struct EnclosedTemplateParser < EscapeParser , QueryParser > {
8
10
pub config : ParserConfig ,
@@ -13,11 +15,13 @@ pub struct EnclosedTemplateParser<EscapeParser, QueryParser> {
13
15
pub type Parser < EscapeParser , QueryParser > = EnclosedTemplateParser < EscapeParser , QueryParser > ;
14
16
15
17
impl < EscapeParser , QueryParser > Parser < EscapeParser , QueryParser > {
18
+ /// Replace [`Parser::config`].
16
19
pub fn with_config ( mut self , config : ParserConfig ) -> Self {
17
20
self . config = config;
18
21
self
19
22
}
20
23
24
+ /// Replace [`Parser::escape_parser`].
21
25
pub fn with_escape_parser < NewEscapeParser > (
22
26
self ,
23
27
escape_parser : NewEscapeParser ,
@@ -34,6 +38,7 @@ impl<EscapeParser, QueryParser> Parser<EscapeParser, QueryParser> {
34
38
}
35
39
}
36
40
41
+ /// Replace [`Parser::query_parser`].
37
42
pub fn with_query_parser < NewQueryParser > (
38
43
self ,
39
44
query_parser : NewQueryParser ,
@@ -52,6 +57,38 @@ impl<EscapeParser, QueryParser> Parser<EscapeParser, QueryParser> {
52
57
}
53
58
54
59
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
+ /// ```
55
92
pub fn curly_braces ( ) -> Self {
56
93
Parser {
57
94
config : ParserConfig :: curly_braces ( ) ,
@@ -61,6 +98,7 @@ impl Parser<(), ()> {
61
98
}
62
99
}
63
100
101
+ /// Error type of [`Parse`] on [`EnclosedTemplateParser`].
64
102
#[ derive( Debug , Display , Error , Clone , Copy ) ]
65
103
pub enum ParseError < ParseEscapeError , ParseQueryError > {
66
104
#[ display( "Unexpected token {_0:?}" ) ]
0 commit comments