use std::{
convert::Infallible,
fmt::{self, Display},
str::FromStr,
};
use clap::Parser;
use url::Url;
#[derive(Parser, Debug, Clone)]
#[command(
version,
about = "Serve an endless maze of poisoned training data. Fight back against AI web scrapers."
)]
pub struct MiasmaConfig {
#[arg(short = 'p', long, default_value_t = 9999)]
pub port: u16,
#[arg(long, default_value_t = String::from("localhost") )]
pub host: String,
#[arg(short = 'c', long, default_value_t = 500, value_parser = clap::value_parser!(u32).range(1..))]
pub max_in_flight: u32,
#[arg(short = 'l', long, default_value_t = 5)]
pub link_count: u8,
#[arg(long, default_value_t = LinkPrefix(String::from("/")))]
pub link_prefix: LinkPrefix,
#[arg(long, default_value_t = Url::parse("https://rnsaffn.com/poison2/").unwrap())]
pub poison_source: Url,
}
impl MiasmaConfig {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
<MiasmaConfig as Parser>::parse()
}
}
#[derive(Debug, Clone)]
pub struct LinkPrefix(String);
impl Display for LinkPrefix {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.0)
}
}
impl FromStr for LinkPrefix {
type Err = Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut prefix = s.to_owned();
if !prefix.starts_with('/') {
prefix.insert(0, '/');
}
if !prefix.ends_with('/') {
prefix.push('/');
}
Ok(Self(prefix))
}
}