use std::io::{self, Write};
pub const REQUEST_DRAG_DROP: &[u8] = b"\x1b]72;t=q\x1b\\";
pub fn write_drag_drop<W: Write>(w: &mut W, options: &[&str], payload: &[u8]) -> io::Result<()> {
w.write_all(b"\x1b]72;")?;
for (index, option) in options.iter().enumerate() {
if index != 0 {
w.write_all(b":")?;
}
w.write_all(option.as_bytes())?;
}
if !payload.is_empty() {
w.write_all(b";")?;
w.write_all(payload)?;
}
w.write_all(b"\x1b\\")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn query_and_raw_chunks() {
let mut bytes = Vec::new();
write_drag_drop(&mut bytes, &["t=q"], b"").unwrap();
assert_eq!(bytes, REQUEST_DRAG_DROP);
bytes.clear();
write_drag_drop(&mut bytes, &["t=r", "x=1", "m=1"], b"YW").unwrap();
write_drag_drop(&mut bytes, &["m=0"], b"Jj").unwrap();
write_drag_drop(&mut bytes, &["t=r", "x=1"], b"").unwrap();
assert_eq!(
bytes,
b"\x1b]72;t=r:x=1:m=1;YW\x1b\\\x1b]72;m=0;Jj\x1b\\\x1b]72;t=r:x=1\x1b\\"
);
}
#[test]
fn verbatim_payload_and_output_errors() {
let mut bytes = Vec::new();
let payload = [b'A'; 4097];
write_drag_drop(&mut bytes, &["t=p", "x=0"], &payload).unwrap();
assert_eq!(
bytes,
[b"\x1b]72;t=p:x=0;".as_slice(), &payload, b"\x1b\\"].concat()
);
assert_eq!(
write_drag_drop(&mut [].as_mut_slice(), &["t=q"], b"")
.unwrap_err()
.kind(),
io::ErrorKind::WriteZero
);
}
}