diff options
author | Florian Klink <flokli@flokli.de> | 2023-07-30T07·05+0200 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2023-07-31T21·41+0000 |
commit | 34b7620764247bb967062a94c06a1750f8069701 (patch) | |
tree | 9aee989bed6e2a4086ba274cbe1d343e7f7903d1 /tvix/nix-compat/src/derivation/escape.rs | |
parent | 79531c3dab1c24ff3171c0aa067004c8e6c92e3f (diff) |
refactor(tvix/nix-compat/derivation): simplify r/6450
Let the escape function only take care of string escaping, not quoting. Let write_array_elements always quote and escape strings it consumes. Move the business of writing additional wrapping characters around it to the caller. Change-Id: Ib8dea69c409561b49862c531ba5a3fe6c2f061f8 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8993 Reviewed-by: raitobezarius <tvl@lahfa.xyz> Autosubmit: flokli <flokli@flokli.de> Tested-by: BuildkiteCI
Diffstat (limited to 'tvix/nix-compat/src/derivation/escape.rs')
-rw-r--r-- | tvix/nix-compat/src/derivation/escape.rs | 18 |
1 files changed, 7 insertions, 11 deletions
diff --git a/tvix/nix-compat/src/derivation/escape.rs b/tvix/nix-compat/src/derivation/escape.rs index 03106c44209e..8c2c6fce0753 100644 --- a/tvix/nix-compat/src/derivation/escape.rs +++ b/tvix/nix-compat/src/derivation/escape.rs @@ -1,7 +1,8 @@ use bstr::{BString, ByteSlice}; -pub fn escape_bstr(s: &[u8]) -> BString { - let mut s: Vec<u8> = s.to_owned(); +/// Escapes a byte sequence. Does not add surrounding quotes. +pub fn escape_bstr<P: AsRef<[u8]>>(s: P) -> BString { + let mut s: Vec<u8> = s.as_ref().to_vec(); s = s.replace(b"\\", b"\\\\"); s = s.replace(b"\n", b"\\n"); @@ -9,12 +10,7 @@ pub fn escape_bstr(s: &[u8]) -> BString { s = s.replace(b"\t", b"\\t"); s = s.replace(b"\"", b"\\\""); - let mut out: Vec<u8> = Vec::new(); - out.push(b'\"'); - out.append(&mut s); - out.push(b'\"'); - - out.into() + s.into() } #[cfg(test)] @@ -22,9 +18,9 @@ mod tests { use super::escape_bstr; use test_case::test_case; - #[test_case(b"", b"\"\""; "empty")] - #[test_case(b"\"", b"\"\\\"\""; "doublequote")] - #[test_case(b":", b"\":\""; "colon")] + #[test_case(b"", b""; "empty")] + #[test_case(b"\"", b"\\\""; "doublequote")] + #[test_case(b":", b":"; "colon")] fn escape(input: &[u8], expected: &[u8]) { assert_eq!(expected, escape_bstr(input)) } |