Skip to content

Commit

Permalink
Merge pull request #167 from LesnyRumcajs/add-compression-from-str
Browse files Browse the repository at this point in the history
implement FromStr for CompressionType
  • Loading branch information
arkpar authored Dec 14, 2022
2 parents 86f5825 + 352aa21 commit ca1625a
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

//! Compression utility and types.

use std::str::FromStr;

use crate::error::Result;

/// Different compression type
Expand Down Expand Up @@ -73,6 +75,19 @@ impl From<&Compress> for CompressionType {
}
}

impl FromStr for CompressionType {
type Err = crate::Error;

fn from_str(s: &str) -> Result<Self> {
match s.to_lowercase().as_str() {
"none" => Ok(CompressionType::NoCompression),
"lz4" => Ok(CompressionType::Lz4),
"snappy" => Ok(CompressionType::Snappy),
_ => Err(crate::Error::Compression),
}
}
}

impl Compress {
pub fn compress(&self, buf: &[u8]) -> Vec<u8> {
match &self.inner {
Expand Down Expand Up @@ -177,4 +192,24 @@ mod tests {
assert_eq!(original, round_tripped);
}
}

#[test]
fn test_compression_from_str() {
let correct_cases = [
("lz4", CompressionType::Lz4),
("snappy", CompressionType::Snappy),
("none", CompressionType::NoCompression),
("SNAPPy", CompressionType::Snappy),
];

for (input, expected_compression) in correct_cases {
assert_eq!(expected_compression, CompressionType::from_str(input).unwrap());
}

let invalid_cases = ["lz5", "", "cthulhu"];

for input in invalid_cases {
assert!(CompressionType::from_str(input).is_err());
}
}
}

0 comments on commit ca1625a

Please sign in to comment.