♻️ refactor: config to its own module
parent
fc61de612a
commit
0a73740b16
|
@ -0,0 +1,46 @@
|
|||
use std::{
|
||||
str::FromStr,
|
||||
};
|
||||
|
||||
type Result <T> = std::result::Result <T, crate::error::Error>;
|
||||
|
||||
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 <I: Iterator <Item=String>> (mut args: I) -> Result <Self> {
|
||||
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 <u64>"))?;
|
||||
that.interval_secs = val;
|
||||
}
|
||||
else if arg == "--prompt" {
|
||||
let val = args.next ().ok_or (ParamNeedsArg ("--prompt"))?;
|
||||
that.prompt = val;
|
||||
}
|
||||
}
|
||||
|
||||
Ok (that)
|
||||
}
|
||||
}
|
|
@ -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),
|
||||
}
|
56
src/main.rs
56
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 <u64>"))?;
|
||||
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::<Message> ();
|
||||
|
||||
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue