1use std::fmt;
4
5use base64::prelude::{BASE64_STANDARD, Engine};
6
7pub const MAX_COPY_BASE64: usize = 100_000;
9
10#[derive(Debug, PartialEq, Eq)]
12pub struct TooLarge(pub usize);
13
14impl fmt::Display for TooLarge {
15 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
16 write!(
17 f,
18 "CSV too large to copy ({} bytes encoded, limit {MAX_COPY_BASE64})",
19 self.0
20 )
21 }
22}
23
24pub fn osc52(text: &str) -> Result<Vec<u8>, TooLarge> {
29 let encoded = BASE64_STANDARD.encode(text);
30 if encoded.len() > MAX_COPY_BASE64 {
31 return Err(TooLarge(encoded.len()));
32 }
33 Ok(format!("\x1b]52;c;{encoded}\x07").into_bytes())
34}
35
36#[cfg(test)]
37mod tests;