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
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
//! Provides configuration options for a library, both programmatically and via config files.

pub mod meta_format;
pub mod selection;
pub mod sorter;

use self::meta_format::MetaFormat;
use self::selection::Selection;
use self::sorter::Sorter;

use crate::metadata::target::Target;

#[derive(Deserialize)]
#[serde(default)]
pub struct Config {
    #[serde(flatten)] pub selection: Selection,
    #[serde(flatten)] pub sorter: Sorter,
    pub item_fn: String,
    pub self_fn: String,
    pub meta_format: MetaFormat,
}

impl Default for Config {
    fn default() -> Self {
        // TODO: Is there a way to intelligently populate this while also
        //       preserving defaulting behavior?
        let selection = Selection::default();
        let sorter = Sorter::default();
        let meta_format = MetaFormat::default();
        let item_fn = format!(
            "{}.{}",
            Target::Siblings.default_file_name(),
            meta_format.file_extension(),
        );
        let self_fn = format!(
            "{}.{}",
            Target::Parent.default_file_name(),
            meta_format.file_extension(),
        );

        Self {
            selection,
            sorter,
            item_fn,
            self_fn,
            meta_format,
        }
    }
}

#[cfg(test)]
mod tests {
    use serde_yaml;

    use super::*;

    use super::sorter::sort_by::SortBy;

    #[test]
    fn test_deserialization() {
        let text_config = r#"
            include_files: '*.flac'
            sort_by: name
        "#;

        let config: Config = serde_yaml::from_str(&text_config).unwrap();

        assert_eq!(config.selection.is_file_pattern_match("music.flac"), true);
        assert_eq!(config.selection.is_file_pattern_match("music.mp3"), false);
        assert_eq!(config.selection.is_file_pattern_match("photo.png"), false);
        assert_eq!(config.selection.is_file_pattern_match("self.yml"), false);
        assert_eq!(config.selection.is_file_pattern_match("item.yml"), false);
        assert_eq!(config.sorter.sort_by, SortBy::Name);
        assert_eq!(config.item_fn, "item.yml");
        assert_eq!(config.self_fn, "self.yml");
        assert_eq!(config.meta_format, MetaFormat::Yaml);

        let text_config = r#"
            include_files:
                - '*.flac'
                - '*.mp3'
            sort_by: mod_time
        "#;

        let config: Config = serde_yaml::from_str(&text_config).unwrap();

        assert_eq!(config.selection.is_file_pattern_match("music.flac"), true);
        assert_eq!(config.selection.is_file_pattern_match("music.mp3"), true);
        assert_eq!(config.selection.is_file_pattern_match("photo.png"), false);
        assert_eq!(config.sorter.sort_by, SortBy::ModTime);
        assert_eq!(config.item_fn, "item.yml");
        assert_eq!(config.self_fn, "self.yml");
        assert_eq!(config.meta_format, MetaFormat::Yaml);

        let text_config = r#"
            include_files: '*'
            sort_by: mod_time
        "#;

        let config: Config = serde_yaml::from_str(&text_config).unwrap();

        assert_eq!(config.selection.is_file_pattern_match("music.flac"), true);
        assert_eq!(config.selection.is_file_pattern_match("music.mp3"), true);
        assert_eq!(config.selection.is_file_pattern_match("photo.png"), true);
        assert_eq!(config.sorter.sort_by, SortBy::ModTime);
        assert_eq!(config.item_fn, "item.yml");
        assert_eq!(config.self_fn, "self.yml");
        assert_eq!(config.meta_format, MetaFormat::Yaml);

        let text_config = r#"
            include_files: '*'
            exclude_files: '*.mp3'
            sort_by: name
            item_fn: item_meta.yml
            meta_format: yaml
        "#;

        let config: Config = serde_yaml::from_str(&text_config).unwrap();

        assert_eq!(config.selection.is_file_pattern_match("music.flac"), true);
        assert_eq!(config.selection.is_file_pattern_match("music.mp3"), false);
        assert_eq!(config.selection.is_file_pattern_match("photo.png"), true);
        assert_eq!(config.sorter.sort_by, SortBy::Name);
        assert_eq!(config.item_fn, "item_meta.yml");
        assert_eq!(config.self_fn, "self.yml");
        assert_eq!(config.meta_format, MetaFormat::Yaml);
    }
}