From 54debb3e2f169ff837d3dedd1b2c2912147e4bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maxime=20=E2=80=9Cpep=E2=80=9D=20Buquet?= Date: Thu, 12 Jan 2023 22:41:53 +0100 Subject: [PATCH] Rename Account to Client MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Maxime “pep” Buquet --- src/lib.rs | 90 +++++++++++++++++++++++++++--------------------------- 1 file changed, 45 insertions(+), 45 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2dff3e8..4462b4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -43,19 +43,19 @@ impl<'a> From>>> for Token<'a> { pub static DEFAULT_NS: &'static str = "jabber:client"; pub static SCANSION_NS: &'static str = "https://matthewwild.co.uk/projects/scansion"; -pub type AccountName = String; +pub type ClientName = String; #[derive(Debug, Clone, PartialEq)] -pub struct Account { +pub struct Client { pub jid: Jid, pub password: String, pub custom_host: Option, pub custom_port: Option, } -impl Account { - pub fn new>(jid: Jid, password: S) -> Account { - Account { +impl Client { + pub fn new>(jid: Jid, password: S) -> Client { + Client { jid, password: password.into(), custom_host: None, @@ -63,12 +63,12 @@ impl Account { } } - pub fn with_custom_host>(mut self, custom_host: S) -> Account { + pub fn with_custom_host>(mut self, custom_host: S) -> Client { self.custom_host = Some(custom_host.into()); self } - pub fn with_custom_port(mut self, custom_port: u16) -> Account { + pub fn with_custom_port(mut self, custom_port: u16) -> Client { self.custom_port = Some(custom_port); self } @@ -76,15 +76,15 @@ impl Account { #[derive(Debug, Clone, PartialEq)] pub enum Action { - Connect(AccountName), - Send(AccountName, Element), - Receive(AccountName, Element), - Disconnect(AccountName), + Connect(ClientName), + Send(ClientName, Element), + Receive(ClientName, Element), + Disconnect(ClientName), } #[derive(Debug, Clone, PartialEq)] pub struct Spec { - pub accounts: HashMap, + pub clients: HashMap, pub actions: Vec, } @@ -139,7 +139,7 @@ where } } -fn parse_account(s: Span) -> IResult { +fn parse_client(s: Span) -> IResult { let (s, (_, _, _, name, _)) = tuple((allspaces, tag("[Client]"), space0, take_until("\n"), space0))(s)?; @@ -191,18 +191,18 @@ fn parse_account(s: Span) -> IResult { // Skip comments and empty newlines let (s, _) = allspaces(s)?; - let mut account = Account::new(jid.unwrap(), password.unwrap()); - account.custom_host = custom_host.map(String::from); - account.custom_port = custom_port; + let mut client = Client::new(jid.unwrap(), password.unwrap()); + client.custom_host = custom_host.map(String::from); + client.custom_port = custom_port; - Ok((s, (String::from(name), account))) + Ok((s, (String::from(name), client))) } -fn parse_accounts(s: Span) -> IResult> { - let (s, accounts) = many0(parse_account)(s)?; - let mut map: HashMap = HashMap::new(); - for (name, account) in accounts { - map.insert(name, account); +fn parse_clients(s: Span) -> IResult> { + let (s, clients) = many0(parse_client)(s)?; + let mut map: HashMap = HashMap::new(); + for (name, client) in clients { + map.insert(name, client); } Ok((s, map)) } @@ -268,10 +268,10 @@ fn parse_actions(s: Span) -> IResult> { pub fn parse_spec(i: &str) -> Result { let s: Span = i.into(); - let (s, accounts) = parse_accounts(s)?; + let (s, clients) = parse_clients(s)?; let (s, _) = parse_sep(s)?; let (_, actions) = parse_actions(s)?; - Ok(Spec { accounts, actions }) + Ok(Spec { clients, actions }) } #[cfg(test)] @@ -279,8 +279,8 @@ mod tests { use super::*; use pretty_assertions::assert_eq; - fn get_account(name: &str) -> Account { - Account::new( + fn get_client(name: &str) -> Client { + Client::new( Jid::from_str(format!("{}@localhost", name).as_str()).unwrap(), "password", ) @@ -328,41 +328,41 @@ mod tests { } #[test] - fn test_account() { + fn test_client() { 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\tcustom_port: 5234\n"; let buf3 = "[Client] louise\njid: louise@localhost\n\tpassword: password\n"; let name = String::from("louise"); - let account = Account::new(Jid::from_str("louise@localhost").unwrap(), "password"); + let client = Client::new(Jid::from_str("louise@localhost").unwrap(), "password"); assert_eq!( - parse_account(buf1.into()), + parse_client(buf1.into()), Ok(( unsafe { LocatedSpan::new_from_raw_offset(59, 4, "", ()) }, - (name.clone(), account.clone()) + (name.clone(), client.clone()) )) ); let name = String::from("louise's phone"); - let account = Account::new(Jid::from_str("louise2@localhost").unwrap(), "password") + let client = Client::new(Jid::from_str("louise2@localhost").unwrap(), "password") .with_custom_port(5234); assert_eq!( - parse_account(buf2.into()), + parse_client(buf2.into()), Ok(( unsafe { LocatedSpan::new_from_raw_offset(88, 5, "", ()) }, - (name.clone(), account.clone()) + (name.clone(), client.clone()) )) ); // Missing tab - match parse_account(buf3.into()) { + match parse_client(buf3.into()) { Err(_) => (), err => panic!("Unexpected result: {:?}", err), } } #[test] - fn test_accounts() { + fn test_clients() { let buf1 = r#" [Client] louise jid: louise@localhost @@ -373,20 +373,20 @@ mod tests { password: password "#; - let mut accounts: HashMap = HashMap::new(); - accounts.insert( + let mut clients: HashMap = HashMap::new(); + clients.insert( String::from("louise"), - Account::new(Jid::from_str("louise@localhost").unwrap(), "password"), + Client::new(Jid::from_str("louise@localhost").unwrap(), "password"), ); - accounts.insert( + clients.insert( String::from("須賀子"), - Account::new(Jid::from_str("sugako@localhost").unwrap(), "password"), + Client::new(Jid::from_str("sugako@localhost").unwrap(), "password"), ); assert_eq!( - parse_accounts(buf1.into()), + parse_clients(buf1.into()), Ok(( unsafe { LocatedSpan::new_from_raw_offset(123, 9, "", ()) }, - accounts + clients )) ); } @@ -468,8 +468,8 @@ louise receives: "#; - let mut accounts: HashMap = HashMap::new(); - accounts.insert(String::from("louise"), get_account("louise")); + let mut clients: HashMap = HashMap::new(); + clients.insert(String::from("louise"), get_client("louise")); let xml1 = b"\n\n"; let xml2 = b"\n\n"; @@ -486,7 +486,7 @@ louise receives: ), ]; - let spec = Spec { accounts, actions }; + let spec = Spec { clients, actions }; assert_eq!(parse_spec(buf), Ok(spec)); }