quote_string

Function quote_string 

Source
pub fn quote_string(s: &str) -> String
Expand description

Quotes a string for use in control protocol commands.

Wraps the string in double quotes and escapes special characters:

  • "\"
  • \\\
  • newline → \n
  • carriage return → \r
  • tab → \t

This is the inverse of the unescaping performed by ControlLine::pop with escaped = true.

§Arguments

  • s - The string to quote

§Returns

A quoted and escaped string.

§Example

use stem_rs::protocol::quote_string;

// Simple string
assert_eq!(quote_string("hello"), "\"hello\"");

// String with special characters
assert_eq!(quote_string("hello\nworld"), "\"hello\\nworld\"");
assert_eq!(quote_string("say \"hi\""), "\"say \\\"hi\\\"\"");
assert_eq!(quote_string("path\\to\\file"), "\"path\\\\to\\\\file\"");

§Round-Trip Property

For any string s, quoting and then unquoting should return the original string:

use stem_rs::protocol::{quote_string, ControlLine};

let original = "hello\nworld";
let quoted = quote_string(original);
let mut line = ControlLine::new(&quoted);
let unquoted = line.pop(true, true).unwrap();
assert_eq!(original, unquoted);