Simplify common functions on strings

This commit is contained in:
Alexis Mousset
2014-05-11 17:11:07 +02:00
parent d0685d13a7
commit 0e843103bf
3 changed files with 30 additions and 32 deletions

View File

@@ -66,7 +66,7 @@ impl FromStr for SmtpResponse<StrBuf> {
match (
from_str::<uint>(s.slice_to(3)),
vec!(" ", "-").contains(&s.slice(3,4)),
remove_trailing_crlf(StrBuf::from_str(s.slice_from(4)))
StrBuf::from_str(remove_trailing_crlf(s.slice_from(4).to_owned()))
) {
(Some(code), true, message) => Some(SmtpResponse{
code: code,
@@ -390,7 +390,7 @@ impl<S: Writer + Reader + Clone> SmtpClient<StrBuf, S> {
Ok(response) => {
self.server_info = Some(
SmtpServerInfo{
name: get_first_word(response.message.clone().unwrap()),
name: StrBuf::from_str(get_first_word(response.message.clone().unwrap().into_owned())),
esmtp_features: None
}
);
@@ -409,7 +409,7 @@ impl<S: Writer + Reader + Clone> SmtpClient<StrBuf, S> {
Ok(response) => {
self.server_info = Some(
SmtpServerInfo{
name: get_first_word(response.message.clone().unwrap()),
name: StrBuf::from_str(get_first_word(response.message.clone().unwrap().to_owned())),
esmtp_features: SmtpServerInfo::parse_esmtp_response(response.message.clone().unwrap())
}
);
@@ -424,7 +424,7 @@ impl<S: Writer + Reader + Clone> SmtpClient<StrBuf, S> {
pub fn mail(&mut self, from_address: StrBuf, options: Option<Vec<StrBuf>>) -> Result<SmtpResponse<StrBuf>, SmtpResponse<StrBuf>> {
check_state_in!(vec!(HeloSent));
match self.send_command(commands::Mail(unquote_email_address(from_address), options)).with_code(vec!(250)) {
match self.send_command(commands::Mail(StrBuf::from_str(unquote_email_address(from_address.to_owned())), options)).with_code(vec!(250)) {
Ok(response) => {
self.state = MailSent;
Ok(response)
@@ -439,7 +439,7 @@ impl<S: Writer + Reader + Clone> SmtpClient<StrBuf, S> {
pub fn rcpt(&mut self, to_address: StrBuf, options: Option<Vec<StrBuf>>) -> Result<SmtpResponse<StrBuf>, SmtpResponse<StrBuf>> {
check_state_in!(vec!(MailSent, RcptSent));
match self.send_command(commands::Recipient(unquote_email_address(to_address), options)).with_code(vec!(250)) {
match self.send_command(commands::Recipient(StrBuf::from_str(unquote_email_address(to_address.to_owned())), options)).with_code(vec!(250)) {
Ok(response) => {
self.state = RcptSent;
Ok(response)

View File

@@ -11,70 +11,68 @@
//!
//! Needs to be organized later
use std::strbuf::StrBuf;
pub static SP: &'static str = " ";
pub static CRLF: &'static str = "\r\n";
/// Adds quotes to emails if needed
pub fn quote_email_address(address: StrBuf) -> StrBuf {
pub fn quote_email_address(address: ~str) -> ~str {
match (address.as_slice().slice_to(1), address.as_slice().slice_from(address.as_slice().len()-1)) {
("<", ">") => address.into_strbuf(),
_ => StrBuf::from_str(format!("<{:s}>", address))
("<", ">") => address,
_ => format!("<{:s}>", address)
}
}
/// Removes quotes from emails if needed
pub fn unquote_email_address(address: StrBuf) -> StrBuf {
pub fn unquote_email_address(address: ~str) -> ~str {
match (address.as_slice().slice_to(1), address.as_slice().slice_from(address.as_slice().len() - 1)) {
("<", ">") => address.as_slice().slice(1, address.as_slice().len() - 1).into_strbuf(),
_ => address.into_strbuf()
("<", ">") => address.as_slice().slice(1, address.as_slice().len() - 1).to_owned(),
_ => address
}
}
/// Removes the trailing line return at the end of a string
pub fn remove_trailing_crlf(string: StrBuf) -> StrBuf {
pub fn remove_trailing_crlf(string: ~str) -> ~str {
if string.as_slice().slice_from(string.as_slice().len() - 2) == CRLF {
StrBuf::from_str(string.as_slice().slice_to(string.as_slice().len() - 2))
string.as_slice().slice_to(string.as_slice().len() - 2).to_owned()
} else if string.as_slice().slice_from(string.as_slice().len() - 1) == "\r" {
StrBuf::from_str(string.as_slice().slice_to(string.as_slice().len() - 1))
string.as_slice().slice_to(string.as_slice().len() - 1).to_owned()
} else {
StrBuf::from_str(string.as_slice())
string
}
}
/// Returns the first word of a string, or the string if it contains no space
pub fn get_first_word(string: StrBuf) -> StrBuf {
StrBuf::from_str(string.into_owned().split_str(CRLF).next().unwrap().splitn(' ', 1).next().unwrap())
pub fn get_first_word(string: ~str) -> ~str {
string.split_str(CRLF).next().unwrap().splitn(' ', 1).next().unwrap().to_owned()
}
#[cfg(test)]
mod test {
#[test]
fn test_quote_email_address() {
assert_eq!(super::quote_email_address(StrBuf::from_str("address")), StrBuf::from_str("<address>"));
assert_eq!(super::quote_email_address(StrBuf::from_str("<address>")), StrBuf::from_str("<address>"));
assert_eq!(super::quote_email_address("address".to_owned()), "<address>".to_owned());
assert_eq!(super::quote_email_address("<address>".to_owned()), "<address>".to_owned());
}
#[test]
fn test_unquote_email_address() {
assert_eq!(super::unquote_email_address(StrBuf::from_str("<address>")), StrBuf::from_str("address"));
assert_eq!(super::unquote_email_address(StrBuf::from_str("address")), StrBuf::from_str("address"));
assert_eq!(super::unquote_email_address(StrBuf::from_str("<address")), StrBuf::from_str("<address"));
assert_eq!(super::unquote_email_address("<address>".to_owned()), "address".to_owned());
assert_eq!(super::unquote_email_address("address".to_owned()), "address".to_owned());
assert_eq!(super::unquote_email_address("<address".to_owned()), "<address".to_owned());
}
#[test]
fn test_remove_trailing_crlf() {
assert_eq!(super::remove_trailing_crlf(StrBuf::from_str("word")), StrBuf::from_str("word"));
assert_eq!(super::remove_trailing_crlf(StrBuf::from_str("word\r\n")), StrBuf::from_str("word"));
assert_eq!(super::remove_trailing_crlf(StrBuf::from_str("word\r\n ")), StrBuf::from_str("word\r\n "));
assert_eq!(super::remove_trailing_crlf(StrBuf::from_str("word\r")), StrBuf::from_str("word"));
assert_eq!(super::remove_trailing_crlf("word".to_owned()), "word".to_owned());
assert_eq!(super::remove_trailing_crlf("word\r\n".to_owned()), "word".to_owned());
assert_eq!(super::remove_trailing_crlf("word\r\n ".to_owned()), "word\r\n ".to_owned());
assert_eq!(super::remove_trailing_crlf("word\r".to_owned()), "word".to_owned());
}
#[test]
fn test_get_first_word() {
assert_eq!(super::get_first_word(StrBuf::from_str("first word")), StrBuf::from_str("first"));
assert_eq!(super::get_first_word(StrBuf::from_str("first word\r\ntest")), StrBuf::from_str("first"));
assert_eq!(super::get_first_word(StrBuf::from_str("first")), StrBuf::from_str("first"));
assert_eq!(super::get_first_word("first word".to_owned()), "first".to_owned());
assert_eq!(super::get_first_word("first word\r\ntest".to_owned()), "first".to_owned());
assert_eq!(super::get_first_word("first".to_owned()), "first".to_owned());
}
}

View File

@@ -60,7 +60,7 @@
#![deny(unused_result)]
#![deny(deprecated_owned_vector)]
#[feature(phase)] #[phase(syntax, link)] extern crate log;
#![feature(phase)] #[phase(syntax, link)] extern crate log;
pub mod commands;
pub mod common;