Browse Source

"Database" skeleton

master
Julio Biason 4 years ago
parent
commit
00c2fd9057
  1. 64
      Cargo.lock
  2. 3
      Cargo.toml
  3. 54
      src/database.rs
  4. 10
      src/main.rs

64
Cargo.lock generated

@ -61,6 +61,44 @@ name = "nrp"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"clap", "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]] [[package]]
@ -69,6 +107,17 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" 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]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.11.0" version = "0.11.0"
@ -78,12 +127,27 @@ dependencies = [
"unicode-width", "unicode-width",
] ]
[[package]]
name = "toml"
version = "0.5.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "75cf45bb0bef80604d001caaec0d09da99611b3c0fd39d3080468875cdb65645"
dependencies = [
"serde",
]
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.7" version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479"
[[package]]
name = "unicode-xid"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f7fe0bb3479651439c9112f72b6c505038574c9fbb575ed1bf3b797fa39dd564"
[[package]] [[package]]
name = "vec_map" name = "vec_map"
version = "0.8.1" version = "0.8.1"

3
Cargo.toml

@ -7,3 +7,6 @@ edition = "2018"
[dependencies] [dependencies]
clap = "2.33" clap = "2.33"
toml = "0.5"
serde = "*"
serde_derive = "*"

54
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<char, LinkedList<String>>;
#[derive(Debug, Serialize, Deserialize)]
pub struct Database {
adjectives: WordStorage,
metals: WordStorage,
}
pub enum DatabaseError {
InvalidFormat,
}
impl std::convert::From<std::io::Error> for DatabaseError {
fn from(_error: std::io::Error) -> DatabaseError {
DatabaseError::InvalidFormat
}
}
impl std::convert::From<toml::de::Error> 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<Self, DatabaseError> {
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())
}
}
}

10
src/main.rs

@ -18,10 +18,18 @@
mod actions; mod actions;
mod args; mod args;
mod database;
fn main() { fn main() {
match args::parse() { 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), Err(x) => println!("Error {:?}", x),
} }
} }

Loading…
Cancel
Save