use std::borrow::Cow;
use std::mem;
use serde::Serialize;
use serde_json::{Map, Value};
use crate::host;
use super::Limits;
pub(super) fn truncate_args(mut call: host::Call, limits: &Limits) -> host::Call {
let mut chars_left = limits.output_chars / limits.calls_per_execute.max(1);
let mut kept = Vec::new();
let mut dropped = Map::new();
for (arg, value) in mem::take(&mut call.args) {
let arg_chars = arg.chars().count();
if dropped.is_empty()
&& let Some(value_share) = chars_left.checked_sub(arg_chars)
{
let text = to_text(&value);
let text_chars = text.chars().count();
let truncated = truncate_text(&text, text_chars, value_share);
drop(text);
let value_chars = truncated
.as_ref()
.map_or(text_chars, |text| text.chars().count());
if value_chars <= value_share {
chars_left = value_share - value_chars;
kept.push((arg, value, truncated, arg_chars + value_chars));
continue;
}
}
dropped.insert(arg, value);
}
if !dropped.is_empty() {
let mut text_chars = count_json_chars(&dropped);
while text_chars > chars_left
&& chars_left
< format_truncation_marker(text_chars, text_chars)
.chars()
.count()
&& let Some((arg, value, _, used_chars)) = kept.pop()
{
text_chars += count_json_chars(&arg) + ":".len() + count_json_chars(&value) + ",".len();
chars_left += used_chars;
dropped.insert(arg, value);
}
let text = serde_json::to_string(&dropped).expect("a Map always serializes");
call.truncated_args = Some(truncate_text(&text, text_chars, chars_left).unwrap_or(text));
}
call.args = kept
.into_iter()
.map(|(arg, value, truncated, _)| (arg, truncated.map_or(value, Value::String)))
.collect();
call
}
pub(super) fn to_text(value: &Value) -> Cow<'_, str> {
match value {
Value::String(text) => Cow::Borrowed(text),
other => Cow::Owned(serde_json::to_string(other).expect("a Value always serializes")),
}
}
pub(super) fn truncate_text(text: &str, text_chars: usize, max_chars: usize) -> Option<String> {
if text_chars <= max_chars {
return None;
}
let longest_marker = format_truncation_marker(text_chars, text_chars);
let kept_chars = max_chars.saturating_sub(longest_marker.chars().count());
let tail_chars = kept_chars / 2;
let head_end = text
.char_indices()
.nth(kept_chars - tail_chars)
.map_or(text.len(), |(byte, _)| byte);
let tail_start = text
.char_indices()
.rev()
.take(tail_chars)
.last()
.map_or(text.len(), |(byte, _)| byte);
let head = &text[..head_end];
let tail = &text[tail_start..];
let marker = format_truncation_marker(text_chars - kept_chars, text_chars);
Some(format!("{head}{marker}{tail}"))
}
fn format_truncation_marker(truncated_chars: usize, text_chars: usize) -> String {
let truncated_count = group_thousands(truncated_chars);
let total_count = group_thousands(text_chars);
format!("[… {truncated_count} of {total_count} characters truncated …]")
}
fn count_json_chars(value: &impl Serialize) -> usize {
serde_json::to_string(value)
.expect("a Map, Value or String always serializes")
.chars()
.count()
}
fn group_thousands(number: usize) -> String {
let digits = number.to_string();
let mut grouped = String::new();
for (index, digit) in digits.chars().enumerate() {
if index > 0 && (digits.len() - index).is_multiple_of(3) {
grouped.push(',');
}
grouped.push(digit);
}
grouped
}
pub(super) fn split_budget(
budget: usize,
first_chars: usize,
second_chars: usize,
) -> (usize, usize) {
let second_share = budget
.saturating_sub(first_chars)
.max(budget / 2)
.min(second_chars);
(budget - second_share, second_share)
}