Changed message parsing to accept more line ending options.
This commit is contained in:
parent
b64766c9be
commit
321bbec7ad
1 changed files with 32 additions and 4 deletions
|
@ -200,11 +200,21 @@ impl FromStr for Message {
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
let line_ending_len = if state.ends_with("\r\n") {
|
||||||
|
"\r\n"
|
||||||
|
} else if state.ends_with("\r") {
|
||||||
|
"\r"
|
||||||
|
} else if state.ends_with("\n") {
|
||||||
|
"\n"
|
||||||
|
} else {
|
||||||
|
""
|
||||||
|
}.len();
|
||||||
let suffix = if state.contains(" :") {
|
let suffix = if state.contains(" :") {
|
||||||
let suffix = state.find(" :").map(|i| &state[i + 2..state.len() - 2]);
|
let suffix = state.find(" :").map(|i| &state[i + 2..state.len() - line_ending_len]);
|
||||||
state = state.find(" :").map_or("", |i| &state[..i + 1]);
|
state = state.find(" :").map_or("", |i| &state[..i + 1]);
|
||||||
suffix
|
suffix
|
||||||
} else {
|
} else {
|
||||||
|
state = &state[..state.len() - line_ending_len];
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
let command = match state.find(' ').map(|i| &state[..i]) {
|
let command = match state.find(' ').map(|i| &state[..i]) {
|
||||||
|
@ -214,9 +224,6 @@ impl FromStr for Message {
|
||||||
}
|
}
|
||||||
_ => return Err(ErrorKind::InvalidCommand.into()),
|
_ => return Err(ErrorKind::InvalidCommand.into()),
|
||||||
};
|
};
|
||||||
if suffix.is_none() {
|
|
||||||
state = &state[..state.len() - 2]
|
|
||||||
}
|
|
||||||
let args: Vec<_> = state.splitn(14, ' ').filter(|s| !s.is_empty()).collect();
|
let args: Vec<_> = state.splitn(14, ' ').filter(|s| !s.is_empty()).collect();
|
||||||
Message::with_tags(tags, prefix, command, args, suffix)
|
Message::with_tags(tags, prefix, command, args, suffix)
|
||||||
.map_err(|_| ErrorKind::InvalidCommand.into())
|
.map_err(|_| ErrorKind::InvalidCommand.into())
|
||||||
|
@ -378,6 +385,27 @@ mod test {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn from_string_atypical_endings() {
|
||||||
|
let message = Message {
|
||||||
|
tags: None,
|
||||||
|
prefix: None,
|
||||||
|
command: PRIVMSG(format!("test"), format!("Testing!")),
|
||||||
|
};
|
||||||
|
assert_eq!(
|
||||||
|
"PRIVMSG test :Testing!\r".parse::<Message>().unwrap(),
|
||||||
|
message
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
"PRIVMSG test :Testing!\n".parse::<Message>().unwrap(),
|
||||||
|
message
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
"PRIVMSG test :Testing!".parse::<Message>().unwrap(),
|
||||||
|
message
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn from_and_to_string() {
|
fn from_and_to_string() {
|
||||||
let message = "@aaa=bbb;ccc;example.com/ddd=eee :test!test@test PRIVMSG test :Testing with \
|
let message = "@aaa=bbb;ccc;example.com/ddd=eee :test!test@test PRIVMSG test :Testing with \
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue