use crate::tokens::{Count, Error, Method};
use std::fmt;
const UNIT: &str = "itok";
fn tier(method: Method) -> itok::render::Method {
match method {
Method::Estimate => itok::render::DUMMY,
Method::Bpe => itok::render::O200K,
}
}
impl fmt::Display for Count {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let tier = tier(self.method);
let mark = if tier.approximate { "~" } else { "" };
write!(f, "{mark}{} {UNIT} ({})", self.tokens, tier.label())
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.path.display(), self.reason)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn count(tokens: u64, method: Method) -> Count {
Count { tokens, method }
}
#[test]
fn the_estimate_wears_its_tilde() {
let shown = count(12, Method::Estimate).to_string();
assert!(shown.starts_with('~'), "{shown}");
assert!(shown.contains(UNIT), "{shown}");
}
#[test]
fn a_real_count_names_its_encoding() {
let shown = count(12, Method::Bpe).to_string();
assert!(!shown.contains('~'), "{shown}");
assert!(shown.contains("o200k"), "{shown}");
}
#[test]
fn the_two_tiers_never_render_alike() {
assert_ne!(
count(12, Method::Estimate).to_string(),
count(12, Method::Bpe).to_string()
);
}
#[test]
fn a_failure_names_the_path_and_the_reason() {
let err = Error {
path: "a/b.rs".into(),
reason: "nope".to_owned(),
};
assert_eq!(err.to_string(), "a/b.rs: nope");
}
}