From e661526e783762a08a15e693036239569d279a99 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Wed, 12 Oct 2022 16:29:14 -0500 Subject: [PATCH] Escape commas as a marshalling step --- src/message.rs | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/src/message.rs b/src/message.rs index a242fc6..99720f9 100644 --- a/src/message.rs +++ b/src/message.rs @@ -22,21 +22,6 @@ pub enum Error { } impl Message { - // pub fn parse(text: String) -> Result { - // 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 { 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"); } }