cmd.rs
1 use crate::config::Configuration; 2 use crate::error::JournalError; 3 use crate::journal::Journal; 4 use clap::Parser; 5 use log::debug; 6 use std::path::PathBuf; 7 8 #[derive(Debug, Parser)] 9 pub struct Config {} 10 11 impl Config { 12 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 13 config.dump(); 14 Ok(()) 15 } 16 } 17 18 #[derive(Debug, Parser)] 19 pub struct Init { 20 #[clap(help = "Short name for journal")] 21 journalname: String, 22 23 #[clap(help = "Short description of journal, its title")] 24 description: String, 25 } 26 27 impl Init { 28 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 29 debug!( 30 "init: journalname={:?} description={:?}", 31 self.journalname, self.description 32 ); 33 Journal::init(&config.dirname, &config.entries)?; 34 Ok(()) 35 } 36 } 37 38 #[derive(Debug, Parser)] 39 pub struct IsJournal {} 40 41 impl IsJournal { 42 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 43 if !Journal::is_journal(&config.dirname, &config.entries) { 44 return Err(JournalError::NotAJournal( 45 config.dirname.display().to_string(), 46 )); 47 } 48 Ok(()) 49 } 50 } 51 52 #[derive(Debug, Parser)] 53 pub struct New { 54 #[clap(help = "Title of new draft")] 55 title: String, 56 57 #[clap(long, help = "Add links to topic pages")] 58 topic: Vec<PathBuf>, 59 } 60 61 impl New { 62 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 63 let journal = Journal::new(&config.dirname, &config.entries)?; 64 journal.new_draft(&self.title, &self.topic, &config.editor)?; 65 Ok(()) 66 } 67 } 68 69 #[derive(Debug, Parser)] 70 pub struct List {} 71 72 impl List { 73 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 74 let journal = Journal::new(&config.dirname, &config.entries)?; 75 journal.list_drafts()?; 76 Ok(()) 77 } 78 } 79 80 #[derive(Debug, Parser)] 81 pub struct NewTopic { 82 #[clap(help = "Path to topic page in journal")] 83 path: PathBuf, 84 85 #[clap(help = "Title of topic page")] 86 title: String, 87 } 88 89 impl NewTopic { 90 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 91 let journal = Journal::new(&config.dirname, &config.entries)?; 92 journal.new_topic(&self.path, &self.title, &config.editor)?; 93 Ok(()) 94 } 95 } 96 97 #[derive(Debug, Parser)] 98 pub struct Edit { 99 /// Draft id. 100 draft: String, 101 } 102 103 impl Edit { 104 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 105 let journal = Journal::new(&config.dirname, &config.entries)?; 106 let filename = journal.pick_draft(&self.draft)?; 107 journal.edit_draft(&config.editor, &filename)?; 108 Ok(()) 109 } 110 } 111 112 #[derive(Debug, Parser)] 113 pub struct Remove { 114 /// Draft id. 115 draft: String, 116 } 117 118 impl Remove { 119 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 120 let journal = Journal::new(&config.dirname, &config.entries)?; 121 let filename = journal.pick_draft(&self.draft)?; 122 journal.remove_draft(&filename)?; 123 Ok(()) 124 } 125 } 126 127 #[derive(Debug, Parser)] 128 pub struct Finish { 129 /// Draft id. 130 draft: String, 131 132 /// Set base name of published file. 133 basename: String, 134 } 135 136 impl Finish { 137 pub fn run(&self, config: &Configuration) -> Result<(), JournalError> { 138 let journal = Journal::new(&config.dirname, &config.entries)?; 139 let filename = journal.pick_draft(&self.draft)?; 140 journal.finish_draft(&filename, &self.basename)?; 141 Ok(()) 142 } 143 }