use crate::expr::ExpressCompileIR::{Original, Tag};
use crate::expr::ExpressionIR;
use crate::expr::ExpressionIR::ItemValue;
use crate::utils::str::Block;
use crate::value::TmplValue;
#[derive(Debug, PartialEq)]
pub enum ExpressCompileIR<'a> {
Tag(ExpressionIR),
Original(&'a str),
}
impl ExpressCompileIR<'static> {
pub fn parse_static_str(src: &str) -> Vec<ExpressCompileIR> {
Block::new_group(src.trim(), "\"", "\"", &vec![("'", "'")])
.into_iter()
.flat_map(|e| match e {
Block::Static(d) => Block::new_group(d, "'", "'", &vec![("\"", "\"")])
.into_iter()
.map(|e| match e {
Block::Dynamic(dy) => Tag(ItemValue(TmplValue::Text(dy.to_string()))),
Block::Static(st) => Original(st),
})
.collect(),
Block::Dynamic(s) => vec![Tag(ItemValue(TmplValue::Text(s.to_string())))],
})
.collect::<Vec<ExpressCompileIR>>()
}
}
#[cfg(test)]
mod test {
use crate::expr::ExpressCompileIR;
use crate::expr::ExpressCompileIR::{Original, Tag};
use crate::expr::ExpressionIR::ItemValue;
use crate::value::TmplValue;
#[test]
fn test_parse_str() {
assert_eq!(
ExpressCompileIR::parse_static_str(r#"hello world 'dragon' "dragon""#),
vec![
Original("hello world "),
Tag(ItemValue(TmplValue::Text("dragon".to_string()))),
Original(" "),
Tag(ItemValue(TmplValue::Text("dragon".to_string()))),
]
)
}
}