limnifs_write/config/
toml.rs1use std::path::Path;
4
5use crate::config::error::ConfigError;
6use crate::config::WriteConfig;
7
8impl WriteConfig {
9 pub fn from_toml(path: &Path) -> Result<Self, ConfigError> {
15 let contents = std::fs::read_to_string(path)?;
16 let config: Self = toml::from_str(&contents)?;
17 config.validate()?;
18 Ok(config)
19 }
20
21 pub fn to_toml(&self) -> Result<String, ConfigError> {
25 Ok(toml::to_string_pretty(self)?)
26 }
27
28 pub fn write_to_toml(&self, path: &Path) -> Result<(), ConfigError> {
33 let s = self.to_toml()?;
34 std::fs::write(path, s)?;
35 Ok(())
36 }
37}
38
39#[cfg(test)]
40mod tests {
41 use super::*;
42
43 #[test]
44 fn round_trip_default() {
45 let original = WriteConfig::default_v0_1();
46 let s = original.to_toml().expect("serialise");
47 let parsed: WriteConfig = toml::from_str(&s).expect("parse");
48 assert_eq!(parsed, original);
49 }
50
51 #[test]
52 fn parsed_with_all_fields() {
53 let toml = r#"
54[defaults]
55text_codec = "brotli"
56binary_codec = "lz4"
57metadata_codec = "brotli"
58metadata_quality = 5
59max_drop_size = 1048576
60inline_threshold = 4096
61
62[[categorizer]]
63name = "dna"
64extensions = ["fasta", "fa"]
65codec = "glza"
66max_size = 524288
67enabled = true
68
69[chunking]
70avg_chunk_size = 8192
71min_chunk_size = 1024
72max_chunk_size = 65536
73
74[tournament]
75codecs = ["store", "lz4", "zstd", "brotli"]
76min_size_threshold = 256
77skip_for_binary = true
78
79[encryption]
80aead = "chacha20-poly1305"
81key_wrap = "x25519-hkdf"
82
83[dictionaries]
84enabled = true
85min_class_size = 100
86max_dict_size = 65536
87"#;
88 let config: WriteConfig = toml::from_str(toml).expect("parse");
89 config.validate().expect("validate");
90 assert_eq!(config.categorizers.len(), 1);
91 assert_eq!(config.categorizers[0].name, "dna");
92 assert_eq!(config.categorizers[0].max_size, Some(524_288));
93 assert_eq!(config.defaults.max_drop_size, 1_048_576);
94 }
95
96 #[test]
97 fn defaults_resolved_correctly() {
98 let config = WriteConfig::default_v0_1();
99 assert_eq!(config.text_codec_id().unwrap(), 0x04);
100 assert_eq!(config.binary_codec_id().unwrap(), 0x01);
101 }
102}