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 DEFAULT_NS: &'static str = "jabber:client";
|
||||||
pub static SCANSION_NS: &'static str = "https://matthewwild.co.uk/projects/scansion";
|
pub static SCANSION_NS: &'static str = "https://matthewwild.co.uk/projects/scansion";
|
||||||
|
|
||||||
pub type AccountName = String;
|
pub type ClientName = String;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Account {
|
pub struct Client {
|
||||||
pub jid: Jid,
|
pub jid: Jid,
|
||||||
pub password: String,
|
pub password: String,
|
||||||
pub custom_host: Option<String>,
|
pub custom_host: Option<String>,
|
||||||
pub custom_port: Option<u16>,
|
pub custom_port: Option<u16>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Account {
|
impl Client {
|
||||||
pub fn new<S: Into<String>>(jid: Jid, password: S) -> Account {
|
pub fn new<S: Into<String>>(jid: Jid, password: S) -> Client {
|
||||||
Account {
|
Client {
|
||||||
jid,
|
jid,
|
||||||
password: password.into(),
|
password: password.into(),
|
||||||
custom_host: None,
|
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.custom_host = Some(custom_host.into());
|
||||||
self
|
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.custom_port = Some(custom_port);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@ -76,15 +76,15 @@ impl Account {
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub enum Action {
|
pub enum Action {
|
||||||
Connect(AccountName),
|
Connect(ClientName),
|
||||||
Send(AccountName, Element),
|
Send(ClientName, Element),
|
||||||
Receive(AccountName, Element),
|
Receive(ClientName, Element),
|
||||||
Disconnect(AccountName),
|
Disconnect(ClientName),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq)]
|
#[derive(Debug, Clone, PartialEq)]
|
||||||
pub struct Spec {
|
pub struct Spec {
|
||||||
pub accounts: HashMap<AccountName, Account>,
|
pub clients: HashMap<ClientName, Client>,
|
||||||
pub actions: Vec<Action>,
|
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, _)) =
|
let (s, (_, _, _, name, _)) =
|
||||||
tuple((allspaces, tag("[Client]"), space0, take_until("\n"), space0))(s)?;
|
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
|
// Skip comments and empty newlines
|
||||||
let (s, _) = allspaces(s)?;
|
let (s, _) = allspaces(s)?;
|
||||||
|
|
||||||
let mut account = Account::new(jid.unwrap(), password.unwrap());
|
let mut client = Client::new(jid.unwrap(), password.unwrap());
|
||||||
account.custom_host = custom_host.map(String::from);
|
client.custom_host = custom_host.map(String::from);
|
||||||
account.custom_port = custom_port;
|
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>> {
|
fn parse_clients(s: Span) -> IResult<Span, HashMap<ClientName, Client>> {
|
||||||
let (s, accounts) = many0(parse_account)(s)?;
|
let (s, clients) = many0(parse_client)(s)?;
|
||||||
let mut map: HashMap<AccountName, Account> = HashMap::new();
|
let mut map: HashMap<ClientName, Client> = HashMap::new();
|
||||||
for (name, account) in accounts {
|
for (name, client) in clients {
|
||||||
map.insert(name, account);
|
map.insert(name, client);
|
||||||
}
|
}
|
||||||
Ok((s, map))
|
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> {
|
pub fn parse_spec(i: &str) -> Result<Spec, Token> {
|
||||||
let s: Span = i.into();
|
let s: Span = i.into();
|
||||||
let (s, accounts) = parse_accounts(s)?;
|
let (s, clients) = parse_clients(s)?;
|
||||||
let (s, _) = parse_sep(s)?;
|
let (s, _) = parse_sep(s)?;
|
||||||
let (_, actions) = parse_actions(s)?;
|
let (_, actions) = parse_actions(s)?;
|
||||||
Ok(Spec { accounts, actions })
|
Ok(Spec { clients, actions })
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
@ -279,8 +279,8 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
use pretty_assertions::assert_eq;
|
use pretty_assertions::assert_eq;
|
||||||
|
|
||||||
fn get_account(name: &str) -> Account {
|
fn get_client(name: &str) -> Client {
|
||||||
Account::new(
|
Client::new(
|
||||||
Jid::from_str(format!("{}@localhost", name).as_str()).unwrap(),
|
Jid::from_str(format!("{}@localhost", name).as_str()).unwrap(),
|
||||||
"password",
|
"password",
|
||||||
)
|
)
|
||||||
|
@ -328,41 +328,41 @@ mod tests {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_account() {
|
fn test_client() {
|
||||||
let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: password\n";
|
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 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 buf3 = "[Client] louise\njid: louise@localhost\n\tpassword: password\n";
|
||||||
|
|
||||||
let name = String::from("louise");
|
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!(
|
assert_eq!(
|
||||||
parse_account(buf1.into()),
|
parse_client(buf1.into()),
|
||||||
Ok((
|
Ok((
|
||||||
unsafe { LocatedSpan::new_from_raw_offset(59, 4, "", ()) },
|
unsafe { LocatedSpan::new_from_raw_offset(59, 4, "", ()) },
|
||||||
(name.clone(), account.clone())
|
(name.clone(), client.clone())
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
let name = String::from("louise's phone");
|
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);
|
.with_custom_port(5234);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_account(buf2.into()),
|
parse_client(buf2.into()),
|
||||||
Ok((
|
Ok((
|
||||||
unsafe { LocatedSpan::new_from_raw_offset(88, 5, "", ()) },
|
unsafe { LocatedSpan::new_from_raw_offset(88, 5, "", ()) },
|
||||||
(name.clone(), account.clone())
|
(name.clone(), client.clone())
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
|
|
||||||
// Missing tab
|
// Missing tab
|
||||||
match parse_account(buf3.into()) {
|
match parse_client(buf3.into()) {
|
||||||
Err(_) => (),
|
Err(_) => (),
|
||||||
err => panic!("Unexpected result: {:?}", err),
|
err => panic!("Unexpected result: {:?}", err),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_accounts() {
|
fn test_clients() {
|
||||||
let buf1 = r#"
|
let buf1 = r#"
|
||||||
[Client] louise
|
[Client] louise
|
||||||
jid: louise@localhost
|
jid: louise@localhost
|
||||||
|
@ -373,20 +373,20 @@ mod tests {
|
||||||
password: password
|
password: password
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let mut accounts: HashMap<AccountName, Account> = HashMap::new();
|
let mut clients: HashMap<ClientName, Client> = HashMap::new();
|
||||||
accounts.insert(
|
clients.insert(
|
||||||
String::from("louise"),
|
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("須賀子"),
|
String::from("須賀子"),
|
||||||
Account::new(Jid::from_str("sugako@localhost").unwrap(), "password"),
|
Client::new(Jid::from_str("sugako@localhost").unwrap(), "password"),
|
||||||
);
|
);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
parse_accounts(buf1.into()),
|
parse_clients(buf1.into()),
|
||||||
Ok((
|
Ok((
|
||||||
unsafe { LocatedSpan::new_from_raw_offset(123, 9, "", ()) },
|
unsafe { LocatedSpan::new_from_raw_offset(123, 9, "", ()) },
|
||||||
accounts
|
clients
|
||||||
))
|
))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -468,8 +468,8 @@ louise receives:
|
||||||
|
|
||||||
"#;
|
"#;
|
||||||
|
|
||||||
let mut accounts: HashMap<AccountName, Account> = HashMap::new();
|
let mut clients: HashMap<ClientName, Client> = HashMap::new();
|
||||||
accounts.insert(String::from("louise"), get_account("louise"));
|
clients.insert(String::from("louise"), get_client("louise"));
|
||||||
|
|
||||||
let xml1 = b"<presence to=\"some@room\" />\n\n";
|
let xml1 = b"<presence to=\"some@room\" />\n\n";
|
||||||
let xml2 = b"<message from=\"louise@localhost\"\n\t\t/>\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));
|
assert_eq!(parse_spec(buf), Ok(spec));
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue