use crate::traits::{ConvertAsBytes, Parsable};
use anyhow::anyhow;
const LZMA_COMPRESSION: &str = "lzma";
const ZLIB_COMPRESSION: &str = "zlib";
#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PSArchiveCompression {
LZMA,
ZLIB,
ERROR,
}
impl Parsable for PSArchiveCompression {
type Error = anyhow::Error;
fn parse(bytes: impl ConvertAsBytes) -> Result<Self, Self::Error>
where
Self: Sized,
{
let bytes = bytes.convert_as_bytes();
if LZMA_COMPRESSION.convert_as_bytes() == bytes {
return Ok(Self::LZMA);
} else if ZLIB_COMPRESSION.convert_as_bytes() == bytes {
return Ok(Self::ZLIB);
}
return Err(anyhow!("Invalid compression type"));
}
}
impl Default for PSArchiveCompression {
fn default() -> Self {
return Self::ERROR;
}
}
impl std::fmt::Display for PSArchiveCompression {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::LZMA => {
write!(f, "lzma")
}
Self::ZLIB => {
write!(f, "zlib")
}
Self::ERROR => {
write!(f, "Error parsing Archive Compression")
}
}
}
}
#[cfg(test)]
#[doc(hidden)]
mod test {
use super::PSArchiveCompression;
use crate::prelude::*;
#[test]
fn test_compression_parsing_lzma() {
let bytes = "lzma".as_bytes();
let result = PSArchiveCompression::parse(bytes);
assert_eq!(result.is_ok(), true);
let result = result.unwrap();
assert_eq!(result, PSArchiveCompression::LZMA);
}
#[test]
fn test_compression_parsing_zlib() {
let bytes = "zlib".as_bytes();
let result = PSArchiveCompression::parse(bytes);
assert_eq!(result.is_ok(), true);
let result = result.unwrap();
assert_eq!(result, PSArchiveCompression::ZLIB);
}
#[test]
fn test_compression_parsing_error() {
let bytes = "nope".as_bytes();
let result = PSArchiveCompression::parse(bytes);
assert_eq!(result.is_ok(), false);
}
#[test]
fn test_compression_display() {
let bytes = "lzma".as_bytes();
let result = PSArchiveCompression::parse(bytes).unwrap();
assert_eq!(format!("{}", result), "lzma");
}
}