kanidm_proto/
backup.rs

1//! Relates to backup functionality in the Server
2use std::{fmt::Display, path::Path};
3
4use serde::Deserialize;
5
6#[derive(Default, Deserialize, Debug, Clone, Copy)]
7pub enum BackupCompression {
8    NoCompression,
9    #[default]
10    Gzip,
11}
12
13impl BackupCompression {
14    pub fn suffix(&self) -> &'static str {
15        match self {
16            BackupCompression::NoCompression => "",
17            BackupCompression::Gzip => ".gz",
18        }
19    }
20
21    pub fn identify_file(filepath: &Path) -> Self {
22        let filename = filepath.file_name().and_then(|s| s.to_str()).unwrap_or("");
23        if filename.ends_with(".gz") {
24            BackupCompression::Gzip
25        } else {
26            BackupCompression::NoCompression
27        }
28    }
29}
30
31impl Display for BackupCompression {
32    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
33        match self {
34            BackupCompression::NoCompression => write!(f, "No Compression"),
35            BackupCompression::Gzip => write!(f, "Gzip"),
36        }
37    }
38}
39
40impl From<Option<String>> for BackupCompression {
41    fn from(opt: Option<String>) -> Self {
42        match opt {
43            Some(s) => BackupCompression::from(s),
44            None => BackupCompression::default(),
45        }
46    }
47}
48
49impl From<String> for BackupCompression {
50    fn from(s: String) -> Self {
51        match s.to_lowercase().as_str() {
52            "gzip" => BackupCompression::Gzip,
53            "none" | "nocompression" => BackupCompression::NoCompression,
54            _ => {
55                eprintln!(
56                    "Unknown compression type '{}', defaulting to {}",
57                    s,
58                    BackupCompression::default()
59                );
60                BackupCompression::default()
61            }
62        }
63    }
64}