diff options
author | Jürgen Hahn <mail.jhahn@gmail.com> | 2023-01-02T20·00+0100 |
---|---|---|
committer | jrhahn <mail.jhahn@gmail.com> | 2023-01-02T20·55+0000 |
commit | 31973890a9ee60f50c1426ef7173bd4238234268 (patch) | |
tree | ef5cf4964fe08b2e0391769c0590738eb184c258 /tvix/derivation/src/string_escape.rs | |
parent | e6862413ca032acc94615bd969c8fec49a1a1dc5 (diff) |
refactor(tvix/derivation): refactor the derivation serialization r/5564
This refactors the code to serialize a derivation. The original code has beed moved to seperate crates for better code structure. Change-Id: I3b1a6b134428fcbc9930c330bced8ec3610cfb4c Reviewed-on: https://cl.tvl.fyi/c/depot/+/7733 Tested-by: BuildkiteCI Reviewed-by: flokli <flokli@flokli.de>
Diffstat (limited to 'tvix/derivation/src/string_escape.rs')
-rw-r--r-- | tvix/derivation/src/string_escape.rs | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/tvix/derivation/src/string_escape.rs b/tvix/derivation/src/string_escape.rs new file mode 100644 index 000000000000..0e1dbe516f73 --- /dev/null +++ b/tvix/derivation/src/string_escape.rs @@ -0,0 +1,17 @@ +const STRING_ESCAPER: [(char, &str); 5] = [ + ('\\', "\\\\"), + ('\n', "\\n"), + ('\r', "\\r"), + ('\t', "\\t"), + ('\"', "\\\""), +]; + +pub fn escape_string(s: &str) -> String { + let mut s_replaced = s.to_string(); + + for escape_sequence in STRING_ESCAPER { + s_replaced = s_replaced.replace(escape_sequence.0, escape_sequence.1); + } + + format!("\"{}\"", s_replaced) +} |