diff options
author | Florian Klink <flokli@flokli.de> | 2023-07-29T19·14+0200 |
---|---|---|
committer | clbot <clbot@tvl.fyi> | 2023-07-31T21·41+0000 |
commit | 79531c3dab1c24ff3171c0aa067004c8e6c92e3f (patch) | |
tree | 6e4198e648810bb835a8b0e0f68bf1af779829a8 /tvix/nix-compat/src/derivation/escape.rs | |
parent | 9521df708f92a237090b1b17ec969b319c4d00fe (diff) |
refactor(tvix/nix-compat): support non-unicode Derivations r/6449
Derivations can have non-unicode strings in their env values, so the ATerm representations are not necessarily String anymore, but Vec<u8>. Change-Id: Ic23839471eb7f68d9c3c30667c878830946b6607 Reviewed-on: https://cl.tvl.fyi/c/depot/+/8990 Tested-by: BuildkiteCI Reviewed-by: raitobezarius <tvl@lahfa.xyz> Autosubmit: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/nix-compat/src/derivation/escape.rs')
-rw-r--r-- | tvix/nix-compat/src/derivation/escape.rs | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/tvix/nix-compat/src/derivation/escape.rs b/tvix/nix-compat/src/derivation/escape.rs new file mode 100644 index 000000000000..03106c44209e --- /dev/null +++ b/tvix/nix-compat/src/derivation/escape.rs @@ -0,0 +1,31 @@ +use bstr::{BString, ByteSlice}; + +pub fn escape_bstr(s: &[u8]) -> BString { + let mut s: Vec<u8> = s.to_owned(); + + s = s.replace(b"\\", b"\\\\"); + s = s.replace(b"\n", b"\\n"); + s = s.replace(b"\r", b"\\r"); + 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() +} + +#[cfg(test)] +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")] + fn escape(input: &[u8], expected: &[u8]) { + assert_eq!(expected, escape_bstr(input)) + } +} |