Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Compression to_string and into string impls #175

Merged
merged 3 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 67 additions & 5 deletions cassandra-protocol/src/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,27 @@ impl From<String> for Compression {
}
}

impl Compression {
/// It converts `Compression` into `String`. If compression is `None` then empty string will be
krojew marked this conversation as resolved.
Show resolved Hide resolved
/// returned
pub fn to_protocol_string(self) -> String {
match self {
Compression::Lz4 => "LZ4".to_string(),
Compression::Snappy => "SNAPPY".to_string(),
Compression::None => "NONE".to_string(),
}
}

pub fn from_protocol_string(protocol_string: &str) -> std::result::Result<Self, String> {
match protocol_string {
"lz4" | "LZ4" => Ok(Compression::Lz4),
"snappy" | "SNAPPY" => Ok(Compression::Snappy),
"none" | "NONE" => Ok(Compression::None),
_ => Err("Unknown compression".to_string()),
}
}
}

impl<'a> From<&'a str> for Compression {
/// It converts `str` into `Compression`. If string is neither `lz4` nor `snappy` then
/// `Compression::None` will be returned
Expand All @@ -186,13 +207,54 @@ mod tests {
use super::*;

#[test]
fn test_compression_from_str() {
fn test_compression_to_protocol_string() {
let lz4 = Compression::Lz4;
assert_eq!("LZ4", lz4.to_protocol_string());

let snappy = Compression::Snappy;
assert_eq!("SNAPPY", snappy.to_protocol_string());

let none = Compression::None;
assert_eq!("NONE", none.to_protocol_string());
}

#[test]
fn test_compression_from_protocol_str() {
let lz4 = "lz4";
assert_eq!(Compression::from(lz4), Compression::Lz4);
assert_eq!(
Compression::from_protocol_string(lz4).unwrap(),
Compression::Lz4
);

let lz4 = "LZ4";
assert_eq!(
Compression::from_protocol_string(lz4).unwrap(),
Compression::Lz4
);

let snappy = "snappy";
assert_eq!(Compression::from(snappy), Compression::Snappy);
let none = "x";
assert_eq!(Compression::from(none), Compression::None);
assert_eq!(
Compression::from_protocol_string(snappy).unwrap(),
Compression::Snappy
);

let snappy = "SNAPPY";
assert_eq!(
Compression::from_protocol_string(snappy).unwrap(),
Compression::Snappy
);

let none = "none";
assert_eq!(
Compression::from_protocol_string(none).unwrap(),
Compression::None
);

let none = "NONE";
assert_eq!(
Compression::from_protocol_string(none).unwrap(),
Compression::None
);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion cdrs-tokio/tests/compression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async fn encode_decode_test(version: Version) {
.unwrap()
.into_rows()
.unwrap()
.get(0)
.first()
.unwrap()
.r_by_index::<Blob>(0)
.unwrap();
Expand Down
Loading