0.1.0 |
|
---|
#8 in #kma
119 downloads per month
12KB
163 lines
toml_contact_book_parser
Toml Contact Book Parser
This is a simple parser for contact book, stored in toml format. It can parse a file with contacts and return a list of contacts. Made for Rust course.
Table of contents
Example of contact book
[contact]
name = "John"
surname = "Doe"
phones = ["+380501234567", "+380501234568"]
address = "Some address"
birthday = "2000-01-01"
Usage example
use contact_book_parser_protsap::parse_contacts;
fn main() {
let raw_contact_str = "[contact]\nname = \"John\"\nsurname = \"Doe\"\nphones = [\"+380501..."; // fill with your contact book or load from file
let contacts = parse_contacts(raw_contact_str).unwrap();
println!("{:?}", contacts);
// [Contact { name: "John", surname: "Doe", phones: ["+380501234567", "+380501234568"], address: "Some address", birthday: "2000-01-01" }, ...]
}
Grammar
// Helper rules
equals = _{ " "* ~ "=" ~ " "* }
// Phone helper rules
country_code = _{ ASCII_DIGIT{1,3} }
area_code = _{ ASCII_DIGIT{3} }
exchange_code = _{ ASCII_DIGIT{3} }
subscriber_number = _{ ASCII_DIGIT{4} }
subscriber_number_dashed = _{ ASCII_DIGIT{2} ~ "-" ~ ASCII_DIGIT{2} }
subscriber_number_combo = _{ subscriber_number | subscriber_number_dashed }
// Main rules
name = { "\"" ~ (ASCII_ALPHA)+ ~ "\"" }
surname = { "\"" ~ (ASCII_ALPHA)+ ~ "\"" }
address = { "\"" ~ (ASCII_ALPHANUMERIC | " " | "." | "," | "-")+ ~ "\"" }
phone = { "\"" ~ ("+")? ~ (
// Without brackets
(ASCII_DIGIT{11,13}) |
(country_code ~ " "{0,1} ~ area_code ~ " "{0,1} ~ exchange_code ~ " "{0,1} ~ subscriber_number) |
(country_code ~ "-" ~ area_code ~ "-" ~ exchange_code ~ "-" ~ subscriber_number) |
// With brackets
(country_code ~ " "{0,1} ~ "(" ~ area_code ~ ")" ~ " "{0,1} ~ exchange_code ~ " "{0,1} ~ subscriber_number) |
(country_code ~ "(" ~ area_code ~ ")" ~ "-"{0,1} ~ exchange_code ~ "-" ~ subscriber_number_combo)
) ~ "\"" }
date = { "\"" ~ ASCII_DIGIT{4} ~ "-" ~ ASCII_DIGIT{2} ~ "-" ~ ASCII_DIGIT{2} ~ "\"" }
phones = { "[" ~ phone ~ ("," ~ " "{0, 1} ~ phone)* ~ "]" }
contact = {
"[contact]" ~ " "* ~ NEWLINE ~
"name" ~ equals ~ name ~ NEWLINE ~
"surname" ~ equals ~ surname ~ NEWLINE ~
"phones" ~ equals ~ phones ~ NEWLINE ~
"address" ~ equals ~ address ~ NEWLINE ~
"birthday" ~ equals ~ date ~ NEWLINE
}
file = { SOI ~ (contact ~ NEWLINE{0,})* ~ EOI }
Dependencies
~4MB
~74K SLoC