Parse accounts: Parse identifiers until the end of the line
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
1cb94ccfaf
commit
0747db35ee
1 changed files with 20 additions and 7 deletions
27
src/lib.rs
27
src/lib.rs
|
@ -62,7 +62,7 @@ fn parse_account(i: &str) -> IResult<&str, (AccountName, Account)> {
|
|||
allspaces,
|
||||
tag("[Client]"),
|
||||
space0,
|
||||
take_while1(is_not_space),
|
||||
take_until("\n"),
|
||||
space0,
|
||||
))(i)?;
|
||||
let (i, (_, _, _, _, jid, _)) = tuple((
|
||||
|
@ -70,7 +70,7 @@ fn parse_account(i: &str) -> IResult<&str, (AccountName, Account)> {
|
|||
many1(tag("\t")),
|
||||
tag("jid:"),
|
||||
space0,
|
||||
take_while1(is_not_space),
|
||||
take_until("\n"),
|
||||
space0,
|
||||
))(i)?;
|
||||
let (i, (_, _, _, _, password, _)) = tuple((
|
||||
|
@ -78,12 +78,13 @@ fn parse_account(i: &str) -> IResult<&str, (AccountName, Account)> {
|
|||
many1(tag("\t")),
|
||||
tag("password:"),
|
||||
space0,
|
||||
take_while1(is_not_space),
|
||||
take_until("\n"),
|
||||
multispace0,
|
||||
))(i)?;
|
||||
let name = name.trim();
|
||||
let account = Account {
|
||||
jid: BareJid::from_str(jid).unwrap(),
|
||||
password: String::from(password),
|
||||
jid: BareJid::from_str(jid.trim()).unwrap(),
|
||||
password: String::from(password.trim()),
|
||||
};
|
||||
Ok((i, (String::from(name), account)))
|
||||
}
|
||||
|
@ -209,7 +210,8 @@ mod tests {
|
|||
#[test]
|
||||
fn test_account() {
|
||||
let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: password\n";
|
||||
let buf2 = "[Client] louise\njid: louise@localhost\npassword: password\n";
|
||||
let buf2 = "[Client] louise's phone \n\tjid: louise2@localhost\n\tpassword: password\n";
|
||||
let buf3 = "[Client] louise\njid: louise@localhost\npassword: password\n";
|
||||
|
||||
let name = String::from("louise");
|
||||
let account = Account {
|
||||
|
@ -220,7 +222,18 @@ mod tests {
|
|||
parse_account(buf1),
|
||||
Ok(("", (name.clone(), account.clone())))
|
||||
);
|
||||
match parse_account(buf2) {
|
||||
|
||||
let name = String::from("louise's phone");
|
||||
let account = Account {
|
||||
jid: BareJid::from_str("louise2@localhost").unwrap(),
|
||||
password: String::from("password"),
|
||||
};
|
||||
assert_eq!(
|
||||
parse_account(buf2),
|
||||
Ok(("", (name.clone(), account.clone())))
|
||||
);
|
||||
|
||||
match parse_account(buf3) {
|
||||
Err(_) => (),
|
||||
err => panic!("Unexpected result: {:?}", err),
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue