From 81acfc65ededd306a8c2b46d483caee8244c52ca Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Wed, 26 Dec 2018 15:51:08 +0100 Subject: Use serde_derive to deserialize primstav data into a struct. This makes it much easier to work with the data. Adds some restrictions on the yaml data though, all elements must be present, even if empty. --- Cargo.lock | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 ++ src/main.rs | 40 ++++++++++++++++++++++++++-------------- 3 files changed, 76 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index de957ac..6849584 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -41,9 +41,27 @@ name = "primstav" version = "0.1.0" dependencies = [ "chrono 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)", "serde_yaml 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "proc-macro2" +version = "0.4.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "0.6.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "redox_syscall" version = "0.1.44" @@ -54,6 +72,16 @@ name = "serde" version = "1.0.82" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "serde_derive" +version = "1.0.82" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.15.23 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "serde_yaml" version = "0.8.8" @@ -65,6 +93,16 @@ dependencies = [ "yaml-rust 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "syn" +version = "0.15.23" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "time" version = "0.1.41" @@ -75,6 +113,11 @@ dependencies = [ "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "unicode-xid" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "winapi" version = "0.3.6" @@ -109,10 +152,15 @@ dependencies = [ "checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" +"checksum proc-macro2 0.4.24 (registry+https://github.com/rust-lang/crates.io-index)" = "77619697826f31a02ae974457af0b29b723e5619e113e9397b8b82c6bd253f09" +"checksum quote 0.6.10 (registry+https://github.com/rust-lang/crates.io-index)" = "53fa22a1994bd0f9372d7a816207d8a2677ad0325b073f5c5332760f0fb62b5c" "checksum redox_syscall 0.1.44 (registry+https://github.com/rust-lang/crates.io-index)" = "a84bcd297b87a545980a2d25a0beb72a1f490c31f0a9fde52fca35bfbb1ceb70" "checksum serde 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)" = "6fa52f19aee12441d5ad11c9a00459122bd8f98707cadf9778c540674f1935b6" +"checksum serde_derive 1.0.82 (registry+https://github.com/rust-lang/crates.io-index)" = "96a7f9496ac65a2db5929afa087b54f8fc5008dcfbe48a8874ed20049b0d6154" "checksum serde_yaml 0.8.8 (registry+https://github.com/rust-lang/crates.io-index)" = "0887a8e097a69559b56aa2526bf7aff7c3048cf627dff781f0b56a6001534593" +"checksum syn 0.15.23 (registry+https://github.com/rust-lang/crates.io-index)" = "9545a6a093a3f0bd59adb472700acc08cad3776f860f16a897dfce8c88721cbc" "checksum time 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "847da467bf0db05882a9e2375934a8a55cffdc9db0d128af1518200260ba1f6c" +"checksum unicode-xid 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)" = "fc72304796d0818e357ead4e000d19c9c174ab23dc11093ac919054d20a6a7fc" "checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" "checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" "checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 81c7ed8..958872d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,4 +6,6 @@ edition = "2018" [dependencies] chrono = "0.4.6" +serde = "*" +serde_derive = "*" serde_yaml = "0.8.8" diff --git a/src/main.rs b/src/main.rs index 60d04bc..f6ea539 100644 --- a/src/main.rs +++ b/src/main.rs @@ -15,11 +15,12 @@ // along with this program. If not, see . use chrono; +use serde_derive::Deserialize; use serde_yaml; +use std::collections::BTreeMap; use std::io::BufReader; use std::iter::Iterator; use std::fs::File; -use std::str::FromStr; use std::string::ToString; mod locale; @@ -41,28 +42,39 @@ fn test_capitalize() { assert_eq!("", capitalize("")); } -fn print_entry(d: &serde_yaml::Value) { - let name = d.get("name").unwrap(); - println!("{}", capitalize(name.as_str().unwrap())); +#[allow(dead_code)] +#[derive(Deserialize)] +struct Mark { + name: String, + alternative_names: Vec, + symbols: Vec, + links: Vec>, +} + +struct Primstav { + marks: BTreeMap, +} - if let Some(alt_names) = d.get("alternative_names") { - let names = alt_names.as_sequence().unwrap().iter() - .map(|v| capitalize(v.as_str().unwrap())) +fn print_entry(m: &Mark) { + println!("{}", capitalize(&m.name)); + + if !m.alternative_names.is_empty() { + let names = m.alternative_names.iter() + .map(|v| capitalize(&v)) .collect::>(); println!("({})", names.join(", ")); } - if let Some(syms) = d.get("symbols") { - let s = syms.as_sequence().unwrap().iter() - .map(|v| v.as_str().unwrap()) - .collect::>(); - println!("Symboler: {}", s.join(", ")); + if !m.symbols.is_empty() { + println!("Symboler: {}", m.symbols.join(", ")); } } fn main() { let f = BufReader::new(File::open("data/primstav.yml").unwrap()); - let data: serde_yaml::Value = serde_yaml::from_reader(f).unwrap(); + let p = Primstav { + marks: serde_yaml::from_reader(f).unwrap(), + }; let mut args = std::env::args().skip(1); let local_date = match args.next() { @@ -74,7 +86,7 @@ fn main() { println!("{}", capitalize(&locale.format_date(&local_date))); let key = local_date.format("%d%m").to_string(); - match data.get(serde_yaml::Value::Number(u32::from_str(&key).unwrap().into())) { + match p.marks.get(&key) { Some(d) => print_entry(&d), None => () } -- cgit v1.2.3