commit f44c34551a230381922103955e8fca5e3871afe8
parent d809f22b81a6685d489625f71d8245a2034137c3
Author: Shimmy Xu <shimmy.xu@shimmy1996.com>
Date: Sat, 14 Sep 2019 23:36:52 -0400
Move Config to separate module
Diffstat:
A | src/config.rs | | | 43 | +++++++++++++++++++++++++++++++++++++++++++ |
M | src/main.rs | | | 47 | ++++------------------------------------------- |
2 files changed, 47 insertions(+), 43 deletions(-)
diff --git a/src/config.rs b/src/config.rs
@@ -0,0 +1,43 @@
+use std::collections::HashMap;
+use std::path::Path;
+
+#[derive(Debug)]
+pub struct Config {
+ pub nick: String,
+ pub twtfile: String,
+ pub twturl: String,
+ pub pre_tweet_hook: String,
+ pub post_tweet_hook: String,
+ pub following: HashMap<String, String>,
+}
+
+impl Config {
+ pub fn new(config_path: &Path) -> Config {
+ use ini::Ini;
+
+ let config = Ini::load_from_file(config_path).unwrap();
+ let twtxt_config = config.section(Some("twtxt".to_owned())).unwrap();
+
+ let mut following = config
+ .section(Some("following".to_owned()))
+ .unwrap()
+ .to_owned();
+ // Always follow oneself.
+ *following
+ .entry(twtxt_config["nick"].to_owned())
+ .or_default() = twtxt_config["twturl"].to_owned();
+ // Parse hook commands.
+ let pre_tweet_hook = strfmt::strfmt(&twtxt_config["pre_tweet_hook"], twtxt_config).unwrap();
+ let post_tweet_hook =
+ strfmt::strfmt(&twtxt_config["post_tweet_hook"], twtxt_config).unwrap();
+
+ Config {
+ nick: twtxt_config["nick"].to_owned(),
+ twtfile: twtxt_config["twtfile"].to_owned(),
+ twturl: twtxt_config["twturl"].to_owned(),
+ pre_tweet_hook: pre_tweet_hook,
+ post_tweet_hook: post_tweet_hook,
+ following: following,
+ }
+ }
+}
diff --git a/src/main.rs b/src/main.rs
@@ -1,51 +1,12 @@
-use std::collections::HashMap;
+use clap::{crate_version, App, Arg, ArgMatches, SubCommand};
+
use std::fs::OpenOptions;
use std::io::prelude::*;
use std::path::Path;
use std::process::Command;
-use clap::{crate_version, App, Arg, ArgMatches, SubCommand};
-use strfmt::strfmt;
-
-#[derive(Debug)]
-struct Config {
- nick: String,
- twtfile: String,
- twturl: String,
- pre_tweet_hook: String,
- post_tweet_hook: String,
- following: HashMap<String, String>,
-}
-
-impl Config {
- fn new(config_path: &Path) -> Config {
- use ini::Ini;
-
- let config = Ini::load_from_file(config_path).unwrap();
- let twtxt_config = config.section(Some("twtxt".to_owned())).unwrap();
-
- let mut following = config
- .section(Some("following".to_owned()))
- .unwrap()
- .to_owned();
- // Always follow oneself.
- *following
- .entry(twtxt_config["nick"].to_owned())
- .or_default() = twtxt_config["twturl"].to_owned();
- // Parse hook commands.
- let pre_tweet_hook = strfmt(&twtxt_config["pre_tweet_hook"], twtxt_config).unwrap();
- let post_tweet_hook = strfmt(&twtxt_config["post_tweet_hook"], twtxt_config).unwrap();
-
- Config {
- nick: twtxt_config["nick"].to_owned(),
- twtfile: twtxt_config["twtfile"].to_owned(),
- twturl: twtxt_config["twturl"].to_owned(),
- pre_tweet_hook: pre_tweet_hook,
- post_tweet_hook: post_tweet_hook,
- following: following,
- }
- }
-}
+mod config;
+use crate::config::Config;
fn main() {
let command = App::new("twixter")