mithril_common/entities/
compression_algorithm.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
use serde::{Deserialize, Serialize};
use strum::{Display, EnumIter, IntoEnumIterator};

/// Compression algorithm for the snapshot archive artifacts.
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, Default, EnumIter, Display)]
#[serde(rename_all = "lowercase")]
pub enum CompressionAlgorithm {
    /// Gzip compression format
    #[default]
    Gzip,
    /// Zstandard compression format
    Zstandard,
}

impl CompressionAlgorithm {
    /// Get the extension associated to tar archive using the current algorithm.
    pub fn tar_file_extension(&self) -> String {
        match self {
            CompressionAlgorithm::Gzip => "tar.gz".to_owned(),
            CompressionAlgorithm::Zstandard => "tar.zst".to_owned(),
        }
    }

    /// List all the available [algorithms][CompressionAlgorithm].
    pub fn list() -> Vec<Self> {
        Self::iter().collect()
    }

    /// Those ratio will be multiplied by the snapshot size to check if the available
    /// disk space is sufficient to store the archive plus the extracted files.
    /// If the available space is lower than that, a warning is raised.
    /// Those ratio have been experimentally established.
    pub fn free_space_snapshot_ratio(&self) -> f64 {
        match self {
            CompressionAlgorithm::Gzip => 2.5,
            CompressionAlgorithm::Zstandard => 4.0,
        }
    }
}