Escape commas as a marshalling step

This commit is contained in:
Dane Johnson 2022-10-12 16:29:14 -05:00
parent c41bf83eb8
commit e661526e78

View File

@ -22,21 +22,6 @@ pub enum Error {
}
impl Message {
// pub fn parse(text: String) -> Result<Message> {
// let (command, mut tail) = text.split_once(":").ok_or(Error::BadParse)?;
// let command = String::new(command.trim());
// let args = vec![];
// loop {
// let tail = tail.trim_start();
// if tail == "" {
// break
// }
// else {
// let mut escaped = true;
// tail.find
// }
// };
// }
pub fn parse(text: String) -> Result<Message> {
use regex::Regex;
lazy_static! {
@ -124,7 +109,8 @@ impl std::fmt::Display for Message {
if self.args.is_empty() {
write!(f, "{}:", self.command)
} else {
write!(f, "{}: {}", self.command, self.args.as_slice().join(", "))
let args: Vec<_> = self.args.iter().map(|s| s.replace(",", "\\,")).collect();
write!(f, "{}: {}", self.command, args.join(", "))
}
}
}
@ -146,7 +132,12 @@ mod test {
}
#[test]
fn test_to_string() {
// Simple test
let msg = msg!(COMMAND, "arg1", "arg2");
assert_eq!(msg.to_string(), "COMMAND: arg1, arg2".to_string());
// Escaped commas in arguments
let msg = msg!(COMMAND, "arg1, comment", "arg2");
assert_eq!(msg.to_string(), "COMMAND: arg1\\, comment, arg2");
}
}