|
1 | 1 | use crate::{ |
2 | | - enclosed::{self, SimpleEnclosedTemplateSystem, SimpleEscapeParser, SimpleQueryParser}, |
3 | | - IntoTemplateSystem, |
| 2 | + enclosed::{ |
| 3 | + self, simple_escape, simple_query, SimpleEnclosedTemplateSystem, SimpleEscapeParser, |
| 4 | + SimpleQueryParser, |
| 5 | + }, |
| 6 | + iter::{EagerParseIter, LazyParseIter, ParsedTemplate}, |
| 7 | + EnclosedTemplateParser, IntoTemplateSystem, Template, TemplateApplicationError, |
4 | 8 | }; |
| 9 | +use core::{convert::Infallible, fmt}; |
5 | 10 |
|
6 | 11 | /// Create a simple template of string interpolation with curly braces. |
7 | 12 | /// |
@@ -44,9 +49,90 @@ use crate::{ |
44 | 49 | /// assert_eq!(actual, expected); |
45 | 50 | /// # } |
46 | 51 | /// ``` |
47 | | -pub fn simple_curly_braces<'a>() -> SimpleEnclosedTemplateSystem<'a> { |
| 52 | +pub fn simple_curly_braces<'a>() -> SimpleCurlyBraces<'a> { |
48 | 53 | enclosed::Parser::curly_braces() |
49 | 54 | .with_escape_parser(SimpleEscapeParser) |
50 | 55 | .with_query_parser(SimpleQueryParser) |
51 | 56 | .into_template_system() |
52 | 57 | } |
| 58 | + |
| 59 | +/// Return type of [`simple_curly_braces`]. |
| 60 | +pub type SimpleCurlyBraces<'a> = SimpleEnclosedTemplateSystem<'a>; |
| 61 | + |
| 62 | +/// Return type of [`SimpleCurlyBraces::lazy_parse`]. |
| 63 | +pub type LazilyParsed<'a, Query> = Template< |
| 64 | + LazyParseIter<'a, EnclosedTemplateParser<SimpleEscapeParser, SimpleQueryParser>>, |
| 65 | + Query, |
| 66 | +>; |
| 67 | + |
| 68 | +pub use LazilyParsed as LazilyParsedTemplate; |
| 69 | + |
| 70 | +#[cfg_attr( |
| 71 | + feature = "std", |
| 72 | + doc = r"Error type of [`LazilyParsedTemplate::to_string`] and [`LazilyParsedTemplate::write_to`]." |
| 73 | +)] |
| 74 | +#[cfg_attr( |
| 75 | + not(feature = "std"), |
| 76 | + doc = r"Error type of [`LazilyParsedTemplate::write_to`]." |
| 77 | +)] |
| 78 | +pub type LazilyParsedApplicationError<QueryError> = TemplateApplicationError< |
| 79 | + enclosed::ParseError<simple_escape::ParseError, simple_query::ParseError>, |
| 80 | + QueryError, |
| 81 | + fmt::Error, |
| 82 | +>; |
| 83 | + |
| 84 | +/// Value type of [`SimpleCurlyBraces::eager_parse`]. |
| 85 | +pub type EagerlyParsed<SegmentContainer, Query> = ParsedTemplate<SegmentContainer, Query>; |
| 86 | + |
| 87 | +/// Error type of [`SimpleCurlyBraces::eager_parse`]. |
| 88 | +pub type EagerParseError = |
| 89 | + enclosed::ParseError<simple_escape::ParseError, simple_query::ParseError>; |
| 90 | + |
| 91 | +/// Return type of [`EagerlyParsed::to_template`]. |
| 92 | +pub type EagerlyParsedTemplate<SegmentIter, Query> = Template<EagerParseIter<SegmentIter>, Query>; |
| 93 | + |
| 94 | +#[cfg_attr( |
| 95 | + feature = "std", |
| 96 | + doc = r"Error type of [`EagerlyParsedTemplate::to_string`] and [`EagerlyParsedTemplate::write_to`]." |
| 97 | +)] |
| 98 | +#[cfg_attr( |
| 99 | + not(feature = "std"), |
| 100 | + doc = r"Error type of [`EagerlyParsedTemplate::write_to`]." |
| 101 | +)] |
| 102 | +pub type EagerlyParsedApplicationError<QueryError> = |
| 103 | + TemplateApplicationError<Infallible, QueryError, fmt::Error>; |
| 104 | + |
| 105 | +#[cfg(feature = "std")] |
| 106 | +#[cfg(test)] |
| 107 | +mod std_tests { |
| 108 | + use super::{ |
| 109 | + simple_curly_braces, EagerParseError, EagerlyParsed, EagerlyParsedApplicationError, |
| 110 | + EagerlyParsedTemplate, LazilyParsed, LazilyParsedApplicationError, SimpleCurlyBraces, |
| 111 | + }; |
| 112 | + use derive_more::{Display, Error}; |
| 113 | + |
| 114 | + #[test] |
| 115 | + fn using_type_aliases() { |
| 116 | + fn _type_check() { |
| 117 | + #[derive(Debug, Display, Error, Clone, Copy)] |
| 118 | + enum QueryError {} |
| 119 | + |
| 120 | + let system: SimpleCurlyBraces<'static> = simple_curly_braces(); |
| 121 | + |
| 122 | + let lazy_parsed_template: LazilyParsed<'_, _> = system.lazy_parse(""); |
| 123 | + let lazy_result: Result<String, LazilyParsedApplicationError<QueryError>> = |
| 124 | + lazy_parsed_template.to_string(|_| Ok::<_, QueryError>("")); |
| 125 | + drop(lazy_result); |
| 126 | + |
| 127 | + let eager_parsed_template: Result< |
| 128 | + EagerlyParsed<Vec<crate::enclosed::Segment<&str>>, &str>, |
| 129 | + EagerParseError, |
| 130 | + > = system.eager_parse::<Vec<_>>(""); |
| 131 | + let eager_parsed_template: EagerlyParsedTemplate<_, &str> = |
| 132 | + eager_parsed_template.as_ref().unwrap().to_template(); |
| 133 | + let eager_result: Result<String, EagerlyParsedApplicationError<QueryError>> = |
| 134 | + eager_parsed_template.to_string(|_| Ok::<_, QueryError>("")); |
| 135 | + drop(eager_result); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments