use crate::{Error, Result};
use std::path::Path;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CaptureKind {
Text,
Binary,
Image,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Capture {
pub kind: CaptureKind,
pub path: Option<String>,
pub bytes: Vec<u8>,
}
impl Capture {
pub fn from_bytes(kind: CaptureKind, bytes: Vec<u8>) -> Self {
Self {
kind,
path: None,
bytes,
}
}
pub fn from_path(path: impl AsRef<Path>) -> Result<Self> {
let path = path.as_ref();
let bytes = std::fs::read(path).map_err(|e| Error::IoError(e.to_string()))?;
let kind = match path
.extension()
.and_then(|e| e.to_str())
.unwrap_or("")
.to_ascii_lowercase()
.as_str()
{
"dsk" => CaptureKind::Binary,
"png" | "jpg" | "jpeg" | "webp" => CaptureKind::Image,
_ => CaptureKind::Text,
};
Ok(Self {
kind,
path: Some(path.display().to_string()),
bytes,
})
}
}