Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Merge pull request #10 from pleshevskiy/bug-9
Browse files Browse the repository at this point in the history
feat: remove multiline text before push
  • Loading branch information
pleshevskiy authored Dec 21, 2021
2 parents c9c8da4 + 5082159 commit 3b5d40f
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/commands/push.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ impl StreamCommand for PushCommand<'_> {
fn message(&self) -> String {
let mut message = format!(
r#"PUSH {} {} {} "{}""#,
self.collection, self.bucket, self.object, self.text
self.collection,
self.bucket,
self.object,
remove_multiline(self.text)
);
if let Some(locale) = self.locale.as_ref() {
message.push_str(&format!(" LANG({})", locale));
Expand All @@ -28,8 +31,41 @@ impl StreamCommand for PushCommand<'_> {
fn receive(&self, message: String) -> Result<Self::Response> {
if message == "OK\r\n" {
Ok(true)
} else if message.starts_with("ERR ") {
Err(Error::new(ErrorKind::QueryResponseError(Box::leak(
message.into_boxed_str(),
))))
} else {
Err(Error::new(ErrorKind::WrongSonicResponse))
}
}
}

fn remove_multiline(text: &str) -> String {
text.split('\n')
.enumerate()
.fold(String::new(), |mut acc, (i, line)| {
if i != 0 && !line.is_empty() && !acc.is_empty() && !acc.ends_with(' ') {
acc.push(' ');
}

acc.push_str(line);
acc
})
}

#[cfg(test)]
mod tests {
use super::remove_multiline;

#[test]
fn should_make_single_line() {
let text = "
Hello
World
";

let expected_text = "Hello World";
assert_eq!(remove_multiline(text), expected_text);
}
}

0 comments on commit 3b5d40f

Please sign in to comment.