diff --git a/src/lib.rs b/src/lib.rs index ae31627..3704eba 100644 --- a/src/lib.rs +++ b/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, pub custom_host: Option, pub custom_port: Option, } impl Account { - pub fn new>(jid: BareJid, password: S) -> Account { + pub fn new>(jid: Jid, password: S) -> Account { Account { jid, password: password.into(), - resource: None, custom_host: None, custom_port: None, } } - pub fn with_resource>(mut self, resource: S) -> Account { - self.resource = Some(resource.into()); - self - } - pub fn with_custom_host>(mut self, custom_host: S) -> Account { self.custom_host = Some(custom_host.into()); self @@ -156,9 +149,8 @@ fn parse_account(s: Span) -> IResult { )))(s)?; let name = name.trim(); - let mut jid: Option = None; + let mut jid: Option = 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 = None; @@ -167,9 +159,8 @@ fn parse_account(s: Span) -> IResult { 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 { 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 = 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 )) );