diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..bc647dc --- /dev/null +++ b/src/config.rs @@ -0,0 +1,46 @@ +use std::{ + str::FromStr, +}; + +type Result = std::result::Result ; + +pub struct Config { + pub interval_secs: u64, + pub prompt: String, +} + +impl Default for Config { + fn default () -> Self { + Self { + interval_secs: 2225, + prompt: "Write a journal entry, then hit Tab, Enter to submit it.".into (), + } + } +} + +impl Config { + pub fn from_args > (mut args: I) -> Result { + use crate::error::Error::{ + CannotParseArg, + ParamNeedsArg, + }; + + let mut that = Self::default (); + + // Finally found the difference: https://stackoverflow.com/questions/1788923/parameter-vs-argument + + while let Some (arg) = args.next () { + if arg == "--interval-secs" { + let val = args.next ().ok_or (ParamNeedsArg ("--interval-secs"))?; + let val = u64::from_str (&val).map_err (|_| CannotParseArg ("--interval-secs "))?; + that.interval_secs = val; + } + else if arg == "--prompt" { + let val = args.next ().ok_or (ParamNeedsArg ("--prompt"))?; + that.prompt = val; + } + } + + Ok (that) + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..9cfbf92 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,17 @@ +use fltk::prelude::*; + +#[derive (thiserror::Error, Debug)] +pub enum Error { + #[error ("46MVLSEL Cannot parse argument: {0}")] + CannotParseArg (&'static str), + #[error ("4JZ5B2FN Editor has no buffer, this should be impossible")] + EditorHasNoBuffer, + #[error ("OKE7Z5O6 FLTK: {0}")] + Fltk (#[from] FltkError), + #[error ("4BQPBIAJ IO")] + Io (#[from] std::io::Error), + #[error ("KDP4DNOP JSON serialization failed")] + JsonSerialization (#[from] serde_json::Error), + #[error ("3MYHBQWV Parameter {0} needs an argument")] + ParamNeedsArg (&'static str), +} diff --git a/src/main.rs b/src/main.rs index 2265dbe..5fc7228 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,5 @@ use std::{ - str::FromStr, + env, }; use chrono::{ @@ -25,25 +25,15 @@ use tokio::{ }, }; +mod config; +mod error; + +use config::Config; +use error::Error; + #[tokio::main] async fn main () -> Result <(), Error> { - let mut config = Config::default (); - - let mut args = std::env::args (); - - // Finally found the difference: https://stackoverflow.com/questions/1788923/parameter-vs-argument - - while let Some (arg) = args.next () { - if arg == "--interval-secs" { - let val = args.next ().ok_or (Error::ParamNeedsArg ("--interval-secs"))?; - let val = u64::from_str (&val).map_err (|_| Error::CannotParseArg ("--interval-secs "))?; - config.interval_secs = val; - } - else if arg == "--prompt" { - let val = args.next ().ok_or (Error::ParamNeedsArg ("--prompt"))?; - config.prompt = val; - } - } + let config = Config::from_args (env::args ())?; let (fltk_tx, fltk_rx) = app::channel:: (); @@ -85,42 +75,12 @@ async fn main () -> Result <(), Error> { Ok (()) } -struct Config { - interval_secs: u64, - prompt: String, -} - -impl Default for Config { - fn default () -> Self { - Self { - interval_secs: 2225, - prompt: "Write a journal entry, then hit Tab, Enter to submit it.".into (), - } - } -} - #[derive (Clone, Copy)] enum Message { PopUp, Submit, } -#[derive (thiserror::Error, Debug)] -enum Error { - #[error ("46MVLSEL Cannot parse argument: {0}")] - CannotParseArg (&'static str), - #[error ("4JZ5B2FN Editor has no buffer, this should be impossible")] - EditorHasNoBuffer, - #[error ("OKE7Z5O6 FLTK: {0}")] - Fltk (#[from] FltkError), - #[error ("4BQPBIAJ IO")] - Io (#[from] std::io::Error), - #[error ("KDP4DNOP JSON serialization failed")] - JsonSerialization (#[from] serde_json::Error), - #[error ("3MYHBQWV Parameter {0} needs an argument")] - ParamNeedsArg (&'static str), -} - #[derive (serde::Serialize)] struct JournalLine { text: String,