Tabs are now meaningful

Signed-off-by: Maxime “pep” Buquet <pep@bouah.net>
This commit is contained in:
Maxime “pep” Buquet 2023-01-08 19:50:29 +01:00
parent e9becda2b0
commit 8f48183e30
Signed by: pep
GPG key ID: DEDA74AEECA9D0F2

View file

@ -10,8 +10,8 @@ use std::str::FromStr;
use nom::{
self,
branch::alt,
bytes::complete::{tag, take_until, take_until1, take_while1},
character::complete::{multispace0, space0},
bytes::complete::{tag, take_until, take_while1},
character::complete::{multispace0, space0, space1},
multi::{many0, many1},
sequence::{delimited, tuple},
IResult,
@ -39,8 +39,8 @@ pub struct Spec {
pub actions: Vec<Action>,
}
fn is_space(c: char) -> bool {
c == ' ' || c == '\t' || c == '\n'
fn is_not_space(c: char) -> bool {
c != ' ' && c != '\t' && c != '\r' && c != '\n'
}
fn allspaces(i: &str) -> IResult<&str, &str> {
@ -55,20 +55,28 @@ fn comment(i: &str) -> IResult<&str, &str> {
}
fn parse_account(i: &str) -> IResult<&str, (AccountName, Account)> {
let (i, (_, _, _, name, _, _, _, jid, _, _, _, password, _)) = tuple((
let (i, (_, _, _, name, _)) = tuple((
allspaces,
tag("[Client]"),
multispace0,
take_while1(|c| !is_space(c)),
allspaces,
space0,
take_while1(is_not_space),
space0,
))(i)?;
let (i, (_, _, _, _, jid, _)) = tuple((
tag("\n"),
many1(tag("\t")),
tag("jid:"),
space0,
take_while1(|c| !is_space(c)),
allspaces,
take_while1(is_not_space),
space0,
))(i)?;
let (i, (_, _, _, _, password, _)) = tuple((
tag("\n"),
many1(tag("\t")),
tag("password:"),
space0,
take_until1("\n"),
allspaces,
take_while1(is_not_space),
multispace0,
))(i)?;
let account = Account {
jid: BareJid::from_str(jid).unwrap(),
@ -105,7 +113,14 @@ pub fn parse_spec(i: &str) -> Result<Spec, nom::Err<nom::error::Error<&str>>> {
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::{assert_eq, assert_ne};
use pretty_assertions::assert_eq;
fn get_account(name: &str) -> Account {
Account {
jid: BareJid::from_str(format!("{}@localhost", name).as_str()).unwrap(),
password: String::from("password"),
}
}
#[test]
fn test_comment() {
@ -115,20 +130,16 @@ mod tests {
assert_eq!(comment(buf2), Ok(("", "")));
let buf3 = " # Foo\n";
assert_ne!(comment(buf3), Ok(("", "")));
}
#[test]
fn test_allspaces() {
let buf1 = "\n# Foo\n\n";
assert_eq!(allspaces(buf1), Ok(("", "")));
match comment(buf3) {
Err(_) => (),
err => panic!("Unexpected result: {:?}", err),
}
}
#[test]
fn test_account() {
let buf1 = "[Client] louise\n\tjid: louise@localhost\n\tpassword: password\n";
let buf2 =
"# Foo\n[Client] louise # Bar\n\tjid: louise@localhost\n# Baz\n\tpassword: password\n";
let buf2 = "[Client] louise\njid: louise@localhost\npassword: password\n";
let name = String::from("louise");
let account = Account {
@ -139,20 +150,54 @@ mod tests {
parse_account(buf1),
Ok(("", (name.clone(), account.clone())))
);
assert_eq!(parse_account(buf2), Ok(("", (name, account))));
match parse_account(buf2) {
Err(_) => (),
err => panic!("Unexpected result: {:?}", err),
}
}
#[test]
fn test_accounts() {
let buf1 = r#"
[Client] louise
jid: louise@localhost
password: password
[Client] louise
jid: louise@localhost
password: password
[Client]
jid: sugako@localhost
password: password
"#;
[Client]
jid: sugako@localhost
password: password
"#;
let mut accounts: HashMap<AccountName, Account> = HashMap::new();
accounts.insert(
String::from("louise"),
Account {
jid: BareJid::from_str("louise@localhost").unwrap(),
password: String::from("password"),
},
);
accounts.insert(
String::from("須賀子"),
Account {
jid: BareJid::from_str("sugako@localhost").unwrap(),
password: String::from("password"),
},
);
assert_eq!(parse_accounts(buf1), Ok(("", accounts)));
}
#[ignore]
#[test]
fn test_duplicate_accounts() {
let buf1 = r#"
[Client] louise
jid: louise@localhost
password: password
[Client] louise
jid: louise2@localhost
password: password
"#;
let mut accounts: HashMap<AccountName, Account> = HashMap::new();
accounts.insert(