Implement RawString in testing-framework

Keep original string type to avoid incorrect transformed script
generation.
This commit is contained in:
Ivan Boldyrev 2024-08-20 15:34:26 +02:00
parent 7b746e3288
commit c38a1b9343
3 changed files with 15 additions and 6 deletions

View File

@ -54,6 +54,7 @@ pub(crate) enum Sexp {
List(Vec<Sexp>), List(Vec<Sexp>),
Symbol(String), Symbol(String),
String(String), String(String),
RawString(String),
} }
impl Sexp { impl Sexp {
@ -69,6 +70,10 @@ impl Sexp {
Self::String(value.to_string()) Self::String(value.to_string())
} }
pub(crate) fn raw_string(value: impl ToString) -> Self {
Self::RawString(value.to_string())
}
pub(crate) fn canon(peer: Sexp, stream: Sexp, target: Sexp) -> Self { pub(crate) fn canon(peer: Sexp, stream: Sexp, target: Sexp) -> Self {
Self::Canon(Box::new(Canon { Self::Canon(Box::new(Canon {
peer, peer,
@ -103,6 +108,9 @@ impl Sexp {
Sexp::String(ref s) => Err(format!( Sexp::String(ref s) => Err(format!(
r#"cannot attach a service definition to a string: "{s:?}""# r#"cannot attach a service definition to a string: "{s:?}""#
)), )),
Sexp::RawString(ref s) => Err(format!(
r#"cannot attach a service definition to a raw string: "{s:?}""#
)),
} }
} }
} }
@ -138,9 +146,9 @@ impl std::fmt::Display for Sexp {
Sexp::Embed(embed) => { Sexp::Embed(embed) => {
write!( write!(
f, f,
"(embed [{args}] #"{script}"#{var})", r##"(embed [{args}] {script}{var})"##,
args = embed.args.iter().format(" "), args = embed.args.iter().format(" "),
script = embed.script, script = dbg!(&embed.script),
var = match &embed.var { var = match &embed.var {
Some(var) => format!(" {var}"), Some(var) => format!(" {var}"),
None => "".to_owned(), None => "".to_owned(),
@ -150,6 +158,7 @@ impl std::fmt::Display for Sexp {
Sexp::List(items) => write!(f, "({})", items.iter().format(" ")), Sexp::List(items) => write!(f, "({})", items.iter().format(" ")),
Sexp::Symbol(symbol) => write!(f, "{symbol}"), Sexp::Symbol(symbol) => write!(f, "{symbol}"),
Sexp::String(string) => write!(f, r#""{string}""#), Sexp::String(string) => write!(f, r#""{string}""#),
Sexp::RawString(string) => write!(f, r##"#"{string}"#"##),
} }
} }
} }

View File

@ -161,7 +161,7 @@ fn parse_raw_string(inp: Input<'_>) -> IResult<Input<'_>, Sexp, ParseError<'_>>
)), )),
), ),
), ),
Sexp::string, Sexp::raw_string,
)(inp) )(inp)
} }
@ -525,13 +525,13 @@ mod tests {
#[tokio::test] #[tokio::test]
async fn test_empty_raw_string() { async fn test_empty_raw_string() {
let res = Sexp::from_str(r##"#""#"##); let res = Sexp::from_str(r##"#""#"##);
assert_eq!(res, Ok(Sexp::string(""))); assert_eq!(res, Ok(Sexp::raw_string("")));
} }
#[tokio::test] #[tokio::test]
async fn test_raw_string() { async fn test_raw_string() {
let res = Sexp::from_str(r##"#"str " ing"#"##); let res = Sexp::from_str(r##"#"str " ing"#"##);
assert_eq!(res, Ok(Sexp::string("str \" ing"))); assert_eq!(res, Ok(Sexp::raw_string("str \" ing")));
} }
#[tokio::test] #[tokio::test]

View File

@ -98,7 +98,7 @@ impl<R: AirRunner> Transformer<'_, R> {
self.transform(child, test_init_parameters).await; self.transform(child, test_init_parameters).await;
} }
} }
Sexp::Embed(_) | Sexp::Symbol(_) | Sexp::String(_) => {} Sexp::Embed(_) | Sexp::Symbol(_) | Sexp::String(_) | Sexp::RawString(_) => {}
} }
} }