Rename Account to Client
Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
parent
ab24ddf14c
commit
54debb3e2f
1 changed files with 45 additions and 45 deletions
90
src/lib.rs
90
src/lib.rs
|
@ -43,19 +43,19 @@ impl<'a> From<NomErr<nom::error::Error<LocatedSpan<&'a str>>>> 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<String>,
|
||||
pub custom_port: Option<u16>,
|
||||
}
|
||||
|
||||
impl Account {
|
||||
pub fn new<S: Into<String>>(jid: Jid, password: S) -> Account {
|
||||
Account {
|
||||
impl Client {
|
||||
pub fn new<S: Into<String>>(jid: Jid, password: S) -> Client {
|
||||
Client {
|
||||
jid,
|
||||
password: password.into(),
|
||||
custom_host: None,
|
||||
|
@ -63,12 +63,12 @@ impl Account {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn with_custom_host<S: Into<String>>(mut self, custom_host: S) -> Account {
|
||||
pub fn with_custom_host<S: Into<String>>(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<AccountName, Account>,
|
||||
pub clients: HashMap<ClientName, Client>,
|
||||
pub actions: Vec<Action>,
|
||||
}
|
||||
|
||||
|
@ -139,7 +139,7 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn parse_account(s: Span) -> IResult<Span, (AccountName, Account)> {
|
||||
fn parse_client(s: Span) -> IResult<Span, (ClientName, Client)> {
|
||||
let (s, (_, _, _, name, _)) =
|
||||
tuple((allspaces, tag("[Client]"), space0, take_until("\n"), space0))(s)?;
|
||||
|
||||
|
@ -191,18 +191,18 @@ fn parse_account(s: Span) -> IResult<Span, (AccountName, Account)> {
|
|||
// 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<Span, HashMap<AccountName, Account>> {
|
||||
let (s, accounts) = many0(parse_account)(s)?;
|
||||
let mut map: HashMap<AccountName, Account> = HashMap::new();
|
||||
for (name, account) in accounts {
|
||||
map.insert(name, account);
|
||||
fn parse_clients(s: Span) -> IResult<Span, HashMap<ClientName, Client>> {
|
||||
let (s, clients) = many0(parse_client)(s)?;
|
||||
let mut map: HashMap<ClientName, Client> = HashMap::new();
|
||||
for (name, client) in clients {
|
||||
map.insert(name, client);
|
||||
}
|
||||
Ok((s, map))
|
||||
}
|
||||
|
@ -268,10 +268,10 @@ fn parse_actions(s: Span) -> IResult<Span, Vec<Action>> {
|
|||
|
||||
pub fn parse_spec(i: &str) -> Result<Spec, Token> {
|
||||
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<AccountName, Account> = HashMap::new();
|
||||
accounts.insert(
|
||||
let mut clients: HashMap<ClientName, Client> = 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<AccountName, Account> = HashMap::new();
|
||||
accounts.insert(String::from("louise"), get_account("louise"));
|
||||
let mut clients: HashMap<ClientName, Client> = HashMap::new();
|
||||
clients.insert(String::from("louise"), get_client("louise"));
|
||||
|
||||
let xml1 = b"<presence to=\"some@room\" />\n\n";
|
||||
let xml2 = b"<message from=\"louise@localhost\"\n\t\t/>\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));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue