sagiegurari / cargo-make
1
//! # cache
2
//!
3
//! Manages internal cargo-make persistant cache.
4
//!
5

6
#[cfg(test)]
7
#[path = "./cache_test.rs"]
8
mod cache_test;
9

10
use crate::storage;
11
use crate::types::Cache;
12
use dirs;
13
use fsio;
14
use fsio::file::{read_text_file, write_text_file};
15
use std::path::{Path, PathBuf};
16
use toml;
17

18
static CACHE_FILE: &'static str = "cache.toml";
19

20 8
fn load_from_path(directory: PathBuf) -> Cache {
21 8
    let file_path = Path::new(&directory).join(CACHE_FILE);
22

23 8
    let mut cache_data = if file_path.exists() {
24 8
        match read_text_file(&file_path) {
25 8
            Ok(cache_str) => {
26 8
                let cache_data: Cache = match toml::from_str(&cache_str) {
27 8
                    Ok(value) => value,
28 0
                    Err(error) => {
29 0
                        info!("Unable to parse cache file, {}", error);
30 0
                        Cache::new()
31 0
                    }
32 2
                };
33

34 8
                cache_data
35 6
            }
36 2
            Err(error) => {
37 0
                info!(
38 0
                    "Unable to read cache file: {:?} error: {}",
39 0
                    &file_path,
40 0
                    error.to_string()
41
                );
42 0
                Cache::new()
43 0
            }
44 0
        }
45 2
    } else {
46 8
        Cache::new()
47
    };
48

49 8
    match file_path.to_str() {
50 8
        Some(value) => cache_data.file_name = Some(value.to_string()),
51 0
        None => cache_data.file_name = None,
52
    };
53

54 2
    cache_data
55 8
}
56

57 8
fn get_cache_directory(migrate: bool) -> Option<PathBuf> {
58 8
    let os_directory = dirs::cache_dir();
59 8
    storage::get_storage_directory(os_directory, CACHE_FILE, migrate)
60 8
}
61

62
/// Loads the persisted data
63 8
pub(crate) fn load() -> Cache {
64 8
    match get_cache_directory(true) {
65 8
        Some(directory) => load_from_path(directory),
66 0
        None => Cache::new(),
67
    }
68 8
}
69

70
/// Stores the data
71 8
pub(crate) fn store(cache_data: &Cache) {
72 8
    match get_cache_directory(false) {
73 8
        Some(directory) => {
74 8
            let exists = if directory.exists() {
75 8
                true
76
            } else {
77 0
                match fsio::directory::create(&directory) {
78 0
                    Ok(_) => true,
79 0
                    _ => false,
80
                }
81 0
            };
82

83 8
            if exists {
84 8
                let file_name = directory.join(CACHE_FILE);
85

86 8
                match toml::to_string_pretty(cache_data) {
87 8
                    Ok(toml_str) => match write_text_file(&file_name, &toml_str) {
88 8
                        Err(error) => info!(
89 0
                            "Error while writing to cache file: {:#?}, error: {:#?}",
90 0
                            &file_name, error
91 0
                        ),
92
                        _ => (),
93 6
                    },
94 0
                    Err(error) => info!(
95 0
                        "Error during serialization of cache, file: {:#?}, error: {:#?}",
96 0
                        &file_name, error
97 0
                    ),
98 2
                };
99 8
            }
100 6
        }
101 2
        None => (),
102
    }
103 8
}

Read our documentation on viewing source code .

Loading