Add resource attr to Account

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-11 18:03:21 +01:00
parent c522257e8c
commit 80bd6c281f
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -12,7 +12,7 @@ use minidom::Element;
use nom::{
self,
branch::alt,
bytes::complete::{tag, take_until, take_until1, take_while},
bytes::complete::{tag, take_until, take_until1, take_while, take_while1},
character::complete::{multispace0, space0},
combinator::recognize,
error::{ErrorKind, ParseError},
@ -29,6 +29,7 @@ pub type AccountName = String;
pub struct Account {
pub jid: BareJid,
pub password: String,
pub resource: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
@ -83,26 +84,47 @@ where
fn parse_account(i: &str) -> IResult<&str, (AccountName, Account)> {
let (i, (_, _, _, name, _)) =
tuple((allspaces, tag("[Client]"), space0, take_until("\n"), space0))(i)?;
let (i, (_, _, _, _, jid, _)) = tuple((
let (i, lines) = many1(tuple((
tag("\n"),
many1(tag("\t")),
tag("jid:"),
space0,
take_while1(|c| c == '\t'),
take_until("\n"),
space0,
))(i)?;
let (i, (_, _, _, _, password, _)) = tuple((
tag("\n"),
many1(tag("\t")),
tag("password:"),
space0,
take_until("\n"),
multispace0,
))(i)?;
)))(i)?;
let name = name.trim();
let mut jid: Option<BareJid> = None;
let mut password: Option<String> = None;
let mut resource: Option<String> = None;
for line in lines {
let (_, _, attr) = line;
if let Some((key, val)) = attr.split_once(':') {
let key = key.trim();
let val = val.trim();
if key == "jid" {
jid = Some(BareJid::from_str(val).unwrap());
} else if key == "password" {
password = Some(String::from(val));
} else if key == "resource" {
resource = Some(String::from(val));
}
}
}
if jid.is_none() || password.is_none() {
return Err(nom::Err::Error(nom::error::Error::from_error_kind(
i,
ErrorKind::Tag,
)));
}
// Skip comments and empty newlines
let (i, _) = allspaces(i)?;
let account = Account {
jid: BareJid::from_str(jid.trim()).unwrap(),
password: String::from(password.trim()),
jid: jid.unwrap(),
password: password.unwrap(),
resource,
};
Ok((i, (String::from(name), account)))
}
@ -188,6 +210,7 @@ mod tests {
Account {
jid: BareJid::from_str(format!("{}@localhost", name).as_str()).unwrap(),
password: String::from("password"),
resource: None,
}
}
@ -208,13 +231,14 @@ mod tests {
#[test]
fn test_account() {
let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: 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 buf2 = "[Client] louise's phone \n\tjid: louise2@localhost\n\tpassword: password\n\tresource: resource1\n";
let buf3 = "[Client] louise\njid: louise@localhost\n\tpassword: password\n";
let name = String::from("louise");
let account = Account {
jid: BareJid::from_str("louise@localhost").unwrap(),
password: String::from("password"),
resource: None,
};
assert_eq!(
parse_account(buf1),
@ -225,12 +249,14 @@ mod tests {
let account = Account {
jid: BareJid::from_str("louise2@localhost").unwrap(),
password: String::from("password"),
resource: Some(String::from("resource1")),
};
assert_eq!(
parse_account(buf2),
Ok(("", (name.clone(), account.clone())))
);
// Missing tab
match parse_account(buf3) {
Err(_) => (),
err => panic!("Unexpected result: {:?}", err),
@ -247,6 +273,7 @@ mod tests {
[Client]
jid: sugako@localhost
password: password
resource: resource1
"#;
let mut accounts: HashMap<AccountName, Account> = HashMap::new();
@ -255,6 +282,7 @@ mod tests {
Account {
jid: BareJid::from_str("louise@localhost").unwrap(),
password: String::from("password"),
resource: None,
},
);
accounts.insert(
@ -262,6 +290,7 @@ mod tests {
Account {
jid: BareJid::from_str("sugako@localhost").unwrap(),
password: String::from("password"),
resource: Some(String::from("resource1")),
},
);
assert_eq!(parse_accounts(buf1), Ok(("", accounts)));