aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2018-12-26 15:51:08 +0100
committerHarald Eilertsen <haraldei@anduin.net>2018-12-26 15:58:57 +0100
commit81acfc65ededd306a8c2b46d483caee8244c52ca (patch)
treefae2a557e6c76aace05e8f66ca0af5f9f871b59c
parent0e06b4193369dbbc851974e2d22390dea2976f6f (diff)
downloadprimstav-81acfc65ededd306a8c2b46d483caee8244c52ca.tar.gz
primstav-81acfc65ededd306a8c2b46d483caee8244c52ca.tar.bz2
primstav-81acfc65ededd306a8c2b46d483caee8244c52ca.zip
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.
-rw-r--r--Cargo.lock48
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs40
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,10 +41,28 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -55,6 +73,16 @@ 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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -66,6 +94,16 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -76,6 +114,11 @@ dependencies = [
]
[[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"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -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 <https://www.gnu.org/licenses/>.
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<String>,
+ symbols: Vec<String>,
+ links: Vec<BTreeMap<String, String>>,
+}
+
+struct Primstav {
+ marks: BTreeMap<String, Mark>,
+}
- 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::<Vec<_>>();
println!("({})", names.join(", "));
}
- if let Some(syms) = d.get("symbols") {
- let s = syms.as_sequence().unwrap().iter()
- .map(|v| v.as_str().unwrap())
- .collect::<Vec<_>>();
- 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 => ()
}