use crate::geolocate_lib::{
geolocation::Provider,
traits::{GeolocationInput, GeolocationProvider, MutualExclusivity},
};
use clap::{Args, Parser, Subcommand};
use std::{net::IpAddr, path::PathBuf};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
pub struct CommandLineArguments {
#[command(subcommand)]
pub command: Command,
}
#[derive(Debug, Subcommand)]
pub enum Command {
#[command(arg_required_else_help = true)]
Ip2location(Ip2LocationArguments),
#[command(arg_required_else_help = true)]
Ipgeolocation(IpGeolocationArguments),
#[command(arg_required_else_help = true)]
Config(ConfigArguments),
#[command(subcommand)]
Completions(ShellCompletions),
}
#[derive(Debug, Args)]
pub struct Ip2LocationArguments {
#[arg(short, long, num_args = 1.., value_delimiter = ' ')]
pub addrs: Option<Vec<IpAddr>>,
#[arg(short, long)]
pub file: Option<PathBuf>,
}
pub enum ExclusiveGeolocationArgument {
IpAddresses,
File,
}
impl MutualExclusivity for Ip2LocationArguments {
type ExclusiveValue = ExclusiveGeolocationArgument;
fn check_exclusivity(&self) -> anyhow::Result<Self::ExclusiveValue> {
match (self.addrs.as_deref(), self.file.as_deref()) {
(Some(_), None) => Ok(ExclusiveGeolocationArgument::IpAddresses),
(None, Some(_)) => Ok(ExclusiveGeolocationArgument::File),
(Some(_), Some(_)) => anyhow::bail!(
"--file and --addrs arguments are mutually exclusive"
),
(None, None) => {
anyhow::bail!(
"Either --file or --addrs has to be passed, but not both"
)
}
}
}
}
impl GeolocationInput for Ip2LocationArguments {
fn addrs(&mut self) -> Option<Vec<IpAddr>> {
self.addrs.take()
}
fn file(&mut self) -> Option<PathBuf> {
self.file.take()
}
}
impl GeolocationProvider for Ip2LocationArguments {
fn provider(&self) -> Provider {
Provider::Ip2Location
}
}
#[derive(Debug, Args)]
pub struct IpGeolocationArguments {
#[arg(short, long, num_args = 1.., value_delimiter = ' ')]
pub addrs: Option<Vec<IpAddr>>,
#[arg(short, long)]
pub file: Option<PathBuf>,
}
impl MutualExclusivity for IpGeolocationArguments {
type ExclusiveValue = ExclusiveGeolocationArgument;
fn check_exclusivity(&self) -> anyhow::Result<Self::ExclusiveValue> {
match (self.addrs.as_deref(), self.file.as_deref()) {
(Some(_), None) => Ok(ExclusiveGeolocationArgument::IpAddresses),
(None, Some(_)) => Ok(ExclusiveGeolocationArgument::File),
(Some(_), Some(_)) => anyhow::bail!(
"--file and --addrs arguments are mutually exclusive"
),
(None, None) => {
anyhow::bail!(
"Either --file or --addrs has to be passed, but not both"
)
}
}
}
}
impl GeolocationInput for IpGeolocationArguments {
fn addrs(&mut self) -> Option<Vec<IpAddr>> {
self.addrs.take()
}
fn file(&mut self) -> Option<PathBuf> {
self.file.take()
}
}
impl GeolocationProvider for IpGeolocationArguments {
fn provider(&self) -> Provider {
Provider::IpGeolocation
}
}
pub enum ExclusiveConfigArgument {
Show,
Edit,
}
#[derive(Debug, Args)]
pub struct ConfigArguments {
#[arg(short, long)]
show: bool,
#[arg(short, long)]
edit: bool,
#[arg(long)]
print_path: bool,
}
impl ConfigArguments {
pub fn print_path(&self) -> bool {
self.print_path
}
}
impl MutualExclusivity for ConfigArguments {
type ExclusiveValue = ExclusiveConfigArgument;
fn check_exclusivity(&self) -> anyhow::Result<Self::ExclusiveValue> {
match (self.show, self.edit) {
(false, true) => Ok(ExclusiveConfigArgument::Edit),
(true, false) => Ok(ExclusiveConfigArgument::Show),
(true, true) => anyhow::bail!(
"Arguments --edit and --show are mutually exclusive"
),
(false, false) => {
anyhow::bail!("Either --edit or --show has to be provided")
}
}
}
}
#[derive(Debug, Subcommand)]
pub enum ShellCompletions {
Bash(OutDir),
Zsh(OutDir),
Fish(OutDir),
PowerShell(OutDir),
Elvish(OutDir),
}
#[derive(Debug, Args)]
pub struct OutDir {
pub path: Option<PathBuf>,
}