1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
/*!
Compile-time support for interpolated string templates using field-value expressions.
# Field-value templates
A field-value template is a string literal surrounded by field-value expressions:
```text
a, b: 42, "Some text {c} and {d: true}", e, f: "text"
───┬──── ───────────────┬───────────── ──────┬─────
before literal literal after literal
```
The template string literal consists of blocks of text with holes between braces, where
the value in a hole is a field-value expression:
```text
"Some text {c} and {d: true}"
─┬─ ────┬────
└────┬─────┘
hole
"Some text {c} and {d: true}"
─────┬──── ──┬──
└────┬────┘
text
```
The syntax is similar to Rust's `format_args!` macro, but leans entirely on standard field-value
expressions for specifying values to interpolate.
# Why not `format_args!`?
Rust's `format_args!` macro already defines a syntax for string interpolation, but isn't suitable
for all situations:
- It's core purpose is to build strings. `format_args!` is based on machinery that throws away
type-specific information eagerly. It also performs optimizations at compile time that inline
certain values into the builder.
- It doesn't have a programmatic API. You can only make assumptions about how a `format_args!`
invocation will behave by observing the syntactic tokens passed to it at compile-time. You don't get any
visibility into the format literal itself.
- Flags are compact for formatting, but don't scale. The `:?#<>` tokens used for customizing formatting
are compact, but opaque, and don't naturally allow for arbitrarily complex annotation like attributes do.
When any of those trade-offs in `format_args!` becomes a problem, field-value templates may be a solution.
*/
#[cfg(test)]
#[macro_use]
extern crate quote;
use std::fmt::Formatter;
use std::{
borrow::Cow,
fmt,
iter::Peekable,
ops::Range,
str::{self, CharIndices},
};
use proc_macro2::{token_stream, Literal, Span, TokenStream, TokenTree};
use quote::ToTokens;
use syn::{spanned::Spanned, FieldValue};
/**
A field-value template.
*/
pub struct Template {
before_template: Vec<FieldValue>,
literal: Vec<LiteralPart>,
after_template: Vec<FieldValue>,
}
/**
A visitor for the parts of a template string.
*/
pub trait LiteralVisitor {
/**
Visit a text part in a template literal.
*/
fn visit_text(&mut self, text: &str);
/**
Visit a hole part in a template literal.
*/
fn visit_hole(&mut self, hole: &FieldValue);
}
impl<'a, V: ?Sized> LiteralVisitor for &'a mut V
where
V: LiteralVisitor,
{
fn visit_text(&mut self, text: &str) {
(**self).visit_text(text)
}
fn visit_hole(&mut self, hole: &FieldValue) {
(**self).visit_hole(hole)
}
}
impl Template {
/**
Parse a template from a `TokenStream`.
The `TokenStream` is typically all the tokens given to a macro.
*/
pub fn parse2(input: TokenStream) -> Result<Self, Error> {
let mut scan = ScanTemplate::new(input);
// Take any arguments up to the string template
// These are control arguments for the log statement that aren't key-value pairs
let mut parsing_value = false;
let (before_template, template) = scan.take_until(|tt| {
// If we're parsing a value then skip over this token
// It won't be interpreted as the template because it belongs to an arg
if parsing_value {
parsing_value = false;
return false;
}
match tt {
// A literal is interpreted as the template
TokenTree::Literal(_) => true,
// A `:` token marks the start of a value in a field-value
// The following token is the value, which isn't considered the template
TokenTree::Punct(p) if p.as_char() == ':' => {
parsing_value = true;
false
}
// Any other token isn't the template
_ => false,
}
});
// If there's more tokens, they should be a comma followed by comma-separated field-values
let after_template = if scan.has_input() {
scan.expect_punct(',')?;
scan.rest.collect()
} else {
TokenStream::new()
};
// Parse the template literal into its text fragments and field-value holes
let template = LiteralPart::parse_lit2(ScanTemplate::take_literal(
template.ok_or_else(|| Error::missing_template(scan.span))?,
)?)?;
let before_template = ScanTemplate::new(before_template).collect_field_values()?;
let after_template = ScanTemplate::new(after_template).collect_field_values()?;
Ok(Template {
before_template,
literal: template,
after_template,
})
}
/**
Field-values that appear before the template string literal.
*/
pub fn before_literal_field_values<'a>(&'a self) -> impl Iterator<Item = &'a FieldValue> {
self.before_template.iter()
}
/**
Field-values that appear within the template string literal.
This is a simple alternative to [`Template::visit_literal`] that iterates over the field-value holes.
*/
pub fn literal_field_values<'a>(&'a self) -> impl Iterator<Item = &'a FieldValue> {
self.literal.iter().filter_map(|part| {
if let LiteralPart::Hole { expr, .. } = part {
Some(expr)
} else {
None
}
})
}
/**
Field-values that appear after the template string literal.
*/
pub fn after_literal_field_values<'a>(&'a self) -> impl Iterator<Item = &'a FieldValue> {
self.after_template.iter()
}
/**
Visit the parts of the string literal part of the template.
Each fragment of text and field-value hole will be visited in sequence.
Given a template string like:
```text
Some text and a {hole} and some {more}.
```
the visitor will be called with the following inputs:
1. `visit_text("Some text and a ")`
2. `visit_hole("hole")`
3. `visit_text(" and some ")`
4. `visit_hole("more")`
5. `visit_text(".")`
*/
pub fn visit_literal(&self, mut visitor: impl LiteralVisitor) {
for part in &self.literal {
match part {
LiteralPart::Text { text, .. } => visitor.visit_text(text),
LiteralPart::Hole { expr, .. } => visitor.visit_hole(&expr),
}
}
}
}
/**
A part of a parsed template string literal.
*/
enum LiteralPart {
/**
A fragment of text.
*/
Text {
/**
The literal text content.
*/
text: String,
/**
The range within the template string that covers this part.
*/
range: Range<usize>,
},
/**
A replacement expression.
*/
Hole {
/**
The expression within the hole.
*/
expr: FieldValue,
/**
The range within the template string that covers this part.
*/
range: Range<usize>,
},
}
impl fmt::Debug for LiteralPart {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
LiteralPart::Text { text, range } => f
.debug_struct("Text")
.field("text", text)
.field("range", range)
.finish(),
LiteralPart::Hole { expr, range } => f
.debug_struct("Hole")
.field("expr", &format_args!("`{}`", expr.to_token_stream()))
.field("range", range)
.finish(),
}
}
}
impl LiteralPart {
fn parse_lit2(lit: Literal) -> Result<Vec<Self>, Error> {
enum Expecting {
TextOrEOF,
Hole,
}
let input = lit.to_string();
let mut parts = Vec::new();
let mut expecting = Expecting::TextOrEOF;
if input.len() == 0 {
return Ok(parts);
}
let mut scan = ScanPart::new(lit, &input)?;
while scan.has_input() {
match expecting {
Expecting::TextOrEOF => {
if let Some((text, range)) = scan.take_until_eof_or_hole_start()? {
parts.push(LiteralPart::Text {
text: text.into_owned(),
range,
});
}
expecting = Expecting::Hole;
continue;
}
Expecting::Hole => {
let (expr, range) = scan.take_until_hole_end()?;
let expr_span = scan.lit.subspan(range.start..range.end);
let tokens = {
let tokens: TokenStream = str::parse(&*expr).map_err(|e| {
Error::lex_fv_expr(expr_span.unwrap_or(scan.lit.span()), &*expr, e)
})?;
// Attempt to shrink the span of the parsed expression to just the
// fragment of the literal it was parsed from
if let Some(span) = scan.lit.subspan(range.start..range.end) {
tokens
.into_iter()
.map(|mut tt| {
tt.set_span(span);
tt
})
.collect()
} else {
tokens
}
};
let expr = syn::parse2(tokens).map_err(|e| {
Error::parse_fv_expr(expr_span.unwrap_or(scan.lit.span()), &*expr, e)
})?;
parts.push(LiteralPart::Hole { expr, range });
expecting = Expecting::TextOrEOF;
continue;
}
}
}
Ok(parts)
}
}
struct ScanTemplate {
span: Span,
rest: Peekable<token_stream::IntoIter>,
}
impl ScanTemplate {
fn new(input: TokenStream) -> Self {
ScanTemplate {
span: input.span(),
rest: input.into_iter().peekable(),
}
}
fn has_input(&mut self) -> bool {
self.rest.peek().is_some()
}
fn take_until(
&mut self,
mut until_true: impl FnMut(&TokenTree) -> bool,
) -> (TokenStream, Option<TokenTree>) {
let mut taken = TokenStream::new();
while let Some(tt) = self.rest.next() {
if until_true(&tt) {
return (taken, Some(tt));
}
taken.extend(Some(tt));
}
(taken, None)
}
fn is_punct(input: &TokenTree, c: char) -> bool {
match input {
TokenTree::Punct(p) if p.as_char() == c => true,
_ => false,
}
}
fn expect_punct(&mut self, c: char) -> Result<TokenTree, Error> {
match self.rest.next() {
Some(tt) => {
if Self::is_punct(&tt, c) {
Ok(tt)
} else {
Err(Error::invalid_char(tt.span(), &[c]))
}
}
None => Err(Error::invalid_char_eof(self.span, &[c])),
}
}
fn take_literal(tt: TokenTree) -> Result<Literal, Error> {
match tt {
TokenTree::Literal(l) => Ok(l),
_ => Err(Error::invalid_literal(tt.span())),
}
}
fn collect_field_values(mut self) -> Result<Vec<FieldValue>, Error> {
let mut result = Vec::new();
while self.has_input() {
let (arg, _) = self.take_until(|tt| Self::is_punct(&tt, ','));
if !arg.is_empty() {
let expr_span = arg.span();
result.push(syn::parse2::<FieldValue>(arg).map_err(|e| {
Error::parse_fv_expr(expr_span, expr_span.source_text().as_deref(), e)
})?);
}
}
Ok(result)
}
}
struct ScanPart<'input> {
lit: Literal,
input: &'input str,
start: usize,
end: usize,
rest: Peekable<CharIndices<'input>>,
}
struct TakeUntil<'a, 'input> {
current: char,
current_idx: usize,
rest: &'a mut Peekable<CharIndices<'input>>,
lit: &'a Literal,
}
impl<'input> ScanPart<'input> {
fn new(lit: Literal, input: &'input str) -> Result<Self, Error> {
let mut iter = input.char_indices();
let start = iter.next();
let end = iter.next_back();
// This just checks that we're looking at a string
// It doesn't bother with ensuring that last quote is unescaped
// because the input to this is expected to be a proc-macro literal
if start.map(|(_, c)| c) != Some('"') || end.map(|(_, c)| c) != Some('"') {
return Err(Error::invalid_literal(lit.span()));
}
Ok(ScanPart {
lit,
input: &input,
start: 1,
end: input.len() - 1,
rest: iter.peekable(),
})
}
fn has_input(&mut self) -> bool {
self.rest.peek().is_some()
}
fn take_until(
&mut self,
mut until_true: impl FnMut(TakeUntil<'_, 'input>) -> Result<bool, Error>,
) -> Result<Option<(Cow<'input, str>, Range<usize>)>, Error> {
let mut scan = || {
while let Some((i, c)) = self.rest.next() {
if until_true(TakeUntil {
current: c,
current_idx: i,
rest: &mut self.rest,
lit: &self.lit,
})? {
let start = self.start;
let end = i;
self.start = end + 1;
let range = start..end;
return Ok((Cow::Borrowed(&self.input[range.clone()]), range));
}
}
let range = self.start..self.end;
Ok((Cow::Borrowed(&self.input[range.clone()]), range))
};
match scan()? {
(s, r) if s.len() > 0 => Ok(Some((s, r))),
_ => Ok(None),
}
}
fn take_until_eof_or_hole_start(
&mut self,
) -> Result<Option<(Cow<'input, str>, Range<usize>)>, Error> {
let mut escaped = false;
let scanned = self.take_until(|state| match state.current {
// A `{` that's followed by another `{` is escaped
// If it's followed by a different character then it's
// the start of an interpolated expression
'{' => {
let start = state.current_idx;
match state.rest.peek().map(|(_, peeked)| *peeked) {
Some('{') => {
escaped = true;
let _ = state.rest.next();
Ok(false)
}
Some(_) => Ok(true),
None => Err(Error::incomplete_hole(
state
.lit
.subspan(start..start + 1)
.unwrap_or(state.lit.span()),
)),
}
}
// A `}` that's followed by another `}` is escaped
// We should never see these in this parser unless they're escaped
// If we do it means an interpolated expression is missing its start
// or it's been improperly escaped
'}' => match state.rest.peek().map(|(_, peeked)| *peeked) {
Some('}') => {
escaped = true;
let _ = state.rest.next();
Ok(false)
}
Some(_) => Err(Error::unescaped_hole(
state
.lit
.subspan(state.current_idx..state.current_idx + 1)
.unwrap_or(state.lit.span()),
)),
None => Err(Error::unescaped_hole(
state
.lit
.subspan(state.current_idx..state.current_idx + 1)
.unwrap_or(state.lit.span()),
)),
},
_ => Ok(false),
})?;
match scanned {
Some((input, range)) if escaped => {
// If the input is escaped, then replace `{{` and `}}` chars
let input = (&*input).replace("{{", "{").replace("}}", "}");
Ok(Some((Cow::Owned(input), range)))
}
scanned => Ok(scanned),
}
}
fn take_until_hole_end(&mut self) -> Result<(Cow<'input, str>, Range<usize>), Error> {
let mut depth = 1;
let mut matched_hole_end = false;
let mut escaped = false;
let mut next_terminator_escaped = false;
let mut terminator = None;
// NOTE: The starting point is the first char _after_ the opening `{`
// so to get a correct span here we subtract 1 from it to cover that character
let start = self.start - 1;
let scanned = self.take_until(|state| {
match state.current {
// If the depth would return to its start then we've got a full expression
'}' if terminator.is_none() && depth == 1 => {
matched_hole_end = true;
Ok(true)
}
// A block end will reduce the depth
'}' if terminator.is_none() => {
depth -= 1;
Ok(false)
}
// A block start will increase the depth
'{' if terminator.is_none() => {
depth += 1;
Ok(false)
}
// A double quote may be the start or end of a string
// It may also be escaped
'"' if terminator.is_none() => {
terminator = Some('"');
Ok(false)
}
// A single quote may be the start or end of a character
// It may also be escaped
'\'' if terminator.is_none() => {
terminator = Some('\'');
Ok(false)
}
// A `\` means there's embedded escaped characters
// These may be escapes the user needs to represent a `"`
// or they may be intended to appear in the final string
'\\' if state
.rest
.peek()
.map(|(_, peeked)| *peeked == '\\')
.unwrap_or(false) =>
{
next_terminator_escaped = !next_terminator_escaped;
escaped = true;
Ok(false)
}
'\\' => {
escaped = true;
Ok(false)
}
// The sequence `//` or `/*` means the expression contains a comment
// These aren't supported so bail with an error
'/' if state
.rest
.peek()
.map(|(_, peeked)| *peeked == '/' || *peeked == '*')
.unwrap_or(false) =>
{
Err(Error::unsupported_comment(
state
.lit
.subspan(state.current_idx..state.current_idx + 1)
.unwrap_or(state.lit.span()),
))
}
// If the current character is a terminator and it's not escaped
// then break out of the current string or character
c if Some(c) == terminator && !next_terminator_escaped => {
terminator = None;
Ok(false)
}
// If the current character is anything else then discard escaping
// for the next character
_ => {
next_terminator_escaped = false;
Ok(false)
}
}
})?;
if !matched_hole_end {
Err(Error::incomplete_hole(
self.lit
.subspan(start..self.start)
.unwrap_or(self.lit.span()),
))?;
}
match scanned {
Some((input, range)) if escaped => {
// If the input is escaped then replace `\"` with `"`
let input = (&*input).replace("\\\"", "\"");
Ok((Cow::Owned(input), range))
}
Some((input, range)) => Ok((input, range)),
None => Err(Error::missing_expr(
self.lit
.subspan(start..self.start)
.unwrap_or(self.lit.span()),
))?,
}
}
}
/**
An error encountered while parsing a template.
*/
#[derive(Debug)]
pub struct Error {
reason: String,
source: Option<Box<dyn std::error::Error>>,
span: Span,
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source.as_deref()
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "parsing failed: {}", self.reason)
}
}
impl Error {
pub fn span(&self) -> Span {
self.span
}
fn missing_template(span: Span) -> Self {
Error {
reason: format!("missing string template"),
source: None,
span,
}
}
fn incomplete_hole(span: Span) -> Self {
Error {
reason: format!("unexpected end of input, expected `}}`"),
source: None,
span,
}
}
fn unescaped_hole(span: Span) -> Self {
Error {
reason: format!("`{{` and `}}` characters must be escaped as `{{{{` and `}}}}`"),
source: None,
span,
}
}
fn missing_expr(span: Span) -> Self {
Error {
reason: format!("empty replacements (`{{}}`) aren't supported, put the replacement inside like `{{some_value}}`"),
source: None,
span,
}
}
fn lex_fv_expr(span: Span, expr: &str, err: proc_macro2::LexError) -> Self {
Error {
reason: format!("failed to parse `{}` as a field-value expression", expr),
span,
source: Some(format!("{:?}", err).into()),
}
}
fn parse_fv_expr<'a>(span: Span, expr: impl Into<Option<&'a str>>, err: syn::Error) -> Self {
Error {
reason: if let Some(expr) = expr.into() {
format!("failed to parse `{}` as a field-value expression", expr)
} else {
format!("failed to parse field-value expression")
},
span,
source: Some(err.into()),
}
}
fn invalid_literal(span: Span) -> Self {
Error {
reason: format!("templates must be parsed from string literals"),
source: None,
span,
}
}
fn invalid_char(span: Span, expected: &[char]) -> Self {
Error {
reason: format!(
"invalid character, expected: {}",
Error::display_list(expected)
),
source: None,
span,
}
}
fn invalid_char_eof(span: Span, expected: &[char]) -> Self {
Error {
reason: format!(
"unexpected end-of-input, expected: {}",
Error::display_list(expected)
),
source: None,
span,
}
}
fn unsupported_comment(span: Span) -> Self {
Error {
reason: format!("comments within expressions are not supported"),
source: None,
span,
}
}
fn display_list<'a>(l: &'a [impl fmt::Display]) -> impl fmt::Display + 'a {
struct DisplayList<'a, T>(&'a [T]);
impl<'a, T: fmt::Display> fmt::Display for DisplayList<'a, T> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0.len() {
1 => write!(f, "`{}`", self.0[0]),
_ => {
let mut first = true;
for item in self.0 {
if !first {
write!(f, ", ")?;
}
first = false;
write!(f, "`{}`", item)?;
}
Ok(())
}
}
}
}
DisplayList(l)
}
}
#[cfg(test)]
mod tests {
use super::*;
use syn::Member;
#[test]
fn parse_ok() {
let cases = vec![
quote!("template"),
quote!(a: 42, "temaplte"),
quote!("template", a: 42),
quote!(a: 42, "template", b: 42),
];
for case in cases {
let _ = Template::parse2(case).unwrap();
}
}
#[test]
fn parse_err() {
let cases = vec![
(quote!(), "parsing failed: missing string template"),
(quote!(a: 42), "parsing failed: missing string template"),
(
quote!(42),
"parsing failed: templates must be parsed from string literals",
),
(
quote!(a: 42, true),
"parsing failed: missing string template",
),
(
quote!(fn x() {}, "template"),
"parsing failed: failed to parse field-value expression",
),
(
quote!("template", fn x() {}),
"parsing failed: failed to parse field-value expression",
),
];
for (input, expected) in cases {
let actual = match Template::parse2(input) {
Err(e) => e,
Ok(_) => panic!("parsing should've failed but produced a value",),
};
assert_eq!(expected, actual.to_string(),);
}
}
#[test]
fn template_parse_ok() {
let cases = vec![
("", vec![]),
("", vec![]),
("Hello world 🎈📌", vec![text("Hello world 🎈📌", 1..21)]),
(
"Hello {world} 🎈📌",
vec![
text("Hello ", 1..7),
hole("world", 8..13),
text(" 🎈📌", 14..23),
],
),
("{world}", vec![hole("world", 2..7)]),
(
"Hello {#[log::debug] world} 🎈📌",
vec![
text("Hello ", 1..7),
hole("#[log::debug] world", 8..27),
text(" 🎈📌", 28..37),
],
),
(
"Hello {#[log::debug] world: 42} 🎈📌",
vec![
text("Hello ", 1..7),
hole("#[log::debug] world: 42", 8..31),
text(" 🎈📌", 32..41),
],
),
(
"Hello {#[log::debug] world: \"is text\"} 🎈📌",
vec![
text("Hello ", 1..7),
hole("#[log::debug] world: \"is text\"", 8..40),
text(" 🎈📌", 41..50),
],
),
(
"{Hello} {world}",
vec![hole("Hello", 2..7), text(" ", 8..9), hole("world", 10..15)],
),
(
"{a}{b}{c}",
vec![hole("a", 2..3), hole("b", 5..6), hole("c", 8..9)],
),
(
"🎈📌{a}🎈📌{b}🎈📌{c}🎈📌",
vec![
text("🎈📌", 1..9),
hole("a", 10..11),
text("🎈📌", 12..20),
hole("b", 21..22),
text("🎈📌", 23..31),
hole("c", 32..33),
text("🎈📌", 34..42),
],
),
(
"Hello 🎈📌 {{world}}",
vec![text("Hello 🎈📌 {world}", 1..25)],
),
(
"🎈📌 Hello world {{}}",
vec![text("🎈📌 Hello world {}", 1..26)],
),
(
"Hello {#[log::debug] world: \"{\"} 🎈📌",
vec![
text("Hello ", 1..7),
hole("#[log::debug] world: \"{\"", 8..34),
text(" 🎈📌", 35..44),
],
),
(
"Hello {#[log::debug] world: '{'} 🎈📌",
vec![
text("Hello ", 1..7),
hole("#[log::debug] world: '{'", 8..32),
text(" 🎈📌", 33..42),
],
),
(
"Hello {#[log::debug] world: \"is text with 'embedded' stuff\"} 🎈📌",
vec![
text("Hello ", 1..7),
hole(
"#[log::debug] world: \"is text with 'embedded' stuff\"",
8..62,
),
text(" 🎈📌", 63..72),
],
),
("{{", vec![text("{", 1..3)]),
("}}", vec![text("}", 1..3)]),
];
for (template, expected) in cases {
let actual = match LiteralPart::parse_lit2(Literal::string(template)) {
Ok(template) => template,
Err(e) => panic!("failed to parse {:?}: {}", template, e),
};
assert_eq!(
format!("{:?}", expected),
format!("{:?}", actual),
"parsing template: {:?}",
template
);
}
}
#[test]
fn template_parse_err() {
let cases = vec![
("{", "parsing failed: unexpected end of input, expected `}`"),
("a {", "parsing failed: unexpected end of input, expected `}`"),
("a { a", "parsing failed: unexpected end of input, expected `}`"),
("{ a", "parsing failed: unexpected end of input, expected `}`"),
("}", "parsing failed: `{` and `}` characters must be escaped as `{{` and `}}`"),
("} a", "parsing failed: `{` and `}` characters must be escaped as `{{` and `}}`"),
("a } a", "parsing failed: `{` and `}` characters must be escaped as `{{` and `}}`"),
("a }", "parsing failed: `{` and `}` characters must be escaped as `{{` and `}}`"),
("{}", "parsing failed: empty replacements (`{}`) aren\'t supported, put the replacement inside like `{some_value}`"),
("{not real rust}", "parsing failed: failed to parse `not real rust` as a field-value expression"),
("{// a comment!}", "parsing failed: comments within expressions are not supported"),
("{/* a comment! */}", "parsing failed: comments within expressions are not supported"),
];
for (template, expected) in cases {
let actual = match LiteralPart::parse_lit2(Literal::string(template)) {
Err(e) => e,
Ok(actual) => panic!(
"parsing {:?} should've failed but produced {:?}",
template, actual
),
};
assert_eq!(
expected,
actual.to_string(),
"parsing template: {:?}",
template
);
}
}
fn text(text: &str, range: Range<usize>) -> LiteralPart {
LiteralPart::Text {
text: text.to_owned(),
range,
}
}
fn hole(expr: &str, range: Range<usize>) -> LiteralPart {
LiteralPart::Hole {
expr: syn::parse_str(expr)
.unwrap_or_else(|e| panic!("failed to parse {:?} ({})", expr, e)),
range,
}
}
#[test]
fn visit() {
fn to_rt_tokens(template: &Template, base: TokenStream) -> TokenStream {
struct DefaultVisitor {
base: TokenStream,
parts: Vec<TokenStream>,
}
impl LiteralVisitor for DefaultVisitor {
fn visit_text(&mut self, text: &str) {
let base = &self.base;
self.parts.push(quote!(#base::Part::Text(#text)));
}
fn visit_hole(&mut self, hole: &FieldValue) {
let hole = match hole.member {
Member::Named(ref member) => member.to_string(),
Member::Unnamed(ref member) => member.index.to_string(),
};
let base = &self.base;
self.parts.push(quote!(#base::Part::Hole(#hole)));
}
}
let mut visitor = DefaultVisitor {
base,
parts: Vec::new(),
};
template.visit_literal(&mut visitor);
let base = &visitor.base;
let parts = &visitor.parts;
quote!(
#base::Template(&[#(#parts),*])
)
}
let cases = vec![(
quote!("text and {label} and {more: 42}"),
quote!(crate::rt::Template(&[
crate::rt::Part::Text("text and "),
crate::rt::Part::Hole("label"),
crate::rt::Part::Text(" and "),
crate::rt::Part::Hole("more")
])),
)];
for (template, expected) in cases {
let template = Template::parse2(template).unwrap();
assert_eq!(
expected.to_string(),
to_rt_tokens(&template, quote!(crate::rt)).to_string()
);
}
}
}