use core::fmt;
use clap::{Parser, Subcommand};
use colored::Colorize;
use url::Url;
#[derive(Parser, Debug)]
#[command(name = "Coma")]
#[command(author = "Noah")]
#[command(version)]
#[command(help_template = "
{name} - {about}
Author: {author}
Version: {version}
{usage-heading} {usage}
{all-args} {tab}")]
pub struct Args {
#[command(subcommand)]
pub cmd: Commands,
#[arg(short, long)]
pub url: String,
#[arg(short, long, default_value_t = 0, allow_negative_numbers = true)]
pub depth: i32,
#[arg(short, long, default_value = "")]
pub bound: String,
#[arg(short, long, default_value_t = 5)]
pub task: u32,
#[arg(short, long, default_value_t = 0)]
pub external: i32,
}
#[derive(Subcommand, Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Commands {
Texts,
Comments,
Links,
Images,
Graph,
}
pub enum ArgsError {
InvalidUrl(String),
}
impl ArgsError {
fn print(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ArgsError::InvalidUrl(url) => write!(f, "{}: {}", "Invalid URL".red(), url),
}
}
}
impl fmt::Display for ArgsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.print(f)
}
}
impl fmt::Debug for ArgsError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.print(f)
}
}
impl std::error::Error for ArgsError {}
pub fn args() -> Result<Args, ArgsError> {
let args = Args::parse();
match Url::parse(&args.url) {
Ok(v) => {
v.domain().ok_or(ArgsError::InvalidUrl(v.to_string()))?;
Ok(args)
}
Err(e) => Err(ArgsError::InvalidUrl(e.to_string())),
}
}