Merge pull request #1454 from matthiasbeyer/imag-contact/do-not-require-fields

Fix: Make most fields optional
This commit is contained in:
Matthias Beyer 2018-04-26 16:14:48 +02:00 committed by GitHub
commit 47c4a360f7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 28 additions and 29 deletions

View File

@ -177,18 +177,18 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
{ // parse name { // parse name
debug!("Parsing name"); debug!("Parsing name");
let firstname = read_str_from_toml(&toml, "name.first"); let firstname = read_str_from_toml(&toml, "name.first", true);
trace!("firstname = {:?}", firstname); trace!("firstname = {:?}", firstname);
let lastname = read_str_from_toml(&toml, "name.last"); let lastname = read_str_from_toml(&toml, "name.last", true);
trace!("lastname = {:?}", lastname); trace!("lastname = {:?}", lastname);
vcard = vcard.with_name(parameters!(), vcard = vcard.with_name(parameters!(),
read_str_from_toml(&toml, "name.prefix"), read_str_from_toml(&toml, "name.prefix", false),
firstname.clone(), firstname.clone(),
read_str_from_toml(&toml, "name.additional"), read_str_from_toml(&toml, "name.additional", false),
lastname.clone(), lastname.clone(),
read_str_from_toml(&toml, "name.suffix")); read_str_from_toml(&toml, "name.suffix", false));
if let (Some(first), Some(last)) = (firstname, lastname) { if let (Some(first), Some(last)) = (firstname, lastname) {
trace!("Building fullname: '{} {}'", first, last); trace!("Building fullname: '{} {}'", first, last);
@ -198,7 +198,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
{ // parse personal { // parse personal
debug!("Parsing person information"); debug!("Parsing person information");
let birthday = read_str_from_toml(&toml, "person.birthday"); let birthday = read_str_from_toml(&toml, "person.birthday", false);
trace!("birthday = {:?}", birthday); trace!("birthday = {:?}", birthday);
if let Some(bday) = birthday { if let Some(bday) = birthday {
@ -211,7 +211,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
match toml.read("nickname").map_err_trace_exit_unwrap(1) { match toml.read("nickname").map_err_trace_exit_unwrap(1) {
Some(&Value::Array(ref ary)) => { Some(&Value::Array(ref ary)) => {
for (i, element) in ary.iter().enumerate() { for (i, element) in ary.iter().enumerate() {
let nicktype = match read_str_from_toml(element, "type") { let nicktype = match read_str_from_toml(element, "type", false) {
None => BTreeMap::new(), None => BTreeMap::new(),
Some(p) => { Some(p) => {
let mut m = BTreeMap::new(); let mut m = BTreeMap::new();
@ -220,7 +220,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
}, },
}; };
let name = match read_str_from_toml(element, "name") { let name = match read_str_from_toml(element, "name", false) {
Some(p) => p, Some(p) => p,
None => { None => {
error!("Key 'nickname.[{}].name' missing", i); error!("Key 'nickname.[{}].name' missing", i);
@ -255,17 +255,14 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
if let Some(orgs) = read_strary_from_toml(&toml, "organisation.name") { if let Some(orgs) = read_strary_from_toml(&toml, "organisation.name") {
trace!("orgs = {:?}", orgs); trace!("orgs = {:?}", orgs);
vcard = vcard.with_org(orgs); vcard = vcard.with_org(orgs);
} else {
error!("Key 'organisation.name' missing");
ask_continue! { yes => return None; no => exit(1) };
} }
if let Some(title) = read_str_from_toml(&toml, "organisation.title") { if let Some(title) = read_str_from_toml(&toml, "organisation.title", false) {
trace!("title = {:?}", title); trace!("title = {:?}", title);
vcard = vcard.with_title(title); vcard = vcard.with_title(title);
} }
if let Some(role) = read_str_from_toml(&toml, "organisation.role") { if let Some(role) = read_str_from_toml(&toml, "organisation.role", false) {
trace!("role = {:?}", role); trace!("role = {:?}", role);
vcard = vcard.with_role(role); vcard = vcard.with_role(role);
} }
@ -276,7 +273,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
match toml.read("person.phone").map_err_trace_exit_unwrap(1) { match toml.read("person.phone").map_err_trace_exit_unwrap(1) {
Some(&Value::Array(ref ary)) => { Some(&Value::Array(ref ary)) => {
for (i, element) in ary.iter().enumerate() { for (i, element) in ary.iter().enumerate() {
let phonetype = match read_str_from_toml(element, "type") { let phonetype = match read_str_from_toml(element, "type", false) {
Some(p) => p, Some(p) => p,
None => { None => {
error!("Key 'phones.[{}].type' missing", i); error!("Key 'phones.[{}].type' missing", i);
@ -284,7 +281,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
} }
}; };
let number = match read_str_from_toml(element, "number") { let number = match read_str_from_toml(element, "number", false) {
Some(p) => p, Some(p) => p,
None => { None => {
error!("Key 'phones.[{}].number' missing", i); error!("Key 'phones.[{}].number' missing", i);
@ -314,7 +311,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
match toml.read("addresses").map_err_trace_exit_unwrap(1) { match toml.read("addresses").map_err_trace_exit_unwrap(1) {
Some(&Value::Array(ref ary)) => { Some(&Value::Array(ref ary)) => {
for (i, element) in ary.iter().enumerate() { for (i, element) in ary.iter().enumerate() {
let adrtype = match read_str_from_toml(element, "type") { let adrtype = match read_str_from_toml(element, "type", false) {
None => { None => {
error!("Key 'adresses.[{}].type' missing", i); error!("Key 'adresses.[{}].type' missing", i);
ask_continue! { yes => return None; no => exit(1) }; ask_continue! { yes => return None; no => exit(1) };
@ -323,13 +320,13 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
}; };
trace!("adrtype = {:?}", adrtype); trace!("adrtype = {:?}", adrtype);
let bx = read_str_from_toml(element, "box"); let bx = read_str_from_toml(element, "box", false);
let extended = read_str_from_toml(element, "extended"); let extended = read_str_from_toml(element, "extended", false);
let street = read_str_from_toml(element, "street"); let street = read_str_from_toml(element, "street", false);
let code = read_str_from_toml(element, "code"); let code = read_str_from_toml(element, "code", false);
let city = read_str_from_toml(element, "city"); let city = read_str_from_toml(element, "city", false);
let region = read_str_from_toml(element, "region"); let region = read_str_from_toml(element, "region", false);
let country = read_str_from_toml(element, "country"); let country = read_str_from_toml(element, "country", false);
trace!("bx = {:?}", bx); trace!("bx = {:?}", bx);
trace!("extended = {:?}", extended); trace!("extended = {:?}", extended);
@ -361,7 +358,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
match toml.read("person.email").map_err_trace_exit_unwrap(1) { match toml.read("person.email").map_err_trace_exit_unwrap(1) {
Some(&Value::Array(ref ary)) => { Some(&Value::Array(ref ary)) => {
for (i, element) in ary.iter().enumerate() { for (i, element) in ary.iter().enumerate() {
let mailtype = match read_str_from_toml(element, "type") { let mailtype = match read_str_from_toml(element, "type", false) {
None => { None => {
error!("Error: 'email.[{}].type' missing", i); error!("Error: 'email.[{}].type' missing", i);
ask_continue! { yes => return None; no => exit(1) }; ask_continue! { yes => return None; no => exit(1) };
@ -369,7 +366,7 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
Some(p) => p, Some(p) => p,
}; // TODO: Unused, because unsupported by vobject }; // TODO: Unused, because unsupported by vobject
let mail = match read_str_from_toml(element, "addr") { let mail = match read_str_from_toml(element, "addr", false) {
None => { None => {
error!("Error: 'email.[{}].addr' missing", i); error!("Error: 'email.[{}].addr' missing", i);
ask_continue! { yes => return None; no => exit(1) }; ask_continue! { yes => return None; no => exit(1) };
@ -402,13 +399,13 @@ fn parse_toml_into_vcard(toml: Value) -> Option<Vcard> {
debug!("No categories"); debug!("No categories");
} }
if let Some(webpage) = read_str_from_toml(&toml, "other.webpage") { if let Some(webpage) = read_str_from_toml(&toml, "other.webpage", false) {
vcard = vcard.with_url(webpage); vcard = vcard.with_url(webpage);
} else { } else {
debug!("No webpage"); debug!("No webpage");
} }
if let Some(note) = read_str_from_toml(&toml, "other.note") { if let Some(note) = read_str_from_toml(&toml, "other.note", false) {
vcard = vcard.with_note(note); vcard = vcard.with_note(note);
} else { } else {
debug!("No note"); debug!("No note");
@ -448,7 +445,7 @@ fn read_strary_from_toml(toml: &Value, path: &'static str) -> Option<Vec<String>
} }
} }
fn read_str_from_toml(toml: &Value, path: &'static str) -> Option<String> { fn read_str_from_toml(toml: &Value, path: &'static str, must_be_there: bool) -> Option<String> {
let v = toml.read(path) let v = toml.read(path)
.map_warn_err_str(&format!("Failed to read value at '{}'", path)); .map_warn_err_str(&format!("Failed to read value at '{}'", path));
@ -459,7 +456,9 @@ fn read_str_from_toml(toml: &Value, path: &'static str) -> Option<String> {
None None
}, },
Ok(None) => { Ok(None) => {
if must_be_there {
error!("Expected '{}' to be present, but is not.", path); error!("Expected '{}' to be present, but is not.", path);
}
None None
}, },
Err(e) => { Err(e) => {