diff options
author | Lars Wirzenius <liw@liw.fi> | 2021-04-21 07:35:59 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2021-04-21 07:50:35 +0300 |
commit | 280d07974831ae52d3810d42c23771adf5a1e7d0 (patch) | |
tree | 500764237cc3afe62de572d6455d4cdd68130039 /src/bin/jt2.rs | |
parent | 438c1afb936f0e1e646ff41157fd0494a4312b9e (diff) | |
download | jt2-280d07974831ae52d3810d42c23771adf5a1e7d0.tar.gz |
refactor: move subcommand implementations into src/cmd.rs
The command line interface definition (for structopt) and
implementation are now both in src/cmd.rs. This seems tidier than
having them split between src/opt.rs and src/bin/jt2.rs.
Diffstat (limited to 'src/bin/jt2.rs')
-rw-r--r-- | src/bin/jt2.rs | 63 |
1 files changed, 7 insertions, 56 deletions
diff --git a/src/bin/jt2.rs b/src/bin/jt2.rs index 677bb35..8ef0728 100644 --- a/src/bin/jt2.rs +++ b/src/bin/jt2.rs @@ -1,9 +1,6 @@ use jt2::config::Configuration; -use jt2::error::JournalError; -use jt2::journal::Journal; use jt2::opt::{Opt, SubCommand}; -use std::path::{Path, PathBuf}; use structopt::StructOpt; fn main() -> anyhow::Result<()> { @@ -11,59 +8,13 @@ fn main() -> anyhow::Result<()> { let opt = Opt::from_args(); let config = Configuration::read(&opt)?; match opt.cmd { - SubCommand::Config => config.dump(), - SubCommand::Init { - journalname, - description, - } => init(&config.dirname, &journalname, &description, &config)?, - SubCommand::IsJournal => is_journal(&config)?, - SubCommand::New { title, topic } => new_draft(&title, &topic, &config)?, - SubCommand::NewTopic { path, title } => new_topic(&path, &title, &config)?, - SubCommand::Edit { draft } => edit_draft(&draft, &config)?, - SubCommand::Finish { draft, basename } => finish_draft(&draft, &basename, &config)?, + SubCommand::Config(x) => x.run(&config)?, + SubCommand::Init(x) => x.run(&config)?, + SubCommand::IsJournal(x) => x.run(&config)?, + SubCommand::New(x) => x.run(&config)?, + SubCommand::NewTopic(x) => x.run(&config)?, + SubCommand::Edit(x) => x.run(&config)?, + SubCommand::Finish(x) => x.run(&config)?, } Ok(()) } - -fn init( - dirname: &Path, - _journalname: &str, - _description: &str, - config: &Configuration, -) -> anyhow::Result<()> { - Journal::init(dirname, &config.entries)?; - Ok(()) -} - -fn is_journal(config: &Configuration) -> anyhow::Result<()> { - if !Journal::is_journal(&config.dirname, &config.entries) { - return Err(JournalError::NotAJournal(config.dirname.display().to_string()).into()); - } - Ok(()) -} - -fn new_draft(title: &str, topic: &Option<PathBuf>, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - journal.new_draft(title, topic, &config.editor)?; - Ok(()) -} - -fn new_topic(path: &Path, title: &str, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - journal.new_topic(path, title, &config.editor)?; - Ok(()) -} - -fn edit_draft(draft: &str, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - let filename = journal.pick_draft(draft)?; - journal.edit_draft(&config.editor, &filename)?; - Ok(()) -} - -fn finish_draft(draft: &str, basename: &str, config: &Configuration) -> anyhow::Result<()> { - let journal = Journal::new(&config.dirname, &config.entries)?; - let filename = journal.pick_draft(draft)?; - journal.finish_draft(&filename, basename)?; - Ok(()) -} |