From 47e0bd90122d55e7bb4cc684e6b9c0c0b63568d9 Mon Sep 17 00:00:00 2001 From: Jacob Kiers Date: Wed, 1 Feb 2023 17:09:10 +0100 Subject: [PATCH] Convert enum arguments to structs Signed-off-by: Jacob Kiers --- src/cli.rs | 48 +++++++++++++++++++++++++++--------------------- src/main.rs | 37 ++++++++++++++++--------------------- 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 8f46eb3..02ae2fa 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -1,6 +1,6 @@ use std::path::PathBuf; -use clap::{Parser, Subcommand}; +use clap::{Args, Parser, Subcommand}; #[derive(Parser)] #[clap(author, version, about, long_about = None)] @@ -9,35 +9,41 @@ pub(crate) struct Cli { pub command: Command, } +#[derive(Args)] +pub(crate) struct ImapSettings { + #[clap(short, long, value_parser)] + pub server: String, + #[clap(long, value_parser, default_value_t = 993)] + pub port: u16, + #[clap(short, long, value_parser)] + pub username: String, + #[clap(short, long, value_parser)] + pub password: String, +} + +#[derive(Args)] +pub(crate) struct FeedBuilderSettings { + /// Host name hosting the feed + pub hostname: String, + /// Feed file + #[clap(value_parser, default_value = "output/feed.xml")] + pub filename: PathBuf, + /// Create an HTML file for each message + #[clap(short, long, value_parser, default_value_t = false)] + pub include_html: bool, +} + #[derive(Subcommand)] pub(crate) enum Command { /// Fetch emails from an IMAP server - FetchFromImap { - #[clap(short, long, value_parser)] - server: String, - #[clap(long, value_parser, default_value_t = 993)] - port: u16, - #[clap(short, long, value_parser)] - username: String, - #[clap(short, long, value_parser)] - password: String, - }, + FetchFromImap(ImapSettings), /// Fetch an email from a .eml file FetchFromFile { #[clap(value_parser)] filename: PathBuf, }, /// Build an ATOM feed containing the full contents of the email - BuildFeed { - /// Host name hosting the feed - hostname: String, - /// Feed file - #[clap(value_parser, default_value = "output/feed.xml")] - filename: PathBuf, - /// Create an HTML file for each message - #[clap(short, long, value_parser, default_value_t = false)] - include_html: bool, - }, + BuildFeed(FeedBuilderSettings), /// Exports the emails as HTML files ExportHtml { /// The directory in which the emails will be stored diff --git a/src/main.rs b/src/main.rs index ba63abc..d227a07 100644 --- a/src/main.rs +++ b/src/main.rs @@ -29,24 +29,22 @@ fn main() -> Result<(), Box> { let data_directory = "data"; let result = match &cli.command { - cli::Command::FetchFromImap { - server, - port, - username, - password, - } => fetch_from_imap( + cli::Command::FetchFromImap(imap_settings) => fetch_from_imap( data_directory, - server.to_owned(), - *port, - username.to_owned(), - password.to_owned(), + imap_settings.server.to_owned(), + imap_settings.port, + imap_settings.username.to_owned(), + imap_settings.password.to_owned(), ), - cli::Command::BuildFeed { - filename, - hostname, - include_html, - } => build_feed(filename, hostname, *include_html), + + cli::Command::BuildFeed(settings) => build_feed( + &settings.filename, + &settings.hostname, + settings.include_html, + ), + cli::Command::Update => command::update::self_update(), + _ => unimplemented!("This method is not yet implemented."), }; @@ -168,12 +166,9 @@ fn fetch_from_imap( ); let path = get_path(&parsed, &msg); - let html_path: PathBuf = [ - Path::new(data_directory), - Path::new(&format!("{path}.eml")), - ] - .iter() - .collect(); + let html_path: PathBuf = [Path::new(data_directory), Path::new(&format!("{path}.eml"))] + .iter() + .collect(); println!("Storing to {}", &html_path.display());