Skip to content

Commit e23d5bc

Browse files
cslinwangimeoer
authored andcommitted
fix: #1644 and #1651 resolve Algorithm to_string and FromStr inconsistency
Signed-off-by: Lin Wang <l.wang@mail.dlut.edu.cn>
1 parent acdf021 commit e23d5bc

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

utils/src/compress/mod.rs

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ pub enum Algorithm {
2929

3030
impl fmt::Display for Algorithm {
3131
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
32-
write!(f, "{:?}", self)
32+
let output = match self {
33+
Algorithm::None => "none",
34+
Algorithm::Lz4Block => "lz4_block",
35+
Algorithm::GZip => "gzip",
36+
Algorithm::Zstd => "zstd",
37+
};
38+
write!(f, "{}", output)
3339
}
3440
}
3541

@@ -250,6 +256,7 @@ mod tests {
250256
use std::fs::OpenOptions;
251257
use std::io::{Seek, SeekFrom};
252258
use std::path::Path;
259+
use std::str::FromStr;
253260
use vmm_sys_util::tempfile::TempFile;
254261

255262
#[test]
@@ -593,4 +600,46 @@ mod tests {
593600
assert!(!Algorithm::GZip.is_none());
594601
assert!(!Algorithm::Zstd.is_none());
595602
}
603+
604+
#[test]
605+
fn test_algorithm_to_string() {
606+
assert_eq!(Algorithm::None.to_string(), "none");
607+
assert_eq!(Algorithm::Lz4Block.to_string(), "lz4_block");
608+
assert_eq!(Algorithm::GZip.to_string(), "gzip");
609+
assert_eq!(Algorithm::Zstd.to_string(), "zstd");
610+
}
611+
612+
#[test]
613+
fn test_algorithm_from_str() {
614+
assert_eq!(Algorithm::from_str("none").unwrap(), Algorithm::None);
615+
assert_eq!(
616+
Algorithm::from_str("lz4_block").unwrap(),
617+
Algorithm::Lz4Block
618+
);
619+
assert_eq!(Algorithm::from_str("gzip").unwrap(), Algorithm::GZip);
620+
assert_eq!(Algorithm::from_str("zstd").unwrap(), Algorithm::Zstd);
621+
}
622+
623+
#[test]
624+
fn test_algorithm_to_string_and_from_str_consistency() {
625+
let algorithms = vec![
626+
Algorithm::None,
627+
Algorithm::Lz4Block,
628+
Algorithm::GZip,
629+
Algorithm::Zstd,
630+
];
631+
632+
for algo in algorithms {
633+
let stringified = algo.to_string();
634+
let parsed = Algorithm::from_str(&stringified).unwrap();
635+
assert_eq!(algo, parsed, "Mismatch for algorithm: {:?}", algo);
636+
}
637+
}
638+
639+
#[test]
640+
fn test_algorithm_from_str_invalid_input() {
641+
assert!(Algorithm::from_str("invalid_algorithm").is_err());
642+
assert!(Algorithm::from_str("GZIP").is_err());
643+
assert!(Algorithm::from_str("LZ4_BLOCK").is_err());
644+
}
596645
}

0 commit comments

Comments
 (0)