mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-11-30 20:14:01 +01:00
Print template strings in plain quotes unless they can't round-trip (#708)
This commit is contained in:
1 parent
6e7f23a778
commit
23d369d84e
6 files changed
+191
-19
No files matched your search
@@ -45,14 +45,36 @@ pub enum Val {
|
||||
Null,
|
||||
}
|
||||
|
||||
/// Strings are printed as plain single-quoted values whenever they can survive a round-trip
|
||||
/// through both the parser and the client's editor. The editor can't handle `]}` inside a tag
|
||||
/// (the Lezer grammar and the JSON linter both close a tag at the first one, regardless of
|
||||
/// quoting), and newlines or other control characters don't belong in a single-line tag, so
|
||||
/// those cases fall back to base64.
|
||||
fn requires_b64(text: &str) -> bool {
|
||||
text.contains("]}") || text.chars().any(char::is_control)
|
||||
}
|
||||
|
||||
/// Escape the only two characters the parser treats specially inside a single-quoted string.
|
||||
fn escape_str(text: &str) -> String {
|
||||
let mut out = String::with_capacity(text.len());
|
||||
for c in text.chars() {
|
||||
match c {
|
||||
'\\' => out.push_str("\\\\"),
|
||||
'\'' => out.push_str("\\'"),
|
||||
_ => out.push(c),
|
||||
}
|
||||
}
|
||||
out
|
||||
}
|
||||
|
||||
impl Display for Val {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
let str = match self {
|
||||
Val::Str { text } => {
|
||||
if text.chars().all(|c| c.is_alphanumeric() || c == ' ' || c == '_' || c == '_') {
|
||||
format!("'{}'", text)
|
||||
} else {
|
||||
if requires_b64(text) {
|
||||
format!("b64'{}'", BASE64_URL_SAFE_NO_PAD.encode(text))
|
||||
} else {
|
||||
format!("'{}'", escape_str(text))
|
||||
}
|
||||
}
|
||||
Val::Var { name } => name.to_string(),
|
||||
@@ -835,10 +857,64 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn token_display_complex_str() -> Result<()> {
|
||||
assert_eq!(
|
||||
Val::Str { text: "Hello 'You'".to_string() }.to_string(),
|
||||
"b64'SGVsbG8gJ1lvdSc'"
|
||||
);
|
||||
assert_eq!(Val::Str { text: "Hello 'You'".to_string() }.to_string(), r#"'Hello \'You\''"#);
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_display_str_plain() -> Result<()> {
|
||||
// Strings the parser can read back verbatim print in plain quotes
|
||||
assert_eq!(Val::Str { text: "X-Api-Key".to_string() }.to_string(), "'X-Api-Key'");
|
||||
assert_eq!(Val::Str { text: "$.token".to_string() }.to_string(), "'$.token'");
|
||||
assert_eq!(Val::Str { text: "it's".to_string() }.to_string(), r#"'it\'s'"#);
|
||||
assert_eq!(Val::Str { text: r"back\slash".to_string() }.to_string(), r"'back\\slash'");
|
||||
assert_eq!(Val::Str { text: r#"{"a":1}"#.to_string() }.to_string(), r#"'{"a":1}'"#);
|
||||
assert_eq!(Val::Str { text: "héllo".to_string() }.to_string(), "'héllo'");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_display_str_b64_fallback() -> Result<()> {
|
||||
// `]}` would close the tag early in the client editor
|
||||
assert_eq!(Val::Str { text: "a]}b".to_string() }.to_string(), "b64'YV19Yg'");
|
||||
|
||||
// Control characters don't belong in a single-line tag
|
||||
assert_eq!(Val::Str { text: "line\nbreak".to_string() }.to_string(), "b64'bGluZQpicmVhaw'");
|
||||
assert_eq!(Val::Str { text: "line\rbreak".to_string() }.to_string(), "b64'bGluZQ1icmVhaw'");
|
||||
assert_eq!(Val::Str { text: "tab\there".to_string() }.to_string(), "b64'dGFiCWhlcmU'");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn token_str_round_trip() -> Result<()> {
|
||||
let cases = [
|
||||
"X-Api-Key",
|
||||
"$.token",
|
||||
"hello world",
|
||||
"it's",
|
||||
r"back\slash",
|
||||
r#"{"a":1}"#,
|
||||
"a]}b",
|
||||
"line\nbreak",
|
||||
"héllo",
|
||||
r"trailing\",
|
||||
"'",
|
||||
"",
|
||||
];
|
||||
|
||||
for text in cases {
|
||||
let val = Val::Str { text: text.to_string() };
|
||||
let template = Token::Tag { val: val.clone() }.to_string();
|
||||
let tokens = Parser::new(&template).parse()?;
|
||||
assert_eq!(
|
||||
tokens.tokens,
|
||||
vec![Token::Tag { val: val.clone() }, Token::Eof],
|
||||
"failed to round-trip {text:?} (printed as {template})"
|
||||
);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
@@ -879,7 +955,7 @@ mod tests {
|
||||
}
|
||||
}
|
||||
.to_string(),
|
||||
r#"${[ foo(arg=b64'diAneCc', arg2=my_var) ]}"#
|
||||
r#"${[ foo(arg='v \'x\'', arg2=my_var) ]}"#
|
||||
);
|
||||
|
||||
Ok(())
|
||||
|
||||
Reference in new issue
Block a user