From 00c2fd9057d9e0b40901d671d517f190541a15f8 Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 20 Oct 2020 13:43:20 -0300 Subject: [PATCH] "Database" skeleton --- Cargo.lock | 64 +++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 3 +++ src/database.rs | 54 +++++++++++++++++++++++++++++++++++++++++ src/main.rs | 10 +++++++- 4 files changed, 130 insertions(+), 1 deletion(-) create mode 100644 src/database.rs diff --git a/Cargo.lock b/Cargo.lock index 6cd4ef7..73da412 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -61,6 +61,44 @@ name = "nrp" version = "0.1.0" dependencies = [ "clap", + "serde", + "serde_derive", + "toml", +] + +[[package]] +name = "proc-macro2" +version = "1.0.24" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +dependencies = [ + "unicode-xid", +] + +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b88fa983de7720629c9387e9f517353ed404164b1e482c970a90c1a4aaf7dc1a" + +[[package]] +name = "serde_derive" +version = "1.0.117" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbd1ae72adb44aab48f325a02444a5fc079349a8d804c1fc922aed3f7454c74e" +dependencies = [ + "proc-macro2", + "quote", + "syn", ] [[package]] @@ -69,6 +107,17 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "syn" +version = "1.0.45" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ea9c5432ff16d6152371f808fb5a871cd67368171b09bb21b43df8e4a47a3556" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -78,12 +127,27 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "toml" +version = "0.5.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645" +dependencies = [ + "serde", +] + [[package]] name = "unicode-width" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" +[[package]] +name = "unicode-xid" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564" + [[package]] name = "vec_map" version = "0.8.1" diff --git a/Cargo.toml b/Cargo.toml index 464f0a2..f3f74dd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,6 @@ edition = "2018" [dependencies] clap = "2.33" +toml = "0.5" +serde = "*" +serde_derive = "*" diff --git a/src/database.rs b/src/database.rs new file mode 100644 index 0000000..92a5a3a --- /dev/null +++ b/src/database.rs @@ -0,0 +1,54 @@ +use std::collections::BTreeMap; +use std::collections::LinkedList; +use std::fs::File; +use std::io::Read; + +use serde_derive::Deserialize; +use serde_derive::Serialize; +use toml; + +type WordStorage = BTreeMap>; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Database { + adjectives: WordStorage, + metals: WordStorage, +} + +pub enum DatabaseError { + InvalidFormat, +} + +impl std::convert::From for DatabaseError { + fn from(_error: std::io::Error) -> DatabaseError { + DatabaseError::InvalidFormat + } +} + +impl std::convert::From for DatabaseError { + fn from(_error: toml::de::Error) -> DatabaseError { + DatabaseError::InvalidFormat + } +} + +impl Database { + /// Create an empty database + fn empty() -> Self { + Database { + adjectives: WordStorage::new(), + metals: WordStorage::new(), + } + } + + /// Load the database + pub fn load() -> Result { + if let Ok(mut fp) = File::open("database.toml") { + let mut content = String::new(); + fp.read_to_string(&mut content)?; + let data = toml::from_str(&content)?; + Ok(data) + } else { + Ok(Database::empty()) + } + } +} diff --git a/src/main.rs b/src/main.rs index d8f54f3..2d18b46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,10 +18,18 @@ mod actions; mod args; +mod database; fn main() { match args::parse() { - Ok(x) => println!("Ok: {:?}", x), + Ok(x) => { + println!("Ok: {:?}", x); + if let Ok(_db) = database::Database::load() { + println!("Has db"); + } else { + println!("Do not has db"); + } + } Err(x) => println!("Error {:?}", x), } }