Parse accounts: Parse identifiers until the end of the line

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-11 15:36:24 +01:00
parent 1cb94ccfaf
commit 0747db35ee
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -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),
}