use clap::ValueHint;
use log::LevelFilter;
use std::path::PathBuf;
#[derive(clap::Parser)]
#[command(
version,
about,
long_about = "
This is a local email filtering program that watches for emails in the configured mailboxes and matches
the rules to emails and executes certain actions.
There are two main files you need to pay attention to: the config file and the rules file.
The config file is a simple TOML file which configures some global options that influence the programs
execution. It is located at $XDG_CONFIG_HOME/postar/config.toml by default. For the complete reference
of configuration options, refer to https://github.com/filiptrplan/postar?tab=readme-ov-file#toml-configuration-reference.
The rules file is written using a bespoke DSL and defines the rules to match against and the actions
executed when those rules are matched. It is located at $XDG_CONFIG_HOME/postar/rules.ptar by default.
For a complete reference and a short tutorial see https://github.com/filiptrplan/postar?tab=readme-ov-file#rule-dsl.
"
)]
pub struct Args {
#[arg(short, long, value_hint=ValueHint::FilePath)]
pub config: Option<PathBuf>,
#[arg(short, long, value_hint=ValueHint::FilePath)]
pub rules: Option<PathBuf>,
#[arg(long, value_enum, default_value_t=Log::Info)]
pub log: Log,
#[arg(long, short)]
pub server: Option<String>,
#[arg(long, value_hint=ValueHint::FilePath)]
pub db: Option<PathBuf>,
#[arg(long)]
pub polling_delay: Option<u32>,
#[arg(long, default_value_t = false)]
pub check: bool,
#[arg(long, default_value_t = false)]
pub dry_run_remote: bool,
#[arg(long, value_hint=ValueHint::DirPath)]
pub dry_run_local: Option<PathBuf>,
#[command(subcommand)]
pub subcommands: Option<Subcommands>,
}
#[derive(clap::Subcommand)]
pub enum Subcommands {
#[command(
about = "Outputs shell completions to stdout.",
long_about = "
This command outputs shell completions to stdout. It can be used either in the respective
shell rc files at startup or just as a one-time command.
Here is an example of how it would be used for the zsh shell:
eval \"$(postar completions zsh)\" &> /dev/null
"
)]
Completions { shell: Shell },
#[command(
about = "Initializes the configuration files",
long_about = "
This command launches an interactive wizard that initializes the two main configuration files.
It takes you through configuring a mailbox, global options and asks you whether you want to include
the example rules file.
It is recommended to use this command to get up and started quickly, though you will have to write
the rules themselves. Refer to the man page for postar for more information about the configuration
files."
)]
Init(InitArgs),
#[command(
about = "Lists all the folders for a specific mailbox",
long_about = "
This command lists all the folder for a specific mailbox. Useful for specifying destination folders in your rules file.
"
)]
ListFolders(ListFoldersArgs),
}
#[derive(clap::Args)]
pub struct ListFoldersArgs {
pub server: Option<String>,
pub config: Option<PathBuf>,
}
#[derive(clap::Args)]
pub struct InitArgs {
#[arg(long, value_hint=ValueHint::FilePath)]
pub custom_path: Option<PathBuf>,
#[arg(long, default_value_t = true)]
pub write_example_rules: bool,
}
#[derive(clap::ValueEnum, Clone, Copy)]
pub enum Shell {
Zsh,
Fish,
Bash,
}
#[derive(clap::ValueEnum, Clone, Copy)]
pub enum Log {
Off,
Error,
Warn,
Info,
Debug,
Trace,
}
impl From<Log> for LevelFilter {
fn from(value: Log) -> Self {
match value {
Log::Off => LevelFilter::Off,
Log::Error => LevelFilter::Error,
Log::Warn => LevelFilter::Warn,
Log::Info => LevelFilter::Info,
Log::Debug => LevelFilter::Debug,
Log::Trace => LevelFilter::Trace,
}
}
}