summaryrefslogtreecommitdiffstats
path: root/crates/rebel-parse/src/ast.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/rebel-parse/src/ast.rs')
-rw-r--r--crates/rebel-parse/src/ast.rs18
1 files changed, 18 insertions, 0 deletions
diff --git a/crates/rebel-parse/src/ast.rs b/crates/rebel-parse/src/ast.rs
index f9943d4..648a79e 100644
--- a/crates/rebel-parse/src/ast.rs
+++ b/crates/rebel-parse/src/ast.rs
@@ -1,3 +1,5 @@
+use crate::token;
+
pub type Recipe<'a> = Vec<RecipeStmt<'a>>;
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -165,6 +167,22 @@ pub enum StringPiece<'a> {
Interp(Expr<'a>),
}
+impl<'a> TryFrom<&token::StringPiece<'a>> for StringPiece<'a> {
+ type Error = &'static str;
+
+ fn try_from(value: &token::StringPiece<'a>) -> Result<Self, Self::Error> {
+ use crate::recipe;
+
+ Ok(match value {
+ token::StringPiece::Chars(chars) => StringPiece::Chars(chars),
+ token::StringPiece::Escape(c) => StringPiece::Escape(*c),
+ token::StringPiece::Interp(tokens) => StringPiece::Interp(
+ recipe::expr(tokens).or(Err("Invalid expression in string interpolation"))?,
+ ),
+ })
+ }
+}
+
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct MapEntry<'a> {
pub key: &'a str,