about summary refs log tree commit diff
path: root/tvix/derivation/src/string_escape.rs
diff options
context:
space:
mode:
authorJürgen Hahn <mail.jhahn@gmail.com>2023-01-02T20·00+0100
committerjrhahn <mail.jhahn@gmail.com>2023-01-02T20·55+0000
commit31973890a9ee60f50c1426ef7173bd4238234268 (patch)
treeef5cf4964fe08b2e0391769c0590738eb184c258 /tvix/derivation/src/string_escape.rs
parente6862413ca032acc94615bd969c8fec49a1a1dc5 (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.rs17
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 0000000000..0e1dbe516f
--- /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)
+}