snix/tvix/derivation/src/string_escape.rs
Jürgen Hahn 31973890a9 refactor(tvix/derivation): refactor the derivation serialization
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>
2023-01-02 20:55:14 +00:00

17 lines
394 B
Rust

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)
}