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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use crate::{
customlog,
formatter::{Formatter, FormatterName},
get_path_mtime, Mtime,
};
use anyhow::Result;
use log::{debug, error, warn};
use serde::{Deserialize, Serialize};
use sha1::{Digest, Sha1};
use std::collections::BTreeMap;
use std::fs::{read_to_string, File};
use std::io::Write;
use std::path::{Path, PathBuf};
#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone)]
pub struct FormatterInfo {
pub command: PathBuf,
pub command_mtime: Mtime,
pub command_resolved: PathBuf,
pub command_resolved_mtime: Mtime,
pub options: Vec<String>,
pub work_dir: PathBuf,
}
#[derive(Debug, Deserialize, Serialize)]
pub struct CacheManifest {
pub formatters: BTreeMap<FormatterName, FormatterInfo>,
pub matches: BTreeMap<FormatterName, BTreeMap<PathBuf, Mtime>>,
}
impl Default for CacheManifest {
fn default() -> Self {
Self {
formatters: BTreeMap::new(),
matches: BTreeMap::new(),
}
}
}
impl Clone for CacheManifest {
fn clone(&self) -> Self {
Self {
formatters: self.formatters.clone(),
matches: self.matches.clone(),
}
}
}
impl CacheManifest {
pub fn try_load(cache_dir: &Path, treefmt_toml: &Path) -> Result<Self> {
let manifest_path = get_manifest_path(cache_dir, treefmt_toml);
debug!("cache: loading from {}", manifest_path.display());
let content = read_to_string(&manifest_path)?;
let manifest = toml::from_str(&content)?;
Ok(manifest)
}
#[must_use]
pub fn load(cache_dir: &Path, treefmt_toml: &Path) -> Self {
match Self::try_load(cache_dir, treefmt_toml) {
Ok(manifest) => manifest,
Err(err) => {
warn!("cache: failed to load the manifest due to: {}", err);
Self::default()
}
}
}
pub fn try_write(self, cache_dir: &Path, treefmt_toml: &Path) -> Result<()> {
let manifest_path = get_manifest_path(cache_dir, treefmt_toml);
debug!("cache: writing to {}", manifest_path.display());
let mut f = File::create(manifest_path)?;
f.write_all(
format!(
"# {} DO NOT HAND EDIT THIS FILE {}\n\n{}",
customlog::WARN,
customlog::WARN,
toml::to_string_pretty(&self)?
)
.as_bytes(),
)?;
Ok(())
}
pub fn write(self, cache_dir: &Path, treefmt_toml: &Path) {
if let Err(err) = self.try_write(cache_dir, treefmt_toml) {
warn!("cache: failed to write to disk: {}", err);
};
}
pub fn update_formatters(&mut self, formatters: BTreeMap<FormatterName, Formatter>) {
let mut old_formatters = std::mem::take(&mut self.formatters);
for (name, fmt) in formatters {
match load_formatter_info(fmt) {
Ok(new_fmt_info) => {
if let Some(old_fmt_info) = old_formatters.remove(&name) {
if old_fmt_info != new_fmt_info {
self.matches.remove(&name);
}
}
self.formatters.insert(name, new_fmt_info);
}
Err(err) => {
error!("cache: failed to load the formatter info {}", err)
}
}
}
for name in old_formatters.keys() {
self.matches.remove(&name);
}
}
#[must_use]
pub fn filter_matches(
&self,
matches: BTreeMap<FormatterName, BTreeMap<PathBuf, Mtime>>,
) -> BTreeMap<FormatterName, BTreeMap<PathBuf, Mtime>> {
matches
.into_iter()
.fold(BTreeMap::new(), |mut sum, (key, path_infos)| {
let new_path_infos = match self.matches.get(&key) {
Some(prev_paths) => {
path_infos
.into_iter()
.fold(BTreeMap::new(), |mut sum, (path, mtime)| {
let prev_mtime = prev_paths.get(&path).unwrap_or(&Mtime(-1));
if prev_mtime != &mtime {
sum.insert(path, mtime);
}
sum
})
}
None => path_infos,
};
sum.insert(key, new_path_infos);
sum
})
}
pub fn add_results(&mut self, matches: BTreeMap<FormatterName, BTreeMap<PathBuf, Mtime>>) {
for (name, path_infos) in matches {
if let Some(old_path_infos) = self.matches.get_mut(&name) {
old_path_infos.extend(path_infos);
} else {
self.matches.insert(name, path_infos);
}
}
}
}
fn load_formatter_info(fmt: Formatter) -> Result<FormatterInfo> {
let command = fmt.command;
let command_mtime = get_path_mtime(&command)?;
let command_resolved = std::fs::canonicalize(command.clone())?;
let command_resolved_mtime = get_path_mtime(&command_resolved)?;
let options = fmt.options;
let work_dir = fmt.work_dir;
Ok(FormatterInfo {
command,
command_mtime,
command_resolved,
command_resolved_mtime,
options,
work_dir,
})
}
fn get_manifest_path(cache_dir: &Path, treefmt_toml: &Path) -> PathBuf {
assert!(cache_dir.is_absolute());
assert!(treefmt_toml.is_absolute());
let path_bytes = treefmt_toml.to_string_lossy();
let treefmt_hash = Sha1::digest(path_bytes.as_bytes());
let filename = format!("{:x}.toml", treefmt_hash);
cache_dir.join(filename)
}