Initial code to read config file; not hooked up to `open_url` yet.
4 files changed, 86 insertions(+), 8 deletions(-)

M Cargo.lock
M Cargo.toml
A => choosier.toml
M src/main.rs
M Cargo.lock +19 -0
@@ 3,3 3,22 @@ 
 [[package]]
 name = "choosier"
 version = "0.1.0"
+dependencies = [
+ "serde",
+ "toml",
+]
+
+[[package]]
+name = "serde"
+version = "1.0.111"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c9124df5b40cbd380080b2cc6ab894c040a3070d995f5c9dc77e18c34a8ae37d"
+
+[[package]]
+name = "toml"
+version = "0.5.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a"
+dependencies = [
+ "serde",
+]

          
M Cargo.toml +4 -1
@@ 4,8 4,11 @@ version = "0.1.0"
 authors = ["Michel Alexandre Salim <michel@michel-slm.name>"]
 edition = "2018"
 license = "MPL-2.0"
-license-file = "LICENSE.txt"
+readme = "README.md"
+repository = "https://hg.sr.ht/~michel-slm/choosier"
 
 # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
 
 [dependencies]
+serde = "1.0.111"
+toml = "0.5.6"

          
A => choosier.toml +4 -0
@@ 0,0 1,4 @@ 
+default = "/usr/bin/firefox"
+overrides = [
+  [["netflix.com"], "/usr/bin/google-chrome"],
+]

          
M src/main.rs +59 -7
@@ 2,18 2,70 @@ 
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
 
+extern crate serde;
+
+use std::collections::HashMap;
+use std::io;
+
+// #[derive(Deserialize)]
+struct Config {
+    default: String,
+    overrides: HashMap<String, Vec<String>>,
+}
+
+fn parse(config_res: io::Result<String>) -> io::Result<Config> {
+    match config_res {
+        Ok(_config_str) => {
+            let m: HashMap<String, Vec<String>> = vec![(
+                "/usr/bin/google-chrome".to_string(),
+                vec!["netflix.com".to_string()],
+            )]
+            .into_iter()
+            .collect();
+            Ok(Config {
+                default: "/usr/bin/firefox".to_string(),
+                overrides: m,
+            })
+        }
+        Err(e) => Err(e),
+    }
+}
+
+fn read_config() -> io::Result<String> {
+    use std::fs::File;
+    use std::io::prelude::*;
+
+    let mut file = File::open("choosier.toml")?;
+    let mut contents = String::new();
+    file.read_to_string(&mut contents)?;
+    println!("{}", contents);
+    Ok(contents)
+}
+
+fn open_url(url: &str, config: Config) {
+    use std::process::Command;
+
+    match Command::new(config.default).arg(url).spawn() {
+        Ok(_) => (),
+        Err(e) => {
+            println!("Failed to launch browser: {}", e);
+        }
+    }
+}
+
 fn main() {
     use std::env;
-    use std::process::Command;
 
     let args: Vec<String> = env::args().collect();
     if args.len() != 2 {
         println!("Usage: {} URL", args[0]);
-    } else {
-        let output = Command::new("gio")
-            .arg("open")
-            .arg(&args[1])
-            .output();
-        assert!(output.is_ok());
+        return;
+    }
+    let url = &args[1];
+    match parse(read_config()) {
+        Ok(config) => open_url(url, config),
+        Err(_) => {
+            println!("Invalid configuration");
+        }
     }
 }