pub mod frontmatter;
pub fn truncate_at_char_boundary(s: &str, max_bytes: usize) -> &str {
if s.len() <= max_bytes {
return s;
}
let mut end = max_bytes;
while end > 0 && !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
pub fn truncate_chars(s: &str, max: usize, marker: &str) -> String {
if s.chars().count() <= max {
s.to_string()
} else {
let head: String = s.chars().take(max).collect();
format!("{head}{marker}")
}
}
pub fn scrub_secrets(text: &str) -> String {
const MASK: &str = "***";
const SECRET_NAMES: &[&str] = &["token", "key", "secret", "passw", "auth", "credential"];
let is_boundary =
|c: char| c.is_whitespace() || matches!(c, '"' | '\'' | '&' | '?' | ',' | ';');
let mut out = String::with_capacity(text.len());
let mut after_scheme = false;
let mut rest = text;
while !rest.is_empty() {
let end = rest.find(is_boundary).unwrap_or(rest.len());
let (word, tail) = rest.split_at(end);
let masked = if word.is_empty() {
None
} else if after_scheme {
Some(MASK.to_string())
} else if let Some((name, value)) = word.split_once('=') {
let lower = name.to_ascii_lowercase();
(!value.is_empty() && SECRET_NAMES.iter().any(|n| lower.contains(n)))
.then(|| format!("{name}={MASK}"))
} else if (word.starts_with("sk-") || word.starts_with("sk_")) && word.len() >= 10 {
Some(MASK.to_string())
} else {
None
};
if !word.is_empty() {
after_scheme = matches!(word, "Bearer" | "bearer" | "Basic" | "basic");
}
out.push_str(masked.as_deref().unwrap_or(word));
let sep_len = tail.chars().next().map_or(0, char::len_utf8);
out.push_str(&tail[..sep_len]);
rest = &tail[sep_len..];
}
out
}
pub fn scrub_url(text: &str) -> String {
let scrubbed = scrub_secrets(text);
let mut out = String::with_capacity(scrubbed.len());
let mut rest = scrubbed.as_str();
while let Some(at) = rest.find("://") {
let (head, tail) = rest.split_at(at + 3);
out.push_str(head);
let end = tail
.find(|c: char| matches!(c, '/' | '?' | '#') || c.is_whitespace())
.unwrap_or(tail.len());
let (authority, after) = tail.split_at(end);
match authority
.rsplit_once('@')
.and_then(|(userinfo, host)| Some((userinfo.split_once(':')?.0, host)))
{
Some((user, host)) => out.push_str(&format!("{user}:***@{host}")),
None => out.push_str(authority),
}
rest = after;
}
out.push_str(rest);
out
}
#[cfg(test)]
mod tests {
use super::truncate_at_char_boundary as t;
#[test]
fn shorter_than_budget_is_returned_whole() {
assert_eq!(t("hello", 10), "hello");
assert_eq!(t("中文", 100), "中文");
assert_eq!(t("", 0), "");
}
#[test]
fn ascii_cuts_exactly_at_the_budget() {
assert_eq!(t("hello world", 5), "hello");
}
#[test]
fn every_byte_offset_of_a_multibyte_string_is_safe() {
let s = "中文测试abc混合デ";
for n in 0..=s.len() + 4 {
let out = t(s, n);
assert!(
out.len() <= n.min(s.len()),
"n={n} produced {} bytes",
out.len()
);
assert!(s.starts_with(out), "n={n} produced a non-prefix");
}
}
#[test]
fn cut_moves_backwards_never_forwards() {
assert_eq!(t("中文测试", 5), "中");
assert_eq!(t("中文测试", 6), "中文");
assert_eq!(t("中文", 0), "");
assert_eq!(t("中文", 2), "");
}
#[test]
fn combining_marks_and_emoji_survive() {
let s = "e\u{301}👨👩👧"; for n in 0..=s.len() {
let out = t(s, n);
assert!(s.starts_with(out));
}
}
#[test]
fn truncate_long_string() {
let s: String = "x".repeat(3000);
let t = super::truncate_chars(&s, 100, "[truncated]");
assert_eq!(t.chars().count(), 100 + "[truncated]".chars().count());
assert!(t.ends_with("[truncated]"));
}
#[test]
fn truncate_short_string_is_passthrough() {
let t = super::truncate_chars("hi", 10, "[truncated]");
assert_eq!(t, "hi");
}
#[test]
fn scrub_secrets_masks_keys_bearers_and_secret_named_values() {
use super::scrub_secrets;
let line = "got sk-abcdef1234567890 and Authorization: Bearer abc.def \
from https://x.test/cb?token=hunter2&page=2 --api-key=zzz";
let out = scrub_secrets(line);
for leaked in ["abcdef1234567890", "abc.def", "hunter2", "zzz"] {
assert!(!out.contains(leaked), "{leaked} in {out}");
}
assert!(
out.contains("page=2") && out.contains("https://x.test/cb"),
"{out}"
);
}
}