Convert enum arguments to structs

Signed-off-by: Jacob Kiers <jacob@jacobkiers.net>
This commit is contained in:
Jacob Kiers 2023-02-01 17:09:10 +01:00 committed by Jacob Kiers
parent 0569591aa5
commit 47e0bd9012
2 changed files with 43 additions and 42 deletions

View File

@ -1,6 +1,6 @@
use std::path::PathBuf; use std::path::PathBuf;
use clap::{Parser, Subcommand}; use clap::{Args, Parser, Subcommand};
#[derive(Parser)] #[derive(Parser)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
@ -9,35 +9,41 @@ pub(crate) struct Cli {
pub command: Command, 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)] #[derive(Subcommand)]
pub(crate) enum Command { pub(crate) enum Command {
/// Fetch emails from an IMAP server /// Fetch emails from an IMAP server
FetchFromImap { FetchFromImap(ImapSettings),
#[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,
},
/// Fetch an email from a .eml file /// Fetch an email from a .eml file
FetchFromFile { FetchFromFile {
#[clap(value_parser)] #[clap(value_parser)]
filename: PathBuf, filename: PathBuf,
}, },
/// Build an ATOM feed containing the full contents of the email /// Build an ATOM feed containing the full contents of the email
BuildFeed { BuildFeed(FeedBuilderSettings),
/// 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,
},
/// Exports the emails as HTML files /// Exports the emails as HTML files
ExportHtml { ExportHtml {
/// The directory in which the emails will be stored /// The directory in which the emails will be stored

View File

@ -29,24 +29,22 @@ fn main() -> Result<(), Box<dyn Error>> {
let data_directory = "data"; let data_directory = "data";
let result = match &cli.command { let result = match &cli.command {
cli::Command::FetchFromImap { cli::Command::FetchFromImap(imap_settings) => fetch_from_imap(
server,
port,
username,
password,
} => fetch_from_imap(
data_directory, data_directory,
server.to_owned(), imap_settings.server.to_owned(),
*port, imap_settings.port,
username.to_owned(), imap_settings.username.to_owned(),
password.to_owned(), imap_settings.password.to_owned(),
), ),
cli::Command::BuildFeed {
filename, cli::Command::BuildFeed(settings) => build_feed(
hostname, &settings.filename,
include_html, &settings.hostname,
} => build_feed(filename, hostname, *include_html), settings.include_html,
),
cli::Command::Update => command::update::self_update(), cli::Command::Update => command::update::self_update(),
_ => unimplemented!("This method is not yet implemented."), _ => unimplemented!("This method is not yet implemented."),
}; };
@ -168,12 +166,9 @@ fn fetch_from_imap(
); );
let path = get_path(&parsed, &msg); let path = get_path(&parsed, &msg);
let html_path: PathBuf = [ let html_path: PathBuf = [Path::new(data_directory), Path::new(&format!("{path}.eml"))]
Path::new(data_directory), .iter()
Path::new(&format!("{path}.eml")), .collect();
]
.iter()
.collect();
println!("Storing to {}", &html_path.display()); println!("Storing to {}", &html_path.display());