Sfoglia il codice sorgente

working config decoder

Josh Bicking 4 anni fa
parent
commit
0d1a068474
5 ha cambiato i file con 243 aggiunte e 140 eliminazioni
  1. 113 45
      Cargo.lock
  2. 2 1
      Cargo.toml
  3. 56 42
      src/config.rs
  4. 45 34
      src/main.rs
  5. 27 18
      src/screen.rs

+ 113 - 45
Cargo.lock

@@ -2,90 +2,151 @@
 # It is not intended for manual editing.
 [[package]]
 name = "argparse"
-version = "0.2.1"
+version = "0.2.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "cc"
-version = "1.0.17"
+version = "1.0.45"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "cfg-if"
-version = "0.1.3"
+version = "0.1.10"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "dtoa"
+version = "0.4.4"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "fallout-terminal"
 version = "0.1.0"
 dependencies = [
- "argparse 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
- "pancurses 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pancurses 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
-[[package]]
-name = "gcc"
-version = "0.3.54"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
 [[package]]
 name = "libc"
-version = "0.2.42"
+version = "0.2.62"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "linked-hash-map"
-version = "0.5.1"
+version = "0.5.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "log"
-version = "0.4.2"
+version = "0.4.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
 name = "ncurses"
-version = "5.92.0"
+version = "5.99.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
- "pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pkg-config 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
 name = "pancurses"
-version = "0.16.0"
+version = "0.16.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
- "log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)",
- "ncurses 5.92.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "pdcurses-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
+ "log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)",
+ "ncurses 5.99.0 (registry+https://github.com/rust-lang/crates.io-index)",
+ "pdcurses-sys 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)",
  "winreg 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
 name = "pdcurses-sys"
-version = "0.7.0"
+version = "0.7.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "cc 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)",
- "libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)",
+ "cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)",
+ "libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
 name = "pkg-config"
-version = "0.3.11"
+version = "0.3.16"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+
+[[package]]
+name = "proc-macro2"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "quote"
+version = "1.0.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.101"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_derive"
+version = "1.0.101"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "serde_yaml"
+version = "0.8.11"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)",
+ "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)",
+ "yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "syn"
+version = "1.0.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+dependencies = [
+ "proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
+ "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
+]
+
+[[package]]
+name = "unicode-xid"
+version = "0.2.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 
 [[package]]
 name = "winapi"
-version = "0.3.5"
+version = "0.3.8"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
  "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -107,31 +168,38 @@ name = "winreg"
 version = "0.5.1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)",
+ "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
 name = "yaml-rust"
-version = "0.4.0"
+version = "0.4.3"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 dependencies = [
- "linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
+ "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [metadata]
-"checksum argparse 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37bb99f5e39ee8b23b6e227f5b8f024207e8616f44aa4b8c76ecd828011667ef"
-"checksum cc 1.0.17 (registry+https://github.com/rust-lang/crates.io-index)" = "49ec142f5768efb5b7622aebc3fdbdbb8950a4b9ba996393cb76ef7466e8747d"
-"checksum cfg-if 0.1.3 (registry+https://github.com/rust-lang/crates.io-index)" = "405216fd8fe65f718daa7102ea808a946b6ce40c742998fbfd3463645552de18"
-"checksum gcc 0.3.54 (registry+https://github.com/rust-lang/crates.io-index)" = "5e33ec290da0d127825013597dbdfc28bee4964690c7ce1166cbc2a7bd08b1bb"
-"checksum libc 0.2.42 (registry+https://github.com/rust-lang/crates.io-index)" = "b685088df2b950fccadf07a7187c8ef846a959c142338a48f9dc0b94517eb5f1"
-"checksum linked-hash-map 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "70fb39025bc7cdd76305867c4eccf2f2dcf6e9a57f5b21a93e1c2d86cd03ec9e"
-"checksum log 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "6fddaa003a65722a7fb9e26b0ce95921fe4ba590542ced664d8ce2fa26f9f3ac"
-"checksum ncurses 5.92.0 (registry+https://github.com/rust-lang/crates.io-index)" = "536b7d9e89b5f571a2db383b790524d649cf7a3465d9381fe8159bf8dfd4be7e"
-"checksum pancurses 0.16.0 (registry+https://github.com/rust-lang/crates.io-index)" = "cedc1409ecbb0a789f39e2a90ae052df6670c1e1c17724587e1872e6ec270e56"
-"checksum pdcurses-sys 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "90e12bfe55b7080fdfa0742f7a22ce7d5d1da250ca064ae6b81c843a2084fa2a"
-"checksum pkg-config 0.3.11 (registry+https://github.com/rust-lang/crates.io-index)" = "110d5ee3593dbb73f56294327fe5668bcc997897097cbc76b51e7aed3f52452f"
-"checksum winapi 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "773ef9dcc5f24b7d850d0ff101e542ff24c3b090a9768e03ff889fdef41f00fd"
+"checksum argparse 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3f8ebf5827e4ac4fd5946560e6a99776ea73b596d80898f357007317a7141e47"
+"checksum cc 1.0.45 (registry+https://github.com/rust-lang/crates.io-index)" = "4fc9a35e1f4290eb9e5fc54ba6cf40671ed2a2514c3eeb2b2a908dda2ea5a1be"
+"checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
+"checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e"
+"checksum libc 0.2.62 (registry+https://github.com/rust-lang/crates.io-index)" = "34fcd2c08d2f832f376f4173a231990fa5aef4e99fb569867318a227ef4c06ba"
+"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83"
+"checksum log 0.4.8 (registry+https://github.com/rust-lang/crates.io-index)" = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
+"checksum ncurses 5.99.0 (registry+https://github.com/rust-lang/crates.io-index)" = "15699bee2f37e9f8828c7b35b2bc70d13846db453f2d507713b758fabe536b82"
+"checksum pancurses 0.16.1 (registry+https://github.com/rust-lang/crates.io-index)" = "d3058bc37c433096b2ac7afef1c5cdfae49ede0a4ffec3dfc1df1df0959d0ff0"
+"checksum pdcurses-sys 0.7.1 (registry+https://github.com/rust-lang/crates.io-index)" = "084dd22796ff60f1225d4eb6329f33afaf4c85419d51d440ab6b8c6f4529166b"
+"checksum pkg-config 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)" = "72d5370d90f49f70bd033c3d75e87fc529fbfff9d6f7cccef07d6170079d91ea"
+"checksum proc-macro2 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "90cf5f418035b98e655e9cdb225047638296b862b42411c4e45bb88d700f7fc0"
+"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe"
+"checksum serde 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "9796c9b7ba2ffe7a9ce53c2287dfc48080f4b2b362fcc245a259b3a7201119dd"
+"checksum serde_derive 1.0.101 (registry+https://github.com/rust-lang/crates.io-index)" = "4b133a43a1ecd55d4086bd5b4dc6c1751c68b1bfbeba7a5040442022c7e7c02e"
+"checksum serde_yaml 0.8.11 (registry+https://github.com/rust-lang/crates.io-index)" = "691b17f19fc1ec9d94ec0b5864859290dff279dbd7b03f017afda54eb36c3c35"
+"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf"
+"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c"
+"checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
 "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"
 "checksum winreg 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "a27a759395c1195c4cc5cda607ef6f8f6498f64e78f7900f5de0a127a424704a"
-"checksum yaml-rust 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "57ab38ee1a4a266ed033496cf9af1828d8d6e6c1cfa5f643a2809effcae4d628"
+"checksum yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d"

+ 2 - 1
Cargo.toml

@@ -6,4 +6,5 @@ authors = ["Josh Bicking <[email protected]>"]
 [dependencies]
 pancurses = "0.16.0"
 argparse = "0.2.1"
-yaml-rust = "0.4"
+serde = { version = "1.0", features = ["derive"] }
+serde_yaml = "0.8"

+ 56 - 42
src/config.rs

@@ -1,25 +1,38 @@
-extern crate argparse;
-extern crate yaml_rust;
+extern crate serde;
+extern crate serde_yaml;
 
 use std::fs::{File};
+use std::path::Path;
 use std::io::prelude::*;
 use std::process::exit;
 use std::env;
 
-use argparse::{ArgumentParser, Store};
-use yaml_rust::{YamlLoader, YamlEmitter, Yaml};
+use self::serde::{Deserialize};
 
 mod config {}
 
+#[derive(Debug, PartialEq, Deserialize)]
+pub struct Config {
+    foo: String
+}
+
+const CONFIG_FOLDERS: &'static [&'static str] = &["XDG_CONFIG_HOME", "HOME"];
 
-// TODO
-//   Collect args
-//   Determine the config file dir
-//   Load the config file
-//   Validate
-//   Return the config
+pub fn default_config_file() -> String {
+    // TODO cycle through to find config file
+    for f in CONFIG_FOLDERS {
+        match env::var(f) {
+            Err(_) => continue,
+            Ok(dir) => {
+                let config_file_location = Path::new(&dir).join("/fallout-terminal.yaml");
+                if Path::is_file(&config_file_location) {
+                    return config_file_location.into_os_string().into_string().unwrap()
+                }
+            }
+        }
+    }
 
-fn default_config_location() -> String {
+    // TODO fallback to HOME/.config
     let config_dir = match env::var("XDG_CONFIG_HOME") {
         Ok(dir) => dir,
         // This will usually be $HOME/.config
@@ -39,37 +52,38 @@ fn default_config_location() -> String {
     config_location
 }
 
-fn load_config_file(config_dir: str) ->  {
-    
-}
-
-pub fn config_from_env() -> Config {
-    let mut config_location = default_config_location();
-    let config_help = format!("Config file location, default {}", config_location);
-
-    // Argument handling
-    let mut difficulty = String::from("");
-
-    {
-        let mut ap = ArgumentParser::new();
-        ap.set_description("A Fallout terminal minigame inspired game.");
-        ap.refer(&mut difficulty)
-            .add_option(&["--difficulty"], Store, "The game's difficulty (veryEasy [default], \
-                                                   easy, average, hard, or veryHard)");
+pub fn load_config_file(config_file_path: String) -> Config {
+    let mut config_file = match File::open(config_file_path.as_str()) {
+        Ok(config_file) => config_file,
+        Err(e) => {
+            println!("Could not open {}: {}", config_file_path, e);
+            exit(1);
+        }
+    };
 
-        ap.refer(&mut config_location)
-            .add_option(
-                &["--config"],
-                Store,
-                config_help.as_str()
-            );
-        ap.parse_args_or_exit();
-    }
+    // TODO Use serde instead
+    let mut config_str = String::new();
+    match config_file.read_to_string(&mut config_str) {
+        Ok(_) => {
+            let config: Config = match serde_yaml::from_str(&config_str) {
+                Ok(conf) => conf,
+                Err(e) => {
+                    println!("Could not parse YAML in {}: {}", config_file_path, e);
+                    exit(1);
+                }
+            };
+            return config
+        }
+        Err(e) => {
+            println!("Could not read {}: {}", config_file_path, e);
+            exit(1);
+        }
+    };
 
-    match difficulty.as_str() {
-        "" | "veryEasy" => {
-            println!("veryEasy");
-        },
-        _ => println!("Invalid difficulty, see --help")
-    }
+    // DEBUG: seeing yaml values
+    // let mut out_str = String::new();
+    // let mut emitter = YamlEmitter::new(&mut out_str);
+    // for x in &config {
+    //     println!("{:?}", emitter.dump(x).unwrap());
+    // }
 }

+ 45 - 34
src/main.rs

@@ -1,44 +1,55 @@
+extern crate argparse;
 
 mod config;
 mod screen;
 
+use self::argparse::{ArgumentParser, Store};
+use std::process::{exit};
+
 fn main() {
 
-    // Config file handling
-    let mut config_file = match File::open(config_location.as_str()) {
-        Ok(config_file) => config_file,
-        Err(e) => {
-            println!("Could not open {}: {}", config_location, e);
-            exit(1);
-        }
-    };
-
-    let mut config_str = String::new();
-    let config: Vec<Yaml>;
-    match config_file.read_to_string(&mut config_str) {
-        Ok(_) => match YamlLoader::load_from_str(&config_str) {
-            Ok(conf) => (config = conf),
-            Err(e) => {
-                println!("Could not parse YAML in {}: {}", config_location, e);
-                exit(1);
-            }
-        }
-        Err(e) => {
-            println!("Could not read {}: {}", config_location, e);
-            exit(1);
-        }
-    };
+    // TODO parse args
+    // Pass (optional) config file loc from them
+    // Get config
+    // Overlay other options from config struct
+
+    // Argument handling
+    let mut config_location = config::default_config_file();
+    let config_help = format!("Config file location, default {}", config_location);
+    let mut difficulty = String::from("");
 
-    // DEBUG: seeing yaml values
-    let mut out_str = String::new();
-    let mut emitter = YamlEmitter::new(&mut out_str);
-    for x in &config {
-        println!("{:?}", emitter.dump(x).unwrap());
+    {
+        let mut ap = ArgumentParser::new();
+        ap.set_description("A Fallout terminal minigame inspired game.");
+        ap.refer(&mut difficulty)
+            .add_option(&["--difficulty"], Store, "The game's difficulty (veryEasy [default], \
+                                                   easy, average, hard, or veryHard)");
+
+        ap.refer(&mut config_location)
+            .add_option(
+                &["--config"],
+                Store,
+                config_help.as_str()
+            );
+        ap.parse_args_or_exit();
     }
 
-    let window = screen::init_window();
-    screen::intro(&window);
-    window.refresh();
-    window.getch();
-    screen::end_window();
+    match difficulty.as_str() {
+        "" | "veryEasy" => {
+            println!("veryEasy");
+        },
+        _ => {
+            println!("Invalid difficulty, see --help");
+            exit(1)
+        }
+    }
+    // Config file handling
+    let mut config = config::load_config_file(config_location);
+    println!("{:?}", config);
+
+    let screen = screen::init_screen();
+    screen.intro();
+    screen.w.refresh();
+    screen.w.getch();
+    screen.end_window();
 }

+ 27 - 18
src/screen.rs

@@ -9,37 +9,46 @@ use std::{thread, time};
 // Screen control functionality. Essentially just handling the heavy lifting
 // pancurses involves.
 
+// A Screen object here is just a wrapper around pancurses' Window.
+
 mod screen {}
 
-pub fn init_window() -> Window {
+pub struct Screen {
+    pub w: Window
+}
+
+
+pub fn init_screen() -> Screen {
     let window = initscr();
     noecho();
     window_enable_colors();
-    window
+    Screen{w: window}
 }
 
-pub fn end_window() {
-    endwin();
-}
+impl Screen {
+    pub fn intro (&self) {
+        self.w.clear();
+        thread::sleep(time::Duration::from_millis(250));
+        self.slow_print(0, String::from("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK"));
 
-pub fn intro (w: &Window) {
-    fn slow_print(w: &Window, line: i32, string: String) {
-        w.mv(line, 0);
+        self.w.mv(1, 0);
+        self.w.refresh();
+        thread::sleep(time::Duration::from_millis(30));
+    }
+
+    pub fn end_window(&self) {
+        endwin();
+    }
+
+    fn slow_print(&self, line: i32, string: String) {
+        self.w.mv(line, 0);
         for c in string.chars() {
-            w.addch(c);
-            w.refresh();
+            self.w.addch(c);
+            self.w.refresh();
             // TODO keyboard skip
             thread::sleep(time::Duration::from_millis(20));
         }
     }
-
-    w.clear();
-    thread::sleep(time::Duration::from_millis(250));
-    slow_print(&w, 0, String::from("WELCOME TO ROBCO INDUSTRIES (TM) TERMLINK"));
-
-    w.mv(1, 0);
-    w.refresh();
-    thread::sleep(time::Duration::from_millis(30));
 }
 
 fn window_enable_colors() {