use crate::estimate::Estimate;
use std::borrow::Cow;
const UNIT: &str = "itok";
pub struct Method {
pub tier: &'static str,
pub endpoint: Option<Cow<'static, str>>,
pub approximate: bool,
}
impl Method {
#[must_use]
pub fn label(&self) -> Cow<'_, str> {
match &self.endpoint {
Some(e) => Cow::Owned(format!("{} via {e}", self.tier)),
None => Cow::Borrowed(self.tier),
}
}
}
pub const DUMMY: Method = Method {
tier: "bytes/4",
endpoint: None,
approximate: true,
};
pub const O200K: Method = Method {
tier: "o200k",
endpoint: None,
approximate: false,
};
#[cfg(feature = "ollama")]
#[must_use]
pub fn exact_via(model: &str, base: &str) -> Method {
Method {
tier: "exact",
endpoint: Some(Cow::Owned(format!("{model}@{}", short(base)))),
approximate: false,
}
}
#[cfg(feature = "ollama")]
fn short(base: &str) -> &str {
base.strip_prefix("http://").unwrap_or(base)
}
#[must_use]
pub fn human(n: u64) -> String {
if n >= 1_000_000 {
format!("{}M", n.checked_div(1_000_000).unwrap_or(0))
} else if n >= 1_000 {
format!("{}k", n.checked_div(1_000).unwrap_or(0))
} else {
n.to_string()
}
}
#[must_use]
pub fn total(ests: &[Estimate]) -> u64 {
ests.iter().fold(0, |acc, e| acc.saturating_add(e.tokens))
}
#[derive(Default)]
pub struct Style {
pub summarize: bool,
pub human: bool,
}
fn show(n: u64, human_flag: bool) -> String {
if human_flag { human(n) } else { n.to_string() }
}
fn cell(n: u64, method: &Method, human_flag: bool) -> String {
let mark = if method.approximate { "~" } else { "" };
format!("{mark}{} {UNIT}", show(n, human_flag))
}
fn line(cell: &str, label: &str) -> String {
format!("{cell:>16} {label}\n")
}
#[must_use]
pub fn report(ests: &[Estimate], style: &Style, method: &Method) -> String {
let mut out = String::new();
if !style.summarize {
for e in ests {
out.push_str(&line(&cell(e.tokens, method, style.human), &e.path));
}
}
let total_label = format!("total ({})", method.label());
out.push_str(&line(&cell(total(ests), method, style.human), &total_label));
out
}
#[cfg(test)]
mod tests {
use super::*;
fn est(path: &str, tokens: u64) -> Estimate {
Estimate {
path: path.to_owned(),
tokens,
}
}
#[test]
fn human_uses_k_and_m() {
assert_eq!(human(0), "0");
assert_eq!(human(999), "999");
assert_eq!(human(37_000), "37k");
assert_eq!(human(1_000_000), "1M");
}
#[test]
fn total_sums_tokens() {
assert_eq!(total(&[est("a", 10), est("b", 5)]), 15);
}
#[test]
fn report_lists_files_names_method_and_unit() {
let out = report(&[est("a.rs", 12)], &Style::default(), &DUMMY);
assert!(out.contains("a.rs"));
assert!(out.contains("itok"));
assert!(out.contains("total (bytes/4)"));
}
#[cfg(feature = "ollama")]
#[test]
fn the_exact_label_names_its_endpoint() {
let ip = crate::testutil::HOST_IP;
let m = exact_via("gpt-oss:20b", &format!("http://{ip}:11434"));
assert_eq!(m.label(), format!("exact via gpt-oss:20b@{ip}:11434"));
assert!(!m.approximate, "a true count keeps no tilde (V3)");
}
#[cfg(feature = "ollama")]
#[test]
fn two_endpoints_render_differently() {
let base = format!("http://{}:11434", crate::testutil::HOST_IP);
let a = exact_via("gpt-oss:20b", &base);
let b = exact_via("gpt-oss:20b", "http://localhost:11434");
let c = exact_via("devstral:latest", &base);
assert_ne!(a.label(), b.label(), "a different HOST must be visible");
assert_ne!(a.label(), c.label(), "a different MODEL must be visible");
}
#[cfg(feature = "ollama")]
#[test]
fn https_survives_while_plain_http_is_dropped() {
assert!(exact_via("m", "http://h:1").label().ends_with("m@h:1"));
assert!(
exact_via("m", "https://h:1")
.label()
.ends_with("https://h:1")
);
}
#[test]
fn the_local_tier_labels_are_unchanged() {
assert_eq!(DUMMY.label(), "bytes/4");
assert_eq!(O200K.label(), "o200k");
assert_eq!(DUMMY.endpoint, None, "a local tier needs no qualifier");
}
#[test]
fn the_dummy_tier_marks_its_number_approximate() {
let out = report(&[est("a.rs", 12)], &Style::default(), &DUMMY);
assert!(out.contains("~12 itok"), "expected tilde: {out:?}");
}
#[test]
fn an_exact_tier_drops_the_tilde() {
let exact = Method {
tier: "exact",
endpoint: None,
approximate: false,
};
let out = report(&[est("a.rs", 12)], &Style::default(), &exact);
assert!(out.contains("12 itok"));
assert!(
!out.contains("~"),
"exact must not be tilde-marked: {out:?}"
);
}
#[test]
fn summarize_drops_the_per_file_lines() {
let style = Style {
summarize: true,
human: false,
};
let out = report(&[est("a.rs", 12)], &style, &DUMMY);
assert!(!out.contains("a.rs"));
assert!(out.contains("total"));
}
}