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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
use memedb_core;
use rayon::prelude::*;
use std::collections::HashSet;
use std::fs;
//use std::iter::FromIterator;
use std::path::{Path, PathBuf};

extern crate rayon;

pub fn read_file_tags(path: &Path) -> HashSet<String> {
    memedb_core::read_tags(path).unwrap()
}

pub fn read_files_tags(files: HashSet<&Path>) -> HashSet<String> {
    files
        .into_par_iter()
        .map(|path| memedb_core::read_tags(path).unwrap())
        .flatten()
        .collect()
}

pub fn read_dir_tags(dir_path: &Path) -> HashSet<String> {
    fs::read_dir(dir_path)
        .unwrap()
        .map(|file| file.unwrap().path())
        .collect::<HashSet<_>>()
        .par_iter()
        .map(|path| memedb_core::read_tags(path.as_path()).unwrap())
        .flatten()
        .collect()
}

pub fn write_file_tags(path: &Path, tags: HashSet<String>) -> HashSet<String> {
    memedb_core::write_tags(path, &tags).unwrap();
    memedb_core::read_tags(path).unwrap()
}

pub fn add_file_tags(path: &Path, tags: HashSet<String>) -> HashSet<String> {
    let mut actual_tags: HashSet<String> = memedb_core::read_tags(path).unwrap();
    actual_tags.par_extend(tags);
    memedb_core::write_tags(path, &actual_tags).unwrap();
    memedb_core::read_tags(path).unwrap()
}

pub fn delete_file_tags(path: &Path, tags: HashSet<String>) -> HashSet<String> {
    let actual_tags = memedb_core::read_tags(path)
        .unwrap()
        .into_par_iter()
        .filter(|tag| !tags.contains(tag))
        .collect();
    memedb_core::write_tags(path, &actual_tags).unwrap();
    memedb_core::read_tags(path).unwrap()
}

pub fn read_files_by_tags(files: HashSet<&Path>, tags: HashSet<String>) -> HashSet<&Path> {
    files
        .into_par_iter()
        .filter(|path| {
            memedb_core::read_tags(path)
                .unwrap()
                .par_iter()
                .any(|tag| tags.contains(tag))
        })
        .collect()
}

pub fn read_dir_by_tags(dir_path: &Path, tags: HashSet<String>) -> HashSet<PathBuf> {
    fs::read_dir(dir_path)
        .unwrap()
        .map(|file| file.unwrap().path())
        .filter(|file| {
            memedb_core::read_tags(file.as_path())
                .unwrap()
                .par_iter()
                .any(|tag| tags.contains(tag))
        })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_read_tags() {
        let path = Path::new("tests/3.jpg");
        let mut res = HashSet::new();
        res.insert("lol".to_string());
        res.insert("xd".to_string());
        assert_eq!(read_file_tags(path), res)
    }

    #[test]
    fn test_read_all_tags() {
        let path = Path::new("tests");
        let mut res = HashSet::new();
        res.insert("lol".to_string());
        //res.insert("xd".to_string());
        assert_eq!(read_dir_tags(path), res)
    }

    #[test]
    fn test_write_tags() {
        let path = Path::new("tests/4.png");
        let mut tags = HashSet::new();
        let mut res = HashSet::new();
        tags.insert("lol".to_string());
        tags.insert("xd".to_string());
        res.insert("lol".to_string());
        res.insert("xd".to_string());
        write_file_tags(path, tags);
        assert_eq!(read_file_tags(path), res)
    }

    #[test]
    fn test_add_tags() {
        let path = Path::new("tests/4.png");
        let res = read_file_tags(path);
        let mut tags = HashSet::new();
        tags.insert("ahh".to_string());
        tags.insert("xd".to_string());
        add_file_tags(path, tags);
        assert_eq!(read_file_tags(path), res)
    }

    #[test]
    fn test_delete_tags() {
        let path = Path::new("tests/4.png");
        let res = read_file_tags(path);
        let mut tags = HashSet::new();
        tags.insert("ahh".to_string());
        tags.insert("xd".to_string());
        delete_file_tags(path, tags);
        assert_eq!(read_file_tags(path), res)
    }

    #[test]
    fn test_read_files_by_tags() {
        let mut files = HashSet::new();
        let mut tags = HashSet::new();
        let mut dir_res = HashSet::new();
        tags.insert("xd".to_string());
        tags.insert("ahh".to_string());
        files.insert(Path::new("tests\\3.jpg"));
        files.insert(Path::new("tests\\4.png"));
        dir_res.insert(Path::new("tests\\3.jpg"));
        dir_res.insert(Path::new("tests\\4.png"));
        assert_eq!(read_files_by_tags(files, tags), dir_res)
    }
}