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 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

View File

@ -29,24 +29,22 @@ fn main() -> Result<(), Box<dyn Error>> {
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());