Skip to main content

truncate_for_diagnostic

Function truncate_for_diagnostic 

Source
pub fn truncate_for_diagnostic(value: &str, max_chars: usize) -> Cow<'_, str>
Expand description

Truncates value to at most max_chars characters, appending when truncated.

So an attacker-controlled string interpolated into a diagnostic message can never render an unbounded amount of text inline in the editor. Counts characters, not bytes, so a truncation point never lands mid-character (value may contain multi-byte UTF-8). pub, not module-private: deps-github-actions’ mutable-ref-pin diagnostic (issue #473) reuses this same chokepoint for its own attacker-controlled name/tag interpolation, rather than duplicating the truncation logic in a second crate.

§Examples

use deps_core::lsp_helpers::truncate_for_diagnostic;

assert_eq!(truncate_for_diagnostic("short", 128), "short");
assert_eq!(truncate_for_diagnostic(&"a".repeat(200), 5), "aaaaa…");