use std::io::{self, Write};
use std::net::{IpAddr, Ipv4Addr};
use std::sync::{Arc, Mutex};
use zond_engine::{Host, HostStatus};
#[derive(Clone, Default)]
pub(crate) struct Capture(Arc<Mutex<Vec<u8>>>);
impl Capture {
pub(crate) fn text(&self) -> String {
String::from_utf8(self.0.lock().expect("not poisoned").clone())
.expect("a renderer writes text")
}
}
impl Write for Capture {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.0.lock().expect("not poisoned").extend_from_slice(buf);
Ok(buf.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub(crate) fn host(last_octet: u8) -> Host {
let mut host = Host::new(IpAddr::V4(Ipv4Addr::new(192, 0, 2, last_octet)));
host.set_status(HostStatus::Up);
host
}