use chrono::Datelike;
use std::io::IsTerminal;
use std::path::Path;
use std::sync::{LazyLock, OnceLock};
#[derive(serde::Deserialize)]
#[serde(deny_unknown_fields, bound(deserialize = "'de: 'static"))]
pub struct Words {
reserved: Vec<&'static str>,
alone: Vec<&'static str>,
pub lists: Vec<&'static str>,
pub aliases: Vec<(&'static str, &'static str)>,
elsewhere: Vec<(&'static str, &'static str)>,
verbs: Vec<&'static str>,
pub keys: Vec<&'static str>,
bools: Vec<&'static str>,
pub commands: Vec<&'static str>,
led: Vec<&'static str>,
operators: Vec<&'static str>,
tlds: Vec<&'static str>,
pub path_lead: Vec<&'static str>,
file_tlds: Vec<&'static str>,
pub secret_names: Vec<&'static str>,
pub media: Vec<&'static str>,
pub hosts: Vec<(&'static str, &'static str)>,
pub stop: Vec<&'static str>,
}
pub static WORDS: LazyLock<Words> = LazyLock::new(|| crate::settings::table("words"));
pub const CARD: &str = "\
fats <url|file.pdf> fetch, outline fats u3 u3#2 u3#2 l4 page section link
fats u3#2 <words> filter inside fats <words> web search → ids site:ex.com since:7d
mods key:value limit:20 match:exact|words country:de js:true wait:30 (then pending) provider:plain|bd|oxy|decodo device:mobile
more up all full json fats = status fats setup = keys fats account = balance j8.u3 = another session's id";
pub const GUIDE: &str = include_str!("data/guide.md");
pub fn to_guide() -> String {
format!("agents: {} guide = the manual (~{} tok)", bin(), ksize(crate::clean::tok(GUIDE)))
}
const HELP_FLAGS: [&str; 4] = ["--help", "-h", "—help", "–help"];
pub const SCREENS: [(&str, &str); 11] = crate::settings::embed!("screens/", ".txt": "home", "ready", "yours",
"help", "help-mcp", "help-setup", "help-search", "tip-page", "tip-search", "setup", "telemetry");
const STYLES: [(&str, u8); 6] = [("bold", 1), ("dim", 2), ("green", 32), ("red", 31), ("cyan", 36), ("/", 0)];
pub const NO_HOLES: [(&str, &str); 0] = [];
pub fn screen(name: &str) -> &'static str {
SCREENS.iter().find(|s| s.0 == name).map_or("", |s| s.1)
}
pub fn draw(text: &str, holes: &[(&str, impl AsRef<str>)]) -> String {
holes.iter().fold(text.to_string(), |t, (name, value)| t.replace(&format!("{{{name}}}"), value.as_ref()))
}
pub fn paint(text: &str, holes: &[(&str, impl AsRef<str>)]) -> String {
let plain = std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty());
#[cfg(test)]
let plain = tests::NO_COLOR.get().unwrap_or(plain);
draw(&draw(text, holes), &STYLES.map(|(k, n)| (k, if plain { String::new() } else { format!("\x1b[{n}m") })))
}
pub fn human(json: bool) -> bool {
let forced = std::env::var("FATS_HUMAN").ok().and_then(|v| crate::settings::boolean(&v));
let agent = json || cfg!(test) || crate::cli::FENCE.get().is_some();
!agent && forced.unwrap_or_else(|| std::io::stdout().is_terminal())
}
pub fn help_screen(argv: &[String]) -> Option<String> {
let first = argv.first().is_some_and(|a| a.eq_ignore_ascii_case("help"));
let topic = argv.get(1).filter(|_| first).map(|t| screen(&format!("help-{}", t.to_lowercase())));
let text = topic.filter(|t| !t.is_empty()).unwrap_or(screen("help"));
(first || argv.iter().any(|a| HELP_FLAGS.contains(&a.as_str()))).then(|| paint(text, &NO_HOLES))
}
pub fn tip(cmd: &Cmd, out: &Out, search: bool) -> Option<String> {
let paid = crate::fetch::keyed().iter().any(|t| t.spec().price > 0.0);
let keyless = !paid && out.rc != 0 && out.parts.is_empty() && out.lines.iter().any(|l| l.contains("! no_key "));
let page = |u: &String| paint(screen("tip-page"), &[("site", crate::fetch::site(u))]);
let tip = if search { Some(paint(screen("tip-search"), &NO_HOLES)) } else { cmd.urls.first().map(page) };
tip.filter(|_| keyless && cmd.urls.len() < 2 && cmd.verb.is_none() && !cmd.flag("dry"))
}
static BIN: OnceLock<String> = OnceLock::new();
pub fn bin() -> &'static str {
BIN.get_or_init(|| {
let p = std::path::PathBuf::from(std::env::args().next().unwrap_or_default());
let name = p.extension().filter(|e| e.eq_ignore_ascii_case("exe")).map_or(p.file_name(), |_| p.file_stem());
name.map(|f| f.to_string_lossy().into_owned()).filter(|s| !s.is_empty()).unwrap_or_else(|| "fats".into())
})
}
pub static FIX: LazyLock<Vec<(&str, &str)>> = LazyLock::new(|| crate::settings::table("errors"));
const RETRY: &str = "{b} {id} fresh:true";
#[derive(Default)]
pub struct At<'a> {
pub id: &'a str,
pub url: &'a str,
pub arg: &'a str,
pub done: &'a [&'a str],
}
pub fn fix(code: &str, at: &At) -> String {
let hit = |k: &str| k.strip_suffix('*').map_or(k == code, |p| code.starts_with(p));
let t = FIX.iter().find(|r| hit(r.0)).map_or(RETRY, |r| r.1);
let f = draw(t, &[("b", bin()), ("id", at.id), ("url", at.url), ("arg", at.arg)]);
let js = f.split(' ').any(|w| w == "js:true");
let js = js.then(|| crate::provider::Tier::ALL.into_iter().find(|t| t.renders())).flatten().map(|t| t.spec().short);
let tier = js.or(f.split(' ').filter_map(|w| w.strip_prefix("provider:")).next_back());
let sent = tier.is_some_and(|t| at.done.contains(&t));
let barred = sent || f.split(' ').any(|w| w.contains(':') && at.done.contains(&w));
if sent
&& (js.is_some() || crate::provider::REFUSED.contains(&code))
&& let Some((_, arrow)) = crate::fetch::left_ask(at.id, at.url, &crate::provider::Tier::ALL, at.done, |_| true)
{
return arrow;
}
if barred {
let open = if at.url.is_empty() { String::new() } else { format!(" · open {} in a browser", at.url) };
return format!("nothing left to try{open}");
}
let toks: Vec<&str> = f.split(' ').filter(|w| !w.is_empty()).collect();
fn key(w: &str) -> Option<&str> {
w.split_once(':').map(|p| p.0).filter(|k| WORDS.keys.contains(k))
}
let later = |i: usize| key(toks[i]).is_some_and(|k| toks[i + 1..].iter().any(|w| key(w) == Some(k)));
let f = toks.iter().enumerate().filter(|&(i, _)| !later(i)).map(|p| *p.1).collect::<Vec<_>>().join(" ");
let (cmd, rest) = f.split_once(", ").map_or((f.as_str(), ""), |(c, _)| (c, &f[c.len()..]));
let quoted = priced(cmd, at.url);
if code == "budget_hit" && quoted == cmd {
return fix("no_key", &At::default());
}
let free = quoted != cmd && crate::provider::REFUSED.contains(&code);
format!("{quoted}{}{rest}", if free { " · last try not charged" } else { "" })
}
pub fn priced(cmd: &str, url: &str) -> String {
let c = crate::fetch::worst(cmd, url).map_or(0.0, |r| r.1);
if c > 0.0 { format!("{cmd} ≤${c:.4}") } else { cmd.into() }
}
#[derive(Default)]
pub struct Flag {
pub code: String,
pub what: String,
pub next: String,
pub row: bool,
}
impl Flag {
pub fn bang(&self, subject: &str) -> String {
format!("{subject} ! {}", self.body()).trim_start().to_string()
}
pub fn body(&self) -> String {
let head = [&self.code, &self.what].iter().filter(|s| !s.is_empty()).map(|s| s.as_str()).collect::<Vec<_>>();
if self.next.is_empty() { head.join(" ") } else { format!("{} → {}", head.join(" "), self.next) }
}
}
pub fn restamp(row: &str, id: &str, url: &str) -> String {
let ((head, f), code) = (row.split_once(" → ").unwrap_or((row, "")), row.split(' ').nth(1).unwrap_or_default());
let t = FIX.iter().find(|r| r.0 == code).map_or("", |r| r.1);
let place =
t.split(' ').position(|w| w == "{arg}").filter(|_| !t.contains("{id}")).and_then(|i| f.split(' ').nth(i));
let arg =
f.split(' ').find_map(|w| w.strip_prefix("cost:").or(w.strip_prefix("zone:"))).or(place).unwrap_or_default();
format!("{head} → {}{}", fix(code, &At { id, url, arg, done: &[] }), f.find(" · ").map_or("", |i| &f[i..]))
}
pub fn bang(subject: &str, code: &str, detail: &str, at: &At) -> String {
Flag { code: code.into(), what: detail.into(), next: fix(code, at), row: true }.bang(subject)
}
pub fn uniq<T, K: PartialEq>(it: impl IntoIterator<Item = T>, key: impl Fn(&T) -> K) -> Vec<T> {
let mut out: Vec<T> = Vec::new();
for x in it {
if !out.iter().any(|o| key(o) == key(&x)) {
out.push(x);
}
}
out
}
pub fn cut(s: &str, n: usize) -> &str {
s.char_indices().nth(n).map_or(s, |(i, _)| &s[..i])
}
pub fn ksize(n: usize) -> String {
match n {
0..1000 => n.to_string(),
1000..1_000_000 => format!("{:.1}k", n as f64 / 1e3),
_ => format!("{:.1}M", n as f64 / 1e6),
}
}
pub fn mib(n: u64) -> String {
if n >= 1 << 20 { format!("{:.0} MiB", n as f64 / (1u64 << 20) as f64) } else { format!("{} KiB", n >> 10) }
}
pub fn short(url: &str, n: usize) -> String {
let s = url.strip_prefix("https://").or_else(|| url.strip_prefix("http://")).unwrap_or(url);
let s = if s != url { s.strip_prefix("www.").unwrap_or(s) } else { s };
if s.chars().count() <= n { s.to_string() } else { format!("{}…", cut(s, n - 1)) }
}
#[derive(Clone, Copy, PartialEq, Debug)]
pub enum Match {
Exact,
Words,
Near,
Topic,
}
const MATCH: [&str; 4] = ["exact", "words", "near", "topic"];
impl Match {
pub fn name(self) -> &'static str {
MATCH[self as usize]
}
}
#[derive(Clone, Copy, Debug, PartialEq, Default)]
pub struct Id {
pub job: Option<usize>,
pub u: usize,
pub sec: Option<usize>,
pub part: usize,
}
impl Id {
fn parse(t: &str) -> Option<Id> {
let (job, rest) = match t.strip_prefix('j').and_then(|r| r.split_once('.')) {
Some((j, r)) => (Some(num(j)?), r),
None => (None, t),
};
let rest = rest.strip_prefix('u')?;
let (u, sec) = rest.split_once('#').map_or((rest, None), |(u, s)| (u, Some(s)));
let mut id = Id { job, u: num(u)?, ..Id::default() };
if let Some(s) = sec {
let d = s.trim_end_matches(|c: char| c.is_ascii_lowercase());
id.sec = Some(num(d)?);
id.part = part_of(&s[d.len()..])?;
}
Some(id)
}
pub fn sec_tail(&self) -> String {
let part = if self.part > 0 { letters(self.part) } else { String::new() };
self.sec.map(|k| format!("#{k}{part}")).unwrap_or_default()
}
pub fn page(&self) -> String {
let job = self.job.map(|j| format!("j{j}.")).unwrap_or_default();
format!("{job}u{}", self.u)
}
}
pub fn letters(part: usize) -> String {
let (mut n, mut out) = (part + 1, Vec::new());
while n > 0 {
n -= 1;
out.push(b'a' + (n % 26) as u8);
n /= 26;
}
out.iter().rev().map(|&b| b as char).collect()
}
fn part_of(s: &str) -> Option<usize> {
Some(s.bytes().try_fold(0usize, |n, b| n.checked_mul(26)?.checked_add((b - b'a') as usize + 1))?.saturating_sub(1))
}
pub fn since_date(v: &str, today: i64) -> Option<String> {
let unit =
v.chars().last().and_then(|u| [('d', 1), ('w', 7), ('m', 30), ('y', 365)].into_iter().find(|x| x.0 == u));
if let Some((_, days)) = unit {
let count = num(&v[..v.len() - 1])?.min(100_000) as i64;
let (year, month, day) = civil(today - count * days);
return Some(format!("{year:04}-{month:02}-{day:02}"));
}
let parts: Vec<&str> = v.split('-').collect();
let ok = |s: &&str, len: usize, hi: usize| s.len() == len && num(s).is_some_and(|n| (1..=hi).contains(&n));
let fine = parts.len() <= 3 && ok(&parts[0], 4, 9999) && parts.get(1).is_none_or(|m| ok(m, 2, 12));
(fine && parts.get(2).is_none_or(|d| ok(d, 2, 31)))
.then(|| format!("{}-{}-{}", parts[0], parts.get(1).unwrap_or(&"01"), parts.get(2).unwrap_or(&"01")))
}
pub fn civil(z: i64) -> (i32, u32, u32) {
let d = chrono::DateTime::from_timestamp(z * 86_400, 0).unwrap_or_default().date_naive();
(d.year(), d.month(), d.day())
}
fn count(s: &str) -> Option<usize> {
let (n, k) = s.strip_suffix(['k', 'K']).map_or((s, 1.0), |n| (n, 1000.0));
let x = n.parse::<f64>().ok().filter(|x| x.is_finite() && *x > 0.0)? * k;
(1.0..1e9).contains(&x).then_some(x as usize)
}
fn secs(s: &str, lo: u64, hi: u64) -> Option<u64> {
s.strip_suffix('s').unwrap_or(s).parse().ok().filter(|n| (lo..=hi).contains(n))
}
fn bytes(s: &str) -> Option<u64> {
let l = s.to_ascii_lowercase();
let n = l.trim_end_matches(['b', 'i']);
let kib = || n.strip_suffix('k').map_or((n, 0), |n| (n, 10));
let (n, shift) = n.strip_suffix('m').map_or_else(kib, |n| (n, 20));
n.parse::<u64>().ok()?.checked_mul(1 << shift)
}
fn num(s: &str) -> Option<usize> {
(!s.is_empty() && s.bytes().all(|b| b.is_ascii_digit())).then(|| s.parse().ok()).flatten()
}
#[derive(Default, Debug)]
pub struct Cmd {
pub ids: Vec<Id>,
pub jobs: Vec<usize>,
pub links: Vec<usize>,
pub media: Vec<usize>,
pub urls: Vec<String>,
pub files: Vec<String>,
pub reserved: Vec<&'static str>,
pub words: Vec<String>,
pub mods: Vec<(String, String)>,
pub sub: Vec<String>,
pub verb: Option<String>,
pub idle: bool,
pub ask: Option<String>,
pub raw: String,
}
impl Cmd {
pub fn has(&self, r: &str) -> bool {
self.reserved.contains(&r)
}
pub fn get(&self, k: &str) -> Option<&str> {
self.mods.iter().rev().find(|(a, _)| a == k).map(|(_, v)| v.as_str())
}
pub fn flag(&self, k: &str) -> bool {
self.get(k) == Some("true")
}
pub fn fresh(&self) -> bool {
self.flag("fresh") || self.flag("js")
}
pub fn count(&self, k: &str) -> Option<usize> {
self.get(k).and_then(count)
}
pub fn canon(&self) -> String {
self.with_mod("", None)
}
pub fn with_mod(&self, k: &str, v: Option<&str>) -> String {
let mut p: Vec<String> = self.verb.iter().cloned().collect();
p.extend(self.ids.iter().map(|i| i.page() + &i.sec_tail()));
p.extend(self.jobs.iter().map(|j| format!("j{j}")));
p.extend(self.links.iter().map(|l| format!("l{l}")));
p.extend(self.media.iter().map(|m| format!("m{m}")));
p.extend(self.urls.iter().cloned());
p.extend(self.files.iter().cloned());
p.extend(self.reserved.iter().map(|r| r.to_string()));
p.extend(self.words.iter().cloned());
p.extend(self.mods.iter().filter(|(a, _)| a != k).map(|(k, v)| format!("{k}:{v}")));
p.extend(v.map(|v| format!("{k}:{v}")));
p.join(" ")
}
}
fn is_url(t: &str) -> bool {
let l = t.to_ascii_lowercase();
if let Some(r) = l.strip_prefix("https://").or_else(|| l.strip_prefix("http://")) {
return !r.is_empty();
}
let host = l.split('/').next().unwrap_or("");
let labels: Vec<&str> = host.split('.').collect();
labels.len() >= 2
&& labels.iter().all(|x| !x.is_empty() && x.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'-'))
&& labels.last().is_some_and(|x| x.len() >= 2 && x.bytes().all(|b| b.is_ascii_alphabetic()))
}
fn bare_tld(low: &str) -> Option<&str> {
low.rsplit_once('.').filter(|_| !low.contains('/')).map(|(_, tld)| tld)
}
pub fn real_host(url: &str) -> bool {
let host = crate::clean::host_of(url);
let tld = host.strip_suffix('.').unwrap_or(&host).rsplit('.').next().unwrap_or_default();
host.parse::<std::net::IpAddr>().is_ok() || (WORDS.tlds.contains(&tld) && askable(&host))
}
pub fn askable(host: &str) -> bool {
let label_ok = |l: &str| (1..=63).contains(&l.len()) && !l.starts_with('-') && !l.ends_with('-');
host.strip_suffix('.').unwrap_or(host).split('.').all(label_ok)
}
fn is_file(t: &str, any_id: bool) -> Result<bool, String> {
if t == "-" || t.starts_with('/') {
return Ok(true);
}
let ext = Path::new(t).extension().map(|e| e.to_string_lossy().to_ascii_lowercase());
let doc = ext.as_ref().is_some_and(|e| crate::fetch::doc_kind(&format!(".{e}")).is_some());
let file = !any_id && !t.contains("://") && (doc || filed(Path::new(t)) || crate::cli::secret(Path::new(t)));
match file_or_site(t, &WORDS.tlds).filter(|_| file && doc) {
Some(q) => Err(q),
None => Ok(file),
}
}
fn file_or_site(t: &str, tlds: &[&str]) -> Option<String> {
let tld = bare_tld(&t.to_lowercase()).filter(|d| tlds.contains(d) && t.matches('.').count() == 1).is_some();
(tld && !filed(Path::new(t))).then(|| format!("? {t}: a missing file or a site · type ./{t} or https://{t}"))
}
fn shaped(t: &str) -> bool {
let (b, low) = (t.as_bytes(), t.to_ascii_lowercase());
let drive = b.len() > 2 && b[0].is_ascii_alphabetic() && b[1] == b':' && b[2] == b'/';
let segments = t.split('/').filter(|s| !s.is_empty()).count();
let deep = if t.starts_with('/') { segments >= 2 } else { segments >= 3 && !is_url(t) && !t.contains(':') };
let name = |r: &str, dot: bool| {
let n = r.split('/').next().unwrap_or_default();
n.starts_with(|c: char| c.is_ascii_alphabetic() || "_{".contains(c)) && !(dot && WORDS.tlds.contains(&n))
};
let home = |s: char| low.strip_prefix(s).is_some_and(|r| r.contains('/') && name(r, false));
let dot = low.strip_prefix('.').is_some_and(|r| name(r, true));
let lead = ["./", "../", "~/", "file:"].iter().any(|p| low.starts_with(p));
drive || deep || home('~') || home('$') || dot || lead || t.contains('\\')
}
pub fn local_path(f: &str) -> std::path::PathBuf {
f.strip_prefix("~/").map_or_else(|| f.into(), |r| dirs::home_dir().unwrap_or_default().join(r))
}
fn filed(p: &Path) -> bool {
crate::cli::outside(p) || p.is_file()
}
fn modifier(k: &str, v: &str) -> Result<(String, String), String> {
let (k, v) = alias(&k.to_ascii_lowercase(), v)?;
if let Some(built) = crate::settings::LIMITS.get(k.as_str()) {
let fit = crate::settings::fits(&k, &v).map(|_| (k.clone(), v.clone()));
return fit.ok_or(format!("? {k}:{v} · a number like {k}:{built}"));
}
if !WORDS.keys.contains(&k.as_str()) {
return Err(format!("? {k}: unknown · keys {}", WORDS.keys.join(" ")));
}
value(&k, v).map(|v| (k, v))
}
fn alias(k: &str, v: &str) -> Result<(String, String), String> {
let (k, v) = match k {
"n" => ("limit", v.to_string()),
"geo" | "loc" | "gl" | "country" => ("country", v.to_ascii_lowercase()),
"budget" => ("cost", v.to_string()),
"device" | "ua" => match v.to_ascii_lowercase().as_str() {
"mobile" | "phone" => ("device", "mobile".into()),
"desktop" | "pc" => ("device", "desktop".into()),
_ => return Err(format!("? device:{v} · use device:mobile|desktop (Oxylabs only)")),
},
"loose" => {
let n: u32 =
v.parse().map_err(|_| format!("? loose:{v} · loose takes 0..100 · or match:exact|words|near|topic"))?;
("match", MATCH[[1, 50, 80].iter().filter(|&&lo| n >= lo).count()].into())
}
_ => (k, v.to_string()),
};
Ok((k.to_string(), v))
}
fn value(k: &str, v: String) -> Result<String, String> {
if WORDS.bools.contains(&k) {
let b = crate::settings::boolean(&v).ok_or_else(|| format!("? {k}:{v} · use {k}:true or {k}:false"))?;
return Ok(b.to_string());
}
let n = match k {
"hedge" => secs(&v, 0, 60).ok_or("hedge:{v} · seconds before the second provider, 0 to 60: hedge:0"),
"wait" => secs(&v, 0, 300).ok_or("wait:{v} · seconds before a fetch answers pending, 0 to 300: wait:30"),
"timeout" => secs(&v, 1, 300).ok_or("timeout:{v} · seconds for bd and oxyjs, 1 to 300: timeout:150"),
"max_bytes" => bytes(&v)
.filter(|n| (1 << 10..=crate::cli::MAX_BYTES_CEIL).contains(n))
.ok_or("max_bytes:{v} · 1k to 64m (KiB, MiB): max_bytes:32m"),
"fetch" => v.parse().ok().filter(|n| *n <= 50).ok_or("fetch:{v} · results to fetch ahead, 0 to 50: fetch:0"),
"limit" | "max" | "from" => count(&v).map(|n| n as u64).ok_or("{k}:{v} · {k} takes a count: {k}:20 · {k}:1k"),
_ => return text_value(k, v),
};
n.map(|n| n.to_string()).map_err(|q| format!("? {}", draw(q, &[("k", k), ("v", &v)])))
}
fn text_value(k: &str, v: String) -> Result<String, String> {
let (ok, why) = match k {
"match" => (MATCH.contains(&v.as_str()), "use match:exact|words|near|topic"),
"country" => {
(v.len() == 2 && v.bytes().all(|b| b.is_ascii_alphabetic()), "two letters: country:de · country:us")
}
"zone" => (
v.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-'),
"a Bright Data zone name: zone:web_unlocker1",
),
"since" => (since_date(&v, 0).is_some(), "an age or a date: since:7d · since:2w · since:2026-01"),
"cost" => {
(v.parse::<f64>().is_ok_and(|x| x.is_finite() && x >= 0.0), "cost takes US dollars per call: cost:0.5")
}
"provider" => {
use crate::provider::Tier;
let t = Tier::parse(&v.to_ascii_lowercase()).filter(|t| !t.renders());
let p = t.map(|t| t.spec().short).or((v.eq_ignore_ascii_case("auto")).then_some("auto"));
let all: Vec<&str> = Tier::ALL.iter().filter(|t| !t.renders()).map(|t| t.spec().short).collect();
return p.map(String::from).ok_or(format!("? provider:{v} · use provider:auto|{}", all.join("|")));
}
"site" => {
let s = v.to_ascii_lowercase();
let s = s.strip_prefix("https://").or(s.strip_prefix("http://")).unwrap_or(&s).trim_end_matches('/');
let host = is_url(s).then(|| s.to_string());
return host.ok_or(format!("? site:{v} · a host: site:example.com · site:docs.rs/serde"));
}
_ => (true, ""),
};
if ok { Ok(v) } else { Err(format!("? {k}:{v} · {why}")) }
}
fn repair(argv: &[String]) -> Vec<String> {
let s = argv.join(" ");
let s = s.trim().trim_matches('`').trim();
let s = s.strip_prefix("fats ").unwrap_or(s);
let mut out: Vec<String> = Vec::new();
let mut it = s.split_whitespace().map(|t| t.trim_matches(|c| matches!(c, '"' | '\'' | '`'))).peekable();
while let Some(t) = it.next() {
if t.is_empty() || t == "--" {
continue;
}
let mut t = t.to_string();
if t.starts_with(['U', 'J']) && t[1..].starts_with(|c: char| c.is_ascii_digit()) {
t = t.to_lowercase();
}
let job =
t.split_once(['/', ':']).filter(|(a, b)| a.strip_prefix('j').and_then(num).is_some() && b.starts_with('u'));
if let Some((a, b)) = job {
t = format!("{a}.{b}");
}
let sec = t.strip_prefix('u').and_then(|r| r.split_once('.')).filter(|(a, b)| num(a).and(num(b)).is_some());
if let Some((a, b)) = sec {
t = format!("u{a}#{b}");
}
if let Some(r) = t.strip_prefix("--").filter(|r| *r != "help" && WORDS.reserved.contains(r)) {
t = r.to_string();
} else if t == "-v" {
t = "verbose:true".into();
} else if t == "-n" && it.peek().is_some_and(|n| num(n).is_some()) {
t = format!("limit:{}", it.next().unwrap_or_default());
} else if let Some((k, v)) = t.split_once('=')
&& !k.is_empty()
&& k.bytes().all(|b| b.is_ascii_lowercase())
&& !v.is_empty()
{
t = format!("{k}:{v}");
}
if t == "l" && it.peek().is_some_and(|n| num(n).is_some()) {
t = format!("l{}", it.next().unwrap_or_default());
}
if let (Some(r), Some(prev)) = (t.strip_prefix('#'), out.last_mut())
&& num(r).is_some()
&& Id::parse(prev).is_some_and(|i| i.sec.is_none())
{
prev.push_str(&t);
continue;
}
out.push(t);
}
undecorate(out)
}
fn undecorate(toks: Vec<String>) -> Vec<String> {
let first = toks.first().map(|t| t.to_ascii_lowercase()).unwrap_or_default();
let job = first.strip_prefix('j').and_then(num).is_some();
let url = ["http://", "https://"].iter().any(|s| first.starts_with(s));
if !(Id::parse(&first).is_some() || job || url || ["setup", "stats", "config"].contains(&first.as_str())) {
return toks;
}
let (mut group, mut dot) = (false, false);
let mut keep = |t: &String| {
let inside = group || t.starts_with('(');
group = inside && !t.ends_with(')');
dot = (dot || t == "·") && key_value(t).is_none();
let time = t.strip_prefix(['~', '≈']).and_then(|r| r.strip_suffix('s')).is_some_and(|n| num(n).is_some());
!(inside || dot || t.starts_with('≤') || time)
};
toks.into_iter().filter(|t| keep(t)).collect()
}
fn key_value(t: &str) -> Option<(&str, &str)> {
let (k, v) = t.split_once(':')?;
let key = !k.is_empty() && k.bytes().all(|b| b.is_ascii_alphabetic() || b == b'_');
let scheme = ["http", "https"].contains(&k.to_ascii_lowercase().as_str());
(key && !scheme && !v.is_empty() && !v.starts_with('/')).then_some((k, v))
}
fn typo(w: &str, led: bool) -> Option<&'static str> {
let w = w.to_lowercase();
if WORDS.reserved.contains(&w.as_str()) {
return None; }
let extra = if led { &WORDS.led[..] } else { &[] };
let max = |r: &str| if led { 2.min((r.len() - 1) / 2) } else { 1 };
WORDS.commands.iter().chain(extra).copied().find(|r| (1..=max(r)).contains(&strsim::osa_distance(&w, r)))
}
enum Class {
Id(Id),
Job(usize),
Link(usize),
Media(usize),
Url(String),
File(String),
Res(&'static str),
Mod(String, String),
Num(String),
Word,
}
pub fn parse(argv: &[String]) -> Cmd {
let mut toks = repair(argv);
let mut cmd = Cmd { raw: toks.join(" "), ..Cmd::default() };
let lead = toks.iter().position(|t| !matches!(class_of(t, false, false, true), Ok(Class::Mod(..))));
cmd.verb = lead.filter(|&i| WORDS.verbs.contains(&toks[i].to_lowercase().as_str())).map(|i| toks.remove(i));
if let Some(i) = lead.filter(|_| cmd.verb.is_none()) {
own_words(&mut cmd, &mut toks, i);
}
let first_help = cmd.verb.is_none() && lead.is_some_and(|i| toks[i].eq_ignore_ascii_case("help"));
if first_help || toks.iter().any(|t| HELP_FLAGS.contains(&t.as_str())) {
toks = vec!["help".into()];
}
let has_id = toks.iter().any(|t| Id::parse(t).is_some());
let (mut cls, mut any_id) = (Vec::new(), false);
for t in &toks {
match class_of(t, has_id, any_id, cmd.verb.is_some()) {
Ok(Class::File(f)) if !crate::cli::outside(&local_path(&f)) && local_path(&f).is_dir() => {
cmd.ask.get_or_insert(format!("? {f} is a directory · name a file in it"));
}
Ok(c) => {
any_id |= matches!(c, Class::Id(_));
cls.push((t.clone(), c));
}
Err(q) => {
cmd.ask.get_or_insert(q);
}
}
}
as_words(&mut cls);
let search = cmd.verb.is_none() && !cls.iter().any(|p| matches!(p.1, Class::Id(_) | Class::File(_)));
let words: Vec<&String> = cls.iter().filter(|p| search && matches!(p.1, Class::Word)).map(|p| &p.0).collect();
if let Some(t) = words.first().filter(|_| words.iter().all(|t| is_flag(t))) {
let name = t.trim_start_matches(['-', '–', '—']);
let fix =
if WORDS.bools.contains(&name) { format!("mod: {name}:true") } else { format!("keys: {} setup", bin()) };
cmd.ask.get_or_insert(format!("! unknown flag {t} · {fix} · help: {} --help", bin()));
}
let kinds: Vec<&(String, Class)> =
cls.iter().filter(|p| !matches!(p.1, Class::Mod(..) | Class::Res("json"))).collect();
let led = matches!(kinds[..], [(_, Class::Word), (_, Class::Url(_) | Class::Id(_)), ..]);
let (lone, none) = (matches!(kinds[..], [(_, Class::Word)]), kinds.is_empty());
if let [(t, Class::Url(_))] = kinds[..]
&& let Some(q) = file_or_site(t, &WORDS.file_tlds)
{
cmd.ask.get_or_insert(q);
}
sort_into(&mut cmd, kinds.len() == 1, cls, any_id);
cmd.idle = none && cmd.get("site").is_none() || cmd.ask.is_some();
if cmd.verb.is_none() && (led || lone) {
ask_typo(&mut cmd, led);
}
if cmd.ask.is_some() && cmd.words.last().is_some_and(|w| w.eq_ignore_ascii_case("json")) {
cmd.words.pop();
cmd.reserved.push("json");
}
cmd
}
fn class_of(t: &str, has_id: bool, any_id: bool, verb: bool) -> Result<Class, String> {
let low = t.to_lowercase();
if let Some(id) = Id::parse(t) {
return Ok(Class::Id(id));
}
if let Some(n) = t.strip_prefix('j').and_then(num) {
return Ok(Class::Job(n));
}
if let Some(n) = t.strip_prefix('l').and_then(num) {
return Ok(Class::Link(n));
}
if let Some(n) = t.strip_prefix('m').and_then(num).filter(|_| has_id) {
return Ok(Class::Media(n));
}
if let Some((a, w)) = t.split_once(':')
&& Id::parse(a).is_some_and(|i| i.sec.is_some())
&& w.split('-').all(|x| num(x).is_some())
{
return Err(format!("? {t}: line windows are not in v1 · read the section: {} {a}", bin()));
}
let alias = WORDS.aliases.iter().find(|a| !verb && (a.0 == low || a.0 == t)).map(|a| a.1);
if let Some(r) = WORDS.reserved.iter().copied().find(|r| *r == low).or(alias) {
return Ok(Class::Res(r));
}
if shaped(t) && !low.starts_with("file:") {
return Ok(Class::File(t.to_string()));
}
if low.starts_with("file:") {
let path = url::Url::parse(t).ok().filter(|u| u.scheme() == "file" && u.host().is_none());
let path = path.and_then(|u| u.to_file_path().ok()).map(|p| Class::File(p.to_string_lossy().into_owned()));
return path.ok_or(format!("? {t}: a file URL names a local path: {} /path/to/file", bin()));
}
if let Some((k, v)) = key_value(t) {
if WORDS.operators.contains(&k.to_ascii_lowercase().as_str()) {
return Ok(Class::Word);
}
return modifier(k, v).map(|(k, v)| Class::Mod(k, v));
}
if is_file(t, has_id)? {
return Ok(Class::File(t.to_string()));
}
if !any_id && is_url(t) && bare_tld(&low).is_none_or(|tld| WORDS.tlds.contains(&tld)) {
return Ok(Class::Url(if low.starts_with("http") { t.to_string() } else { format!("https://{t}") }));
}
if num(t).is_some() {
return Ok(Class::Num(t.to_string()));
}
if t.len() > 1
&& t.trim_start_matches(['u', 'j', 'l']).len() + 1 == t.len()
&& t[1..].bytes().all(|b| b.is_ascii_digit())
{
return Err(format!("? {t}: out of range"));
}
Ok(Class::Word)
}
fn as_words(cls: &mut [(String, Class)]) {
let free = cls.iter().any(|(t, c)| matches!(c, Class::Word | Class::Num(_)) && !is_flag(t));
let lead = cls.iter().position(|(t, c)| !matches!(c, Class::Mod(..)) && !is_flag(t));
for (i, (t, c)) in cls.iter_mut().enumerate() {
let typed = Some(i) == lead && !t.bytes().any(|b| b.is_ascii_uppercase());
if matches!(c, Class::Url(_)) && bare_tld(&t.to_lowercase()).is_some() && free && !typed {
*c = Class::Word;
}
}
if cls.iter().any(|p| matches!(p.1, Class::Word)) {
for (t, c) in cls.iter_mut() {
if let Class::File(p) = c
&& (p == "-" || !shaped(t) && Path::new(p).extension().is_none() && !filed(&local_path(p)))
{
*c = Class::Word;
}
}
}
}
fn is_flag(t: &str) -> bool {
t.len() > 1 && t.starts_with(['-', '–', '—'])
}
fn sort_into(cmd: &mut Cmd, alone: bool, cls: Vec<(String, Class)>, any_id: bool) {
let words = cls.iter().filter(|(_, c)| matches!(c, Class::Word)).count();
let nums = cls.iter().filter(|(_, c)| matches!(c, Class::Num(_))).count();
let pinned = cls.iter().any(|(_, c)| matches!(c, Class::Mod(k, _) if k == "match"));
let free = words > 0 || nums > 1 || (nums > 0 && (!any_id || pinned));
let page = cls.iter().any(|(_, c)| matches!(c, Class::Id(_) | Class::Url(_) | Class::File(_)));
let lead = cls.iter().position(|(_, c)| matches!(c, Class::Res(_) | Class::Word | Class::Num(_)));
let index = lead.is_some_and(|i| matches!(cls[i].1, Class::Res("stats")));
let read = index || cls.iter().any(|(_, c)| matches!(c, Class::Id(_) | Class::File(_)));
let last = cls.iter().rposition(|(_, c)| !matches!(c, Class::Mod(..)));
let list = |i, r: &str| index || Some(i) == lead && any_id && WORDS.lists.contains(&r);
for (i, (t, c)) in cls.into_iter().enumerate() {
match c {
Class::Res(r) if r == "json" && Some(i) == last && read => cmd.reserved.push(r),
Class::Id(i) => cmd.ids.push(i),
Class::Job(j) => cmd.jobs.push(j),
Class::Link(l) => cmd.links.push(l),
Class::Media(m) => cmd.media.push(m),
Class::Url(u) => cmd.urls.push(u),
Class::File(f) => cmd.files.push(f),
Class::Mod(k, v) => cmd.mods.push((k, v)),
Class::Res(r) if !free && (alone || !page || !WORDS.alone.contains(&r)) || list(i, r) => {
cmd.reserved.push(r)
}
Class::Num(n) if cmd.reserved.iter().any(|r| WORDS.lists.contains(r)) => cmd.words.push(n),
Class::Num(n) if !free => {
let q = number_ask(cmd, &n);
cmd.ask.get_or_insert(q);
}
_ => cmd.words.push(t),
}
}
}
fn number_ask(cmd: &Cmd, n: &str) -> String {
let b = bin();
match cmd.ids.last() {
Some(i) => {
let (full, page) = (cmd.canon(), i.page());
format!("? {n}: link → {b} {full} l{n} · section → {b} {page}#{n} · text → {b} {full} {n} match:words")
}
None => format!("? {n}: link → {b} l{n} · text → {b} {n} match:words"),
}
}
fn own_words(cmd: &mut Cmd, toks: &mut Vec<String>, i: usize) {
let at = |k: usize| toks.get(i + k).map(|t| t.to_lowercase()).unwrap_or_default();
let (verb, on, switches) = (toks[i].to_lowercase(), at(2) == "on" || at(2) == "off", crate::settings::SWITCHES);
let own = |t: &String| {
!HELP_FLAGS.contains(&t.as_str())
&& t != "json"
&& !matches!(class_of(t, false, false, true), Ok(Class::Mod(..)))
};
let n = match (verb.as_str(), at(1).as_str()) {
("setup", w) if switches.contains(&w) || w == "plain" => 1 + usize::from(on),
("setup", "telemetry") => 1 + usize::from(toks.get(i + 2).is_some_and(own)),
("telemetry", "show") | ("setup", "import-env") => 1,
("config", "set") => 3 + toks.iter().skip(i + 4).take_while(|t| own(t)).count(),
("config", "unset") => 2,
_ => 0,
};
let sub = toks.drain(i + 1..(i + 1 + n).min(toks.len())).enumerate();
let low =
|(k, t): (usize, String)| if k > 1 { t } else { t.to_lowercase().replacen("plain", "free", (k == 0).into()) };
cmd.sub = sub.map(low).collect();
if cmd.sub.len() > 3 {
let v = cmd.sub.split_off(2).join(" ");
cmd.sub.push(v);
}
let word = |t: &&String| own(t) && matches!(class_of(t, false, false, true), Ok(Class::Word));
let Some(w) = toks[i + 1..].iter().find(word).filter(|_| verb == "setup") else { return };
let near = switches.into_iter().chain(["on", "off"]).find(|n| strsim::osa_distance(&w.to_lowercase(), n) == 1);
let (raw, b, all) = (&cmd.raw, bin(), switches.join("|"));
let fix = near.map_or(format!(" · {b} setup <{all}|plain> [on|off] · zone:<name>"), |n| {
format!(" → {b} {}", raw.replace(w.as_str(), n))
});
cmd.ask = Some(format!("? {raw}{fix}"));
}
fn ask_typo(cmd: &mut Cmd, led: bool) {
let (Some(w), b) = (cmd.words.first().cloned(), bin()) else { return };
if let Some((_, why)) = WORDS.elsewhere.iter().find(|e| !led && e.0.split(' ').any(|o| w.eq_ignore_ascii_case(o))) {
cmd.ask.get_or_insert(format!("! {w}{} · {}", draw(why, &[("b", b)]), to_guide()));
}
if let Some(r) = typo(&w, led) {
let fixed: Vec<&str> = cmd.raw.split(' ').map(|t| if t == w { r } else { t }).collect();
let q = format!("? {w}: a command? {b} {} · a web search: {b} search {}", fixed.join(" "), cmd.raw);
cmd.ask.get_or_insert(q);
}
}
#[derive(Default)]
pub struct Out {
pub lines: Vec<String>,
pub rc: i32,
pub rec: Box<serde_json::Map<String, serde_json::Value>>,
pub parts: Vec<Out>,
}
impl Out {
pub fn new(lines: Vec<String>) -> Out {
Out { lines, ..Out::default() }
}
pub fn ask(line: &str) -> Out {
Out { rc: 2, ..Out::new(vec![crate::setup::masked(line)]).with("error", "usage") }
}
pub fn fail(code: &str, eq: String, hint: String) -> Out {
Out { rc: 1, ..Out::new(vec![eq, hint.clone()]).with("error", code).with("hint", hint) }
}
pub fn set(&mut self, k: &str, v: impl Into<serde_json::Value>) -> &mut Out {
self.rec.insert(k.into(), v.into());
self
}
pub fn with(mut self, k: &str, v: impl Into<serde_json::Value>) -> Out {
self.set(k, v);
self
}
pub fn warn(&mut self, rows: Vec<String>) {
if rows.is_empty() {
return;
}
let bang = |l: &String| l.split(' ').nth(1) == Some("!");
let at = self.lines.len().min(1) + self.lines.iter().skip(1).take_while(|l| bang(l)).count();
self.lines.splice(at..at, rows.iter().cloned());
let mut all = self.rec.remove("warnings").and_then(|w| w.as_array().cloned()).unwrap_or_default();
all.extend(rows.into_iter().map(Into::into));
self.set("warnings", all);
}
pub fn cap(&mut self, max: usize, fix: &str) {
let mut used = 0;
let Some(keep) = self.lines.iter().skip(1).position(|l| {
used += crate::clean::tok(l);
used > max
}) else {
return;
};
let cut: Vec<String> = self.lines.drain(keep + 1..).collect();
let t: usize = cut.iter().map(|l| crate::clean::tok(l)).sum();
self.lines.push(format!("… max:{max} cut {} lines ~{t}tok → {} {fix}", cut.len(), bin()));
self.set("cut", cut.len());
}
}
pub fn emit(o: &Out, json: bool) -> String {
let text = match (json, o.parts.is_empty()) {
(false, _) => o.lines.join("\n") + "\n",
(true, true) => record(o, &[]),
(true, false) => {
let warn = o.rec.get("warnings").and_then(|w| w.as_array()).map_or(&[][..], |w| w.as_slice());
o.parts.iter().map(|p| record(p, warn)).collect()
}
};
redact(&text, &crate::settings::secrets())
}
pub const MASK: &str = "***";
const SECRET_RUN: usize = 12;
const SECRET_MIN: usize = 8;
pub fn redact(text: &str, secrets: &[String]) -> String {
let (mut hide, low) = (vec![false; text.len()], text.to_ascii_lowercase());
for s in secrets.iter().map(|s| s.to_ascii_lowercase()) {
let at: Vec<usize> = s.char_indices().map(|c| c.0).chain([s.len()]).collect();
let n = at.len() - 1;
if n < SECRET_MIN {
continue;
}
let w = n.min(SECRET_RUN);
for i in 0..=n - w {
let run = &s[at[i]..at[i + w]];
for (j, _) in low.match_indices(run) {
hide[j..j + run.len()].fill(true);
}
}
}
if !hide.contains(&true) {
return text.to_string();
}
let mut out = String::with_capacity(text.len());
let mut masked = false;
for (i, c) in text.char_indices() {
match (hide[i], masked) {
(true, false) => out.push_str(MASK),
(false, _) => out.push(c),
_ => {}
}
masked = hide[i];
}
out
}
fn record(o: &Out, warn: &[serde_json::Value]) -> String {
let mut r = o.rec.clone();
if !warn.is_empty() {
let mut all = warn.to_vec();
if let Some(serde_json::Value::Array(own)) = r.remove("warnings") {
all.extend(own);
}
r.insert("warnings".into(), all.into());
}
let text = o.lines.join("\n");
for (k, v) in [
("provider", "local".into()),
("ms", 0.into()),
("cost", 0.0.into()),
("bytes_in", 0.into()),
("cache", false.into()),
("id", serde_json::Value::Null),
("url", serde_json::Value::Null),
] {
r.entry(k).or_insert(v);
}
r.insert("bytes_out".into(), text.len().into());
r.insert("lines".into(), o.lines.clone().into());
r.sort_keys();
serde_json::Value::Object(*r).to_string() + "\n"
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
thread_local! {
pub(crate) static NO_COLOR: std::cell::Cell<Option<bool>> = const { std::cell::Cell::new(None) };
}
fn c(s: &str) -> Cmd {
parse(&s.split(' ').map(String::from).collect::<Vec<_>>())
}
#[test]
fn draw_fills_named_holes_only() {
assert_eq!(draw("{a}+{a} {c} {\"k\": 1} {", &[("a", "1"), ("b", "2")]), "1+1 {c} {\"k\": 1} {");
assert_eq!(draw("{bold}x{/}", &NO_HOLES), "{bold}x{/}");
}
#[test]
fn every_screen_fills_every_hole() {
let mut fills = vec!["ready", "yours", "pages", "id", "last", "spent", "site", "label", "text", "from"];
fills.extend([
"status", "name", "short", "mint", "signup", "step", "rank", "free", "cost", "b", "url", "env", "level",
]);
let holes: Vec<(&str, &str)> = fills.iter().map(|h| (*h, "X")).collect();
for (name, text) in SCREENS {
let out = paint(text, &holes);
let left = out.split('{').skip(1).find(|r| r.split_once('}').is_some_and(|(h, _)| !h.contains(' ')));
assert!(!text.is_empty() && left.is_none(), "{name}: {left:?}");
}
assert_eq!(screen("help-nope"), "");
assert_eq!(help_screen(&["help".into(), "nope".into()]), Some(paint(screen("help"), &NO_HOLES)));
assert_eq!(help_screen(&["rust".into(), "help".into()]), None, "a search for help");
}
const REPAIRS: &[(&str, &str)] = &[
("read u2#3", "read u2#3"),
("open u6", "open u6"),
("search cheap proxies", "search cheap proxies"),
("u2.3", "u2#3"),
("u2 #3", "u2#3"),
("\"u2#3\"", "u2#3"),
("U2#3", "u2#3"),
("`fats u2#3`", "u2#3"),
("u2#3 --full", "u2#3 full"),
("u2 -n 20", "u2 limit:20"),
("u2 n=20", "u2 limit:20"),
("u2#3 l 4", "u2#3 l4"),
("j8/u2", "j8.u2"),
("j8:u2", "j8.u2"),
("J8.U2#3b", "j8.u2#3b"),
("u3 loose:50", "u3 match:near"),
("u3 price js:1", "u3 price js:true"),
("u3 n:5 geo:FR budget:0.5", "u3 limit:5 country:fr cost:0.5"),
("ex.com ua:Phone", "https://ex.com device:mobile"),
("u3#2 full price", "u3#2 full price"),
("u3#2 links", "u3#2 links"),
("u1 links From:11", "u1 links from:11"),
("u1 iphone 15", "u1 iphone 15"),
("u1 15 match:words", "u1 15 match:words"),
("u1 -n", "u1 -n"),
("u1 -x -y", "u1 -x -y"),
("example.com", "https://example.com"),
("opencode mcp config opencode.yaml", "opencode mcp config opencode.yaml"),
("pyproject.toml build backend options", "pyproject.toml build backend options"),
("nextjs next.config.mjs", "nextjs next.config.mjs"),
("main.go error", "main.go error"),
("logo.png size", "logo.png size"),
("cargo build main.rs", "cargo build main.rs"),
("build lib.so", "build lib.so"),
("rust x.ai", "rust x.ai"),
("contact at acme.com phone", "contact at acme.com phone"),
("Purchasing.NYC Lakewood New Jersey", "Purchasing.NYC Lakewood New Jersey"),
("Booking.com hotels Lisbon", "Booking.com hotels Lisbon"),
("Booking.com", "https://Booking.com"),
("fetch docs.rs tokio", "fetch https://docs.rs tokio"),
("docs.rs tokio spawn", "https://docs.rs tokio spawn"),
("limit:5 docs.rs tokio", "https://docs.rs tokio limit:5"),
("example.com example.org", "https://example.com https://example.org"),
("Example.com Example.org limit:5", "https://Example.com https://Example.org limit:5"),
("example.com example.org rust", "https://example.com example.org rust"),
("main.rs error handling", "https://main.rs error handling"),
("rust example.com", "rust example.com"),
("example.com rust", "https://example.com rust"),
("x.co", "https://x.co"),
("a.io", "https://a.io"),
("blog.rust-lang.org", "https://blog.rust-lang.org"),
("tokio docs.rs/tokio", "https://docs.rs/tokio tokio"),
("https://main.rs", "https://main.rs"),
("https://ex.com/a?b=1", "https://ex.com/a?b=1"),
("u1 v2.0", "u1 v2.0"),
("dry:yes u1#2", "u1#2 dry:true"),
("l4", "l4"),
("help", "help"),
("u1 max_bytes:32MiB", "u1 max_bytes:33554432"),
("-h", "help"),
("--help", "help"),
("how to get help", "how to get help"),
("u1 hedge:0", "u1 hedge:0"),
("u1 hedge:5s", "u1 hedge:5"),
("u1 timeout:300", "u1 timeout:300"),
("rust news site:HTTPS://Blog.Rust-Lang.org/ since:2w", "rust news site:blog.rust-lang.org since:2w"),
("serde site:docs.rs/serde", "serde site:docs.rs/serde"),
("status", "status"),
("ls", "status"),
("--version", "version"),
("-V", "version"),
("rust status", "rust status"),
("u1 version", "u1 version"),
("status json:true", "status json:true"),
("search stat", "search stat"),
("search login", "search login"),
("stats", "stats"),
("ale", "ale"),
("yelp", "yelp"),
("fuel", "fuel"),
("jason", "jason"),
("mode", "mode"),
("acont", "acont"),
("acont limit:5", "acont limit:5"),
("yelp limit:5", "yelp limit:5"),
("go https://go.dev", "https://go.dev go"),
("rust u1", "u1 rust"),
("search fetchh https://x.com", "search https://x.com fetchh"),
("fetchh rust", "fetchh rust"),
#[cfg(unix)]
("file:///tmp/a%20b.html", "/tmp/a b.html"),
#[cfg(unix)]
("file://localhost/tmp/x", "/tmp/x"),
#[cfg(windows)]
("file:///C:/tmp/a%20b.html", "C:\\tmp\\a b.html"),
("login", "setup"),
("Sign-In", "setup"),
("usage", "account"),
("-v", "verbose:true"),
("stat json", "stats json"),
("login limit:5", "setup limit:5"),
("keys json", "setup json"),
("login page design", "login page design"),
("u1 key", "u1 key"),
("u1 install", "u1 install"),
("rust --help", "help"),
("rust —help", "help"),
("–help", "help"),
("-- rust", "rust"),
("u1 Rust — the book", "u1 Rust — the book"),
("rust — tokio", "rust — tokio"),
("tokio —the runtime", "tokio —the runtime"),
("1990 – 2000 census", "1990 – 2000 census"),
("help login", "help"),
("git rebase --onto", "git rebase --onto"),
("search -tokio rust", "search -tokio rust"),
("rust -tokio", "rust -tokio"),
("rust filetype:pdf before:2026-01", "rust filetype:pdf before:2026-01"),
("NORTHROP GRUMMAN CORP /DE/ company", "NORTHROP GRUMMAN CORP /DE/ company"),
("irs liaison committee - tei", "irs liaison committee - tei"),
("./nope.md summary", "./nope.md summary"),
("acme /etc/hosts", "/etc/hosts acme"),
("C:\\x acme", "C:\\x acme"),
("C:/x", "C:/x"),
("docs.rs tokio", "https://docs.rs tokio"),
("acme .\\secret\\plan", ".\\secret\\plan acme"),
("acme ~alice/secret", "~alice/secret acme"),
("acme ${HOME}/secret", "${HOME}/secret acme"),
("acme internal/hr/salaries", "internal/hr/salaries acme"),
("acme Documents/plan.key", "Documents/plan.key acme"),
("TCP/IP and/or AC/DC", "TCP/IP and/or AC/DC"),
("~5/10 $5/mo plan", "~5/10 $5/mo plan"),
("docs.rs/tokio/latest/tokio spawn", "https://docs.rs/tokio/latest/tokio spawn"),
("summary .env", ".env summary"),
(".ssh/config", ".ssh/config"),
("learn .NET core .io ... .5mm", "learn .NET core .io ... .5mm"),
];
const ASKS: &[(&str, &str)] = &[
("u2#3 4", "? 4: link → "),
("x foo:1", "? foo: unknown · keys limit match"),
("rust countr:lt", "? countr: unknown · keys"),
("notes.md hello", "? notes.md: a missing file or a site · type ./notes.md or https://notes.md"),
("main.rs", "? main.rs: a missing file or a site · type ./main.rs or https://main.rs"),
("docs.rs limit:5", "? docs.rs: a missing file or a site"),
("acme phone ./src", "? ./src is a directory · name a file in it"),
("u1 js:maybe", "? js:maybe · use js:true"),
("u1 match:fuzzy", "? match:fuzzy"),
("u1 max_bytes:65m", "? max_bytes:65m · 1k to 64m"),
("u1 max_bytes:lots", "? max_bytes:lots"),
("ex.com device:tablet", "? device:tablet · use device:mobile|desktop"),
("u1 hedge:61", "? hedge:61 · seconds before the second provider, 0 to 60"),
("u1 hedge:soon", "? hedge:soon"),
("u1 timeout:301", "? timeout:301 · seconds for bd and oxyjs, 1 to 300"),
("u1 timeout:99999999", "? timeout:99999999"),
("ex.com provider:oxyjs", "? provider:oxyjs · use provider:auto|plain|bd|oxy|decodo"),
("rust since:lately", "? since:lately · an age or a date"),
("rust since:2026-13", "? since:2026-13"),
("rust site:not_a_host", "? site:not_a_host · a host"),
("stts", "? stts: a command? "),
("acount", "? acount: a command? "),
("confg", "? confg: a command? "),
("cofnig", "? cofnig: a command? "),
("verison", "? verison: a command? "),
("versoin", "? versoin: a command? "),
("rotues", "? rotues: a command? "),
("fetchh", "? fetchh: a command? fats fetch · a web search: fats search fetchh"),
("fetchh https://x.com", "? fetchh: a command? fats fetch https://x.com · a web search: fats search fetchh"),
("fecth u1", "? fecth: a command? fats fetch u1 "),
("acont u1", "? acont: a command? fats account u1 "),
("hlep u1", "? hlep: a command? fats help u1 "),
("stts json:true", "? stts: a command? fats stats json:true "),
("file://host/x", "? file://host/x: a file URL names a local path"),
("l99999999999999999999", "? l99999999999999999999: out of range"),
("u1 u99999999999999999999", "? u99999999999999999999: out of range"),
("--login", "! unknown flag --login · keys: fats setup · help: fats --help"),
("-x", "! unknown flag -x · "),
("limit:5 -x", "! unknown flag -x · "),
("–x", "! unknown flag –x · "),
("-x -y", "! unknown flag -x · "),
("--login -x", "! unknown flag --login · "),
("—", "! unknown flag — · "),
("--dry ex.com", "! unknown flag --dry · mod: dry:true · "),
("logout", "! logout is not a fats command · "),
("install json", "! install: see docs/installation.md"),
("stts json", "? stts: a command? "),
];
#[test]
fn tlds_and_host_shapes() {
assert_eq!((WORDS.tlds.len(), WORDS.tlds[0], WORDS.tlds.last().copied()), (1437, "aaa", Some("zw")));
assert_eq!(
(c("rust site:wiki.corp").get("site"), c("wiki.corp/a b").urls[0].as_str()),
(Some("wiki.corp"), "https://wiki.corp/a")
);
let ours = |o: &&&str| WORDS.keys.contains(o) || crate::settings::LIMITS.contains_key(**o);
assert_eq!(WORDS.operators.iter().find(ours), None);
}
#[test]
fn real_host_needs_askable_labels() {
let (l63, l64) = ("a".repeat(63), "a".repeat(64));
let good = [format!("https://{l63}.com/"), "https://a-b.example.com./x".into(), "http://[::1]/".into()];
let good = good.into_iter().chain(["https://xn--bcher-kva.de/".into(), "https://93.184.215.14/a".into()]);
for u in good {
assert!(real_host(&u), "{u}");
}
let bad = [format!("https://{l64}.com/"), format!("https://x.{l64}.com/"), "https://a..com/".into()];
let bad = bad
.into_iter()
.chain(["https://-a.com/", "https://a-.com/", "https://a.-com.com/", "https://.a.com/"].map(String::from));
for u in bad.chain(["https://dead.invalid/a".into(), "https://a.com../".into()]) {
assert!(!real_host(&u), "{u}");
}
}
#[test]
fn repair_table() {
for (i, want) in REPAIRS {
let got = c(i);
assert!(got.ask.is_none(), "{i} → {:?}", got.ask);
assert_eq!(&got.canon(), want, "input: {i}");
}
for (i, _) in REPAIRS {
let (once, twice) = (c(i).canon(), c(&c(i).canon()).canon());
assert_eq!((c(&once).words, &twice), (c(i).words, &once), "{i}: the canonical form replays");
}
let reading =
|m: &Cmd| (m.verb.clone(), m.words.clone(), m.reserved.clone(), m.ids.iter().map(Id::page).collect());
for (v, w) in WORDS.verbs.iter().flat_map(|v| ["login", "u1", "stat"].map(|w| (v, w))) {
for i in [format!("limit:5 {v} {w}"), format!("{v} {w} limit:5"), format!("-v {v} {w}")] {
let once = c(&i);
let want: (_, _, _, Vec<String>) = reading(&once);
assert!(once.verb.is_some() && once.ask.is_none(), "{i}: the verb is read");
for line in [once.canon(), once.with_mod("limit", Some("3"))] {
assert_eq!(reading(&c(&line)), want, "{i} → {line}: the same reading");
}
}
}
for (i, want) in ASKS {
let got = c(i).ask.unwrap_or_default().replace(bin(), "fats");
assert!(got.starts_with(want), "{i} → {got}");
}
let searches = [
("rust news json", "rust news json"),
("json parser", "json parser"),
("example.com rust json", "rust json"),
];
for (i, json, words) in searches.map(|(i, w)| (i, false, w)) {
let got = c(i);
assert_eq!((got.has("json"), got.words.join(" ").as_str()), (json, words), "{i}");
}
let mod_on = c("rust news json:true");
assert!(mod_on.flag("json"));
assert_eq!(mod_on.words, ["rust", "news"]);
for asked in ["whoami json", "stts json"] {
assert!(c(asked).has("json") && !c(asked).words.contains(&"json".to_string()), "{asked}");
}
assert!(c("u1 price json").has("json") && c("u1 price json").words == ["price"]);
let st = c("stats rust http all json");
assert!(st.has("stats") && st.has("all") && st.has("json") && st.words == ["rust", "http"], "{:?}", st.words);
assert!(c("rust stats").words == ["rust", "stats"], "a search that names stats stays one");
for i in ["guide all", "setup full", "account more", "login all", "keys links"] {
assert!(c(i).words.is_empty() && c(i).ask.is_none(), "{i}: {:?} {:?}", c(i).words, c(i).ask);
}
assert_eq!((c("u1 setup").words, c("https://a.b setup").words), (vec!["setup".into()], vec!["setup".into()]));
}
#[test]
fn warnings_follow_the_cause() {
let mut o = Out::new(vec!["= u1 ! js_shell T".into(), "u1 ! js_shell x → fix".into(), "[u1#1] # T".into()]);
o.warn(vec!["u1 ! no_key oxy → fats setup".into()]);
o.warn(vec!["u1 ! ignored device".into()]);
let want =
["= u1 ! js_shell T", "u1 ! js_shell x → fix", "u1 ! no_key oxy → fats setup", "u1 ! ignored device"];
assert_eq!(o.lines[..4], want);
assert_eq!(o.rec["warnings"], serde_json::json!(want[2..]));
}
#[test]
fn near_words() {
for (a, b, n) in [
("stat", "stats", 1),
("acount", "account", 1),
("confg", "config", 1),
("cofnig", "config", 1),
("sats", "stats", 1),
("fetchh", "fetch", 1),
("sta", "stats", 2),
("acont", "account", 2),
("cinfog", "config", 2),
("rust", "status", 5),
("ab", "ba x", 3),
] {
assert_eq!((strsim::osa_distance(a, b), strsim::osa_distance(b, a)), (n, n), "{a} {b}");
}
assert_eq!((typo("stat", false), typo("ale", false), typo("us", false)), (Some("stats"), None, None));
assert_eq!((typo("stats", false), typo("yelp", false), typo("fuel", false)), (None, None, None));
assert_eq!(
(typo("rotues", false), typo("fetchh", false), typo("acont", false)),
(Some("routes"), Some("fetch"), None)
);
assert_eq!(
(typo("acont", true), typo("hlep", true), typo("gh", true), typo("map", true)),
(Some("account"), Some("help"), None, Some("mcp"))
);
}
#[test]
fn hostile_inputs() {
let junk = [
"", " ", "#", "u", "u#", "u1#", "u1#a", "u99999999999999999999999", "j.u1", "j1.", "l", "l-1", "::", "a:",
":b", "http:", "https://", "--", "-n", "-n x", "n=", "=5", "``", "\"", "u1 #", "u1 # 3", "ü1#2", "u1#2zz",
"\u{0}", "u1\u{a0}links", "🙂 u1", "j8/u", "file:///etc/passwd", "../", "~/", "u1 l 4 l 5", "all all",
];
for j in junk {
let cmd = c(j);
cmd.canon();
let usage = |q: &String| q.starts_with("? ") || q.starts_with("! unknown flag ");
assert!(cmd.ask.as_ref().is_none_or(usage), "{j} → {:?}", cmd.ask);
}
for a in ['a', 'u', '#', '1', ':', '.', ' ', 'l', 'j', '/'] {
for b in ['a', 'u', '#', '1', ':', '.', ' ', 'l', 'j', '/'] {
c(&format!("u1{a}{b}2{a}")).canon();
}
}
}
#[test]
fn part_ids_round_trip() {
assert_eq!(
[letters(0), letters(1), letters(25), letters(26), letters(27), letters(701), letters(702)],
["a", "b", "z", "aa", "ab", "zz", "aaa"]
);
for p in 0..20_000 {
let id = Id::parse(&format!("j3.u2#7{}", letters(p))).unwrap_or_default();
assert_eq!((id.job, id.u, id.sec, id.part), (Some(3), 2, Some(7), p));
}
assert_eq!(Id::parse("u2#7").map(|i| i.part), Some(0));
assert!(Id::parse(&format!("u2#7{}", "z".repeat(40))).is_none());
}
#[test]
fn every_paid_arrow_is_priced() {
let args = ["", "js:true", "provider:bd", "provider:oxy js:true", "wait:30", "timeout:30", "0.0025"];
let dones: [&[&str]; 4] = [&[], &["plain", "bd"], &["plain", "oxyjs"], &["plain", "oxy", "oxyjs"]];
let cases = args
.into_iter()
.flat_map(|a| dones.map(|d| (a, d)))
.flat_map(|c| [("https://93.184.215.14/a", c), ("", c)]);
let (mut paid, b) = (0, bin());
for (url, (arg, done)) in cases.collect::<Vec<_>>() {
let fits = |r: &&(&str, &str)| match r.0 {
"budget_hit" => arg == "0.0025",
"provider_timeout" => arg != "0.0025",
_ => arg.is_empty() || !r.1.contains("{arg}"),
};
for code in FIX.iter().filter(fits).map(|r| r.0.replace('*', "403")) {
let f = fix(&code, &At { id: "u3", url, arg, done });
let cmd = f.split(", ").next().unwrap_or_default().split(" ≤$").next().unwrap_or_default();
let mods = ["fresh:", "js:", "provider:", "timeout:"];
if !cmd.starts_with(b) || !cmd.split(' ').any(|w| mods.iter().any(|k| w.starts_with(k))) {
continue;
}
paid += 1;
let words: Vec<String> = cmd.split(' ').skip(1).map(Into::into).collect();
assert_eq!(parse(&words).ask, None, "{code} {arg}: {f}");
let price = f.split(" ≤$").nth(1).and_then(|p| p.get(..6)).unwrap_or_default();
let dry = words.iter().map(|w| if w == "u3" && !url.is_empty() { url } else { w.as_str() });
let dry: Vec<String> = dry.map(Into::into).chain(["dry:true".into()]).collect();
let c = crate::cache::Cache::at(crate::settings::get().cache.value.clone(), "t".into());
let rows = crate::cli::plan(&parse(&dry), &c).lines.join("\n");
let up = rows.split(" up to ").nth(1).and_then(|r| r.split([' ', '\n']).next()).unwrap_or("none");
if !url.is_empty() {
assert_eq!(format!("${price}"), up, "{code} {arg}: {f}\n{rows}");
}
assert!(f.starts_with(&format!("{cmd} ≤$")), "{code} {arg}: {f}");
}
}
assert!(paid > 500, "{paid}");
}
#[test]
fn a_third_key_is_the_move() {
let at = At { id: "u3", done: &["plain", "oxy", "bd", "oxyjs"], ..Default::default() };
assert_eq!(fix("captcha_wall", &at), format!("{} u3 fresh:true provider:decodo", bin()));
}
#[test]
fn error_table() {
let keys: Vec<&str> = FIX.iter().map(|r| r.0).collect();
let mut uniq = keys.clone();
uniq.sort_unstable();
uniq.dedup();
assert_eq!(uniq.len(), keys.len(), "a code with two fixes");
let born = [include_str!("provider.rs"), include_str!("fetch.rs"), include_str!("stats.rs")].concat();
let mut codes: Vec<&str> = born
.split('"')
.skip(1)
.step_by(2)
.filter(|w| w.contains('_') && w.bytes().all(|b| b.is_ascii_lowercase() || b == b'_' || b.is_ascii_digit()))
.collect();
codes.extend(crate::fetch::CHECK.rules.iter().map(|w| w.code).filter(|c| !c.is_empty()));
codes.extend(["blocked_403", "http_502", "fetch_failed", "no_file", "not_allowed", "too_big", "offline"]);
let names = ["b_algo", "result__a", "cf_chl_opt", "search_query", "status_code", "run_js", "max_results"];
let names = [&names[..], &["brd_json", "dns_wait", "dns_fast_ms"]].concat();
for c in codes.iter().filter(|c| !names.contains(c)) {
let hit = keys.iter().any(|k| k.strip_suffix('*').map_or(k == c, |p| c.starts_with(p)));
assert!(hit, "code {c} has no fix in FIX");
}
let at = At { id: "u3", url: "https://ex.com/a", arg: "0.0025", done: &[] };
let b = bin();
assert_eq!(
fix("provider_timeout", &At { arg: "js:true", ..at }),
format!("{b} u3 fresh:true js:true ≤$0.0030")
);
let at = At { id: "u3", url: "https://ex.com/a", arg: "0.0025", done: &[] };
assert_eq!(
fix("budget_hit", &at),
format!("{b} u3 fresh:true cost:0.0025 ≤$0.0025"),
"dry routes the cap: oxy no-JS + bd"
);
assert_eq!(fix("provider_timeout", &At { arg: "", ..at }), format!("{b} u3 fresh:true ≤$0.0040"), "one space");
assert_eq!(fix("not_found", &at), format!("check the URL · search for it: {b} <words>"), "no home");
let search = At { id: "provider:bd rules", url: "", ..at };
assert_eq!(
fix("provider_refused", &search),
format!("{b} rules fresh:true provider:oxy ≤$0.0010 · last try not charged"),
"one provider:, and bd's refusal is one of `provider::REFUSED`, unbilled"
);
let ceil = At { url: "big.html", done: &["max_bytes:64m"], ..at };
assert_eq!(fix("too_big", &ceil), "nothing left to try · open big.html in a browser", "the cap in force");
assert_eq!(fix("blocked_451", &at), format!("{b} u3 fresh:true provider:oxy js:true ≤$0.0015"));
assert_eq!(fix("brightdata_refused", &at), format!("{b} u3 fresh:true ≤$0.0040"), "an old code: the retry");
assert_eq!(bang("u3", "no_key", "bd", &at), format!("u3 ! no_key bd → {b} setup"));
assert!(FIX.iter().all(|r| !r.1.contains("FATS_ALLOW_PRIVATE")), "a fix prints the private-address bypass");
assert_eq!(fix("private_address", &at), format!("private address refused · see {b} config"));
let done = At { done: &["plain", "oxy", "bd"], ..at };
assert_eq!(fix("not_pdf", &done), "nothing left to try · open https://ex.com/a in a browser");
let want = format!("{b} u3 fresh:true provider:oxy js:true ≤$0.0015");
assert_eq!(fix("captcha_wall", &done), want, "oxyjs untried");
assert_eq!(fix("provider_refused", &done), want, "a refusal's no-JS fix, oxyjs untried");
let all = At { done: &["plain", "oxy", "bd", "oxyjs", "decodo", "decodojs"], url: "", ..at };
assert_eq!(fix("captcha_wall", &all), "nothing left to try");
let skipped = At { done: &["plain", "oxyjs", "bd"], ..at };
assert_eq!(fix("captcha_wall", &skipped), format!("{b} u3 fresh:true provider:oxy ≤$0.0025"));
let free = format!("{b} u3 fresh:true ≤$0.0040 · last try not charged");
assert_eq!(fix("provider_error", &At { arg: "", ..all }), free, "a transient retry names no tier");
let named = At { arg: "provider:oxy js:true", done: &["plain", "oxy", "bd"], ..all };
assert_eq!(fix("provider_error", &named), format!("{want} · last try not charged"), "the tier left");
}
#[test]
fn since_dates() {
let today = 20_738; assert_eq!(civil(today), (2026, 10, 12));
assert_eq!(civil(0), (1970, 1, 1));
for (v, want) in [
("7d", "2026-10-05"),
("2w", "2026-09-28"),
("1m", "2026-09-12"),
("1y", "2025-10-12"),
("0d", "2026-10-12"),
("2026", "2026-01-01"),
("2026-01", "2026-01-01"),
("2024-02-29", "2024-02-29"),
] {
assert_eq!(since_date(v, today).as_deref(), Some(want), "{v}");
}
for v in ["", "d", "7", "7x", "26-01", "2026-1", "2026-00", "2026-01-32", "2026-01-01-01", "-7d", "é7d"] {
assert_eq!(since_date(v, today), None, "{v}");
}
}
#[test]
fn helpers() {
assert_eq!(short("https://www.example.com/abc", 8), "example…");
assert_eq!(ksize(999), "999");
assert_eq!(ksize(1250), "1.2k");
assert_eq!(cut("ąćę", 2), "ąć");
let o = Out::new(vec!["= x".into()]);
assert!(emit(&o, true).contains("\"bytes_out\":3"));
}
#[test]
fn redactor_masks_keys_and_their_runs() {
let key = &["Ab3dEf6hIj9lMn2pQr5t".to_string()];
let s = |v: &[&str]| v.iter().map(|x| x.to_string()).collect::<Vec<_>>();
assert_eq!(redact(&format!("Invalid api key: {}.", key[0]), key), "Invalid api key: ***.");
assert_eq!(redact("echo dEf6hIj9lMn2 end", key), "echo *** end", "a 12-char run");
assert_eq!(redact("dEf6hIj9lMn end", key), "dEf6hIj9lMn end", "11 chars: not a run");
assert_eq!(redact("ąć Ab3dEf6hIj9lMn2pQr5t ąć", key), "ąć *** ąć");
assert_eq!(redact("a sunflower b", &s(&["sunflower"])), "a *** b", "8-11 chars: whole value");
assert_eq!(redact("x y xyz", &s(&["x", "xyz"])), "x y xyz", "too short to be a key");
assert_eq!(redact("ąćęłńóśźż-ąćę", &s(&["ąćęłńóśźż-ąć"])), "***ę", "runs on char boundaries");
}
}