Allow Jid instead of BareJid to replace 'resource'
Some checks are pending
ci/woodpecker/push/woodpecker Pipeline is pending
Some checks are pending
ci/woodpecker/push/woodpecker Pipeline is pending
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
d1d3c57af9
commit
98174ec66b
1 changed files with 14 additions and 26 deletions
40
src/lib.rs
40
src/lib.rs
|
@ -7,7 +7,7 @@
|
|||
use std::collections::HashMap;
|
||||
use std::str::FromStr;
|
||||
|
||||
use jid::BareJid;
|
||||
use jid::Jid;
|
||||
use minidom::Element;
|
||||
use nom::{
|
||||
self,
|
||||
|
@ -46,29 +46,22 @@ pub type AccountName = String;
|
|||
|
||||
#[derive(Debug, Clone, PartialEq)]
|
||||
pub struct Account {
|
||||
pub jid: BareJid,
|
||||
pub jid: Jid,
|
||||
pub password: String,
|
||||
pub resource: Option<String>,
|
||||
pub custom_host: Option<String>,
|
||||
pub custom_port: Option<u16>,
|
||||
}
|
||||
|
||||
impl Account {
|
||||
pub fn new<S: Into<String>>(jid: BareJid, password: S) -> Account {
|
||||
pub fn new<S: Into<String>>(jid: Jid, password: S) -> Account {
|
||||
Account {
|
||||
jid,
|
||||
password: password.into(),
|
||||
resource: None,
|
||||
custom_host: None,
|
||||
custom_port: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_resource<S: Into<String>>(mut self, resource: S) -> Account {
|
||||
self.resource = Some(resource.into());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn with_custom_host<S: Into<String>>(mut self, custom_host: S) -> Account {
|
||||
self.custom_host = Some(custom_host.into());
|
||||
self
|
||||
|
@ -156,9 +149,8 @@ fn parse_account(s: Span) -> IResult<Span, (AccountName, Account)> {
|
|||
)))(s)?;
|
||||
|
||||
let name = name.trim();
|
||||
let mut jid: Option<BareJid> = None;
|
||||
let mut jid: Option<Jid> = None;
|
||||
let mut password: Option<&str> = None;
|
||||
let mut resource: Option<&str> = None;
|
||||
let mut custom_host: Option<&str> = None;
|
||||
let mut custom_port: Option<u16> = None;
|
||||
|
||||
|
@ -167,9 +159,8 @@ fn parse_account(s: Span) -> IResult<Span, (AccountName, Account)> {
|
|||
if let Some((key, val)) = attr.split_once(':') {
|
||||
let val = val.trim();
|
||||
match key.trim() {
|
||||
"jid" => jid = Some(BareJid::from_str(val).unwrap()),
|
||||
"jid" => jid = Some(Jid::from_str(val).unwrap()),
|
||||
"password" => password = Some(val),
|
||||
"resource" => resource = Some(val),
|
||||
"custom_host" => custom_host = Some(val),
|
||||
"custom_port" => {
|
||||
let val: Span = val.into();
|
||||
|
@ -200,7 +191,6 @@ fn parse_account(s: Span) -> IResult<Span, (AccountName, Account)> {
|
|||
let (s, _) = allspaces(s)?;
|
||||
|
||||
let mut account = Account::new(jid.unwrap(), password.unwrap());
|
||||
account.resource = resource.map(String::from);
|
||||
account.custom_host = custom_host.map(String::from);
|
||||
account.custom_port = custom_port;
|
||||
|
||||
|
@ -287,7 +277,7 @@ mod tests {
|
|||
|
||||
fn get_account(name: &str) -> Account {
|
||||
Account::new(
|
||||
BareJid::from_str(format!("{}@localhost", name).as_str()).unwrap(),
|
||||
Jid::from_str(format!("{}@localhost", name).as_str()).unwrap(),
|
||||
"password",
|
||||
)
|
||||
}
|
||||
|
@ -336,11 +326,11 @@ 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\tresource: resource1\n";
|
||||
let buf2 = "[Client] louise's phone \n\tjid: louise2@localhost\n\tpassword: password\n\tcustom_port: 5234\n";
|
||||
let buf3 = "[Client] louise\njid: louise@localhost\n\tpassword: password\n";
|
||||
|
||||
let name = String::from("louise");
|
||||
let account = Account::new(BareJid::from_str("louise@localhost").unwrap(), "password");
|
||||
let account = Account::new(Jid::from_str("louise@localhost").unwrap(), "password");
|
||||
assert_eq!(
|
||||
parse_account(buf1.into()),
|
||||
Ok((
|
||||
|
@ -350,12 +340,12 @@ mod tests {
|
|||
);
|
||||
|
||||
let name = String::from("louise's phone");
|
||||
let account = Account::new(BareJid::from_str("louise2@localhost").unwrap(), "password")
|
||||
.with_resource("resource1");
|
||||
let account = Account::new(Jid::from_str("louise2@localhost").unwrap(), "password")
|
||||
.with_custom_port(5234);
|
||||
assert_eq!(
|
||||
parse_account(buf2.into()),
|
||||
Ok((
|
||||
unsafe { LocatedSpan::new_from_raw_offset(90, 5, "", ()) },
|
||||
unsafe { LocatedSpan::new_from_raw_offset(88, 5, "", ()) },
|
||||
(name.clone(), account.clone())
|
||||
))
|
||||
);
|
||||
|
@ -377,23 +367,21 @@ mod tests {
|
|||
[Client] 須賀子
|
||||
jid: sugako@localhost
|
||||
password: password
|
||||
resource: resource1
|
||||
"#;
|
||||
|
||||
let mut accounts: HashMap<AccountName, Account> = HashMap::new();
|
||||
accounts.insert(
|
||||
String::from("louise"),
|
||||
Account::new(BareJid::from_str("louise@localhost").unwrap(), "password"),
|
||||
Account::new(Jid::from_str("louise@localhost").unwrap(), "password"),
|
||||
);
|
||||
accounts.insert(
|
||||
String::from("須賀子"),
|
||||
Account::new(BareJid::from_str("sugako@localhost").unwrap(), "password")
|
||||
.with_resource("resource1"),
|
||||
Account::new(Jid::from_str("sugako@localhost").unwrap(), "password"),
|
||||
);
|
||||
assert_eq!(
|
||||
parse_accounts(buf1.into()),
|
||||
Ok((
|
||||
unsafe { LocatedSpan::new_from_raw_offset(144, 10, "", ()) },
|
||||
unsafe { LocatedSpan::new_from_raw_offset(123, 9, "", ()) },
|
||||
accounts
|
||||
))
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue