use std::borrow::Cow;
use std::fmt::Display;
use std::fmt::Formatter;
use std::fmt::Result;
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Default)]
pub struct Capture(Vec<u8>);
impl Capture {
pub fn new(bytes: impl Into<Vec<u8>>) -> Self {
Self(bytes.into())
}
pub fn get(&self) -> &[u8] {
&self.0
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn to_string_lossy(&self) -> Cow<'_, str> {
String::from_utf8_lossy(&self.0)
}
}
impl Display for Capture {
fn fmt(&self, formatter: &mut Formatter<'_>) -> Result {
formatter.write_str(&self.to_string_lossy())
}
}
impl From<Vec<u8>> for Capture {
fn from(bytes: Vec<u8>) -> Self {
Self::new(bytes)
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::missing_panics_doc)]
use super::*;
#[test]
fn capture_is_send_and_sync() {
fn assert_send_and_sync<T: Send + Sync>() {}
assert_send_and_sync::<Capture>();
}
#[test]
fn is_empty_without_bytes_reports_an_empty_capture() {
let capture = Capture::default();
assert!(capture.is_empty());
}
#[test]
fn to_string_returns_the_capture_as_text() {
let capture = Capture::new("hello");
assert_eq!(capture.to_string(), "hello");
}
}