Coverage Report

Created: 2024-02-20 21:15

/builds/xfbs/cindy/src/config.rs
Line
Count
Source
1
use crate::hash::Hash;
2
use serde::{Deserialize, Serialize};
3
use std::path::PathBuf;
4
5
42
#[derive(
S26
erializ
e26
,
D24
eserialize,
Clone29
,
Debug1
,
P5
artialE
q5
,
Default29
)]
6
pub struct Config {
7
    pub index: IndexConfig,
8
    pub thumbs: ThumbsConfig,
9
    pub data: DataConfig,
10
}
11
12
42
#[derive(
S26
erializ
e26
,
D24
eserialize,
Clone29
,
P5
artialE
q5
,
Debug1
)]
13
pub struct DataConfig {
14
    pub path: PathBuf,
15
    pub hash: HashAlgorithm,
16
    pub prefix: Vec<u8>,
17
}
18
19
29
#[derive(
S26
erializ
e26
,
D12
eserializ
e18
, Clone,
PartialEq5
,
Debug1
)]
20
pub struct IndexConfig {
21
    pub path: PathBuf,
22
}
23
24
29
#[derive(
S26
erializ
e26
,
D12
eserializ
e18
, Clone,
PartialEq5
,
Debug1
)]
25
pub struct ThumbsConfig {
26
    pub path: PathBuf,
27
}
28
29
57
#[derive(
S26
erializ
e26
,
D12
eserializ
e12
, Clone,
D1
ebu
g1
,
PartialEq5
,
Default30
)]
30
#[serde(rename_all = "snake_case")]
31
pub enum HashAlgorithm {
32
    #[default]
33
    Blake2b512,
34
    Blake2s256,
35
}
36
37
impl Default for IndexConfig {
38
29
    fn default() -> Self {
39
29
        Self {
40
29
            path: "index.db".into(),
41
29
        }
42
29
    }
43
}
44
45
impl Default for DataConfig {
46
30
    fn default() -> Self {
47
30
        Self {
48
30
            path: "data".into(),
49
30
            hash: Default::default(),
50
30
            prefix: vec![2, 2],
51
30
        }
52
30
    }
53
}
54
55
impl DataConfig {
56
41
    pub fn data_path(&self, hash: &Hash) -> PathBuf {
57
41
        let string = hash.to_string();
58
41
        let mut slice = &string[..];
59
41
        let mut path = self.path.clone();
60
123
        for 
length82
in &self.prefix {
61
82
            let (current, rest) = slice.split_at(*length as usize);
62
82
            path.push(current);
63
82
            slice = rest;
64
82
        }
65
41
        path.push(slice);
66
41
        path
67
41
    }
68
}
69
70
impl Default for ThumbsConfig {
71
29
    fn default() -> Self {
72
29
        Self {
73
29
            path: "thumbs".into(),
74
29
        }
75
29
    }
76
}
77
78
#[cfg(test)]
79
mod tests {
80
    use super::*;
81
    use std::path::Path;
82
83
1
    #[test]
84
1
    fn test_default() {
85
1
        let _default = Config::default();
86
1
    }
87
88
1
    #[test]
89
1
    fn test_clone() {
90
1
        let default = Config::default();
91
1
        let _cloned = default.clone();
92
1
    }
93
94
1
    #[test]
95
1
    fn test_debug() {
96
1
        let default = Config::default();
97
1
        format!("{default:?}");
98
1
    }
99
100
1
    #[test]
101
1
    fn test_serialize() {
102
1
        let config = Config::default();
103
1
        let config_string = toml::to_string(&config).unwrap();
104
1
        let config_parsed = toml::from_str(&config_string).unwrap();
105
1
        assert_eq!(config, config_parsed);
106
1
    }
107
108
1
    #[test]
109
1
    fn test_parse() {
110
1
        let config_str = r#"
111
1
[data]
112
1
path = "data"
113
1
hash = "blake2b512"
114
1
prefix = [2, 2]
115
1
116
1
[index]
117
1
path = "index.db"
118
1
119
1
[thumbs]
120
1
path = "thumbs"
121
1
        "#;
122
1
        let _config: Config = toml::from_str(config_str).unwrap();
123
1
    }
124
125
1
    #[test]
126
1
    fn test_data_path() {
127
1
        let data = DataConfig::default();
128
1
        let hash = Hash::new(&[0x9a, 0xbc, 0xde, 0xf0]);
129
1
        let path = data.data_path(hash);
130
1
        assert_eq!(path, Path::new("data/9a/bc/def0"));
131
1
    }
132
}