const MAX_BROKER_STR: usize = 256;
pub(crate) fn truncate_broker_str(s: &str) -> &str {
if s.len() <= MAX_BROKER_STR {
return s;
}
let mut end = MAX_BROKER_STR;
while !s.is_char_boundary(end) {
end -= 1;
}
&s[..end]
}
#[cfg(test)]
mod tests {
use super::truncate_broker_str;
#[test]
fn short_strings_pass_through() {
assert_eq!(
truncate_broker_str("pulsar://broker:6650"),
"pulsar://broker:6650"
);
}
#[test]
fn long_strings_are_truncated_to_budget() {
let long = "x".repeat(1000);
assert_eq!(truncate_broker_str(&long).len(), 256);
}
#[test]
fn truncation_respects_char_boundaries() {
let long = "é".repeat(200);
let cut = truncate_broker_str(&long);
assert!(cut.len() <= 256);
assert!(long.is_char_boundary(cut.len()));
}
}