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
|
//! Views (docs/design.md, 5.3): rendered into scratch, compared with what is in
//! place, and how they changed.
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use anyhow::Result;
use serde::Serialize;
use crate::content::Content;
use crate::name::Address;
use crate::program::{self, Given, Role};
use crate::schema::{Schema, ViewSpec};
use crate::scratch::Scratch;
/// The entries of every channel a view may be made from: the one being planned as it
/// would be, the others as they are.
pub type Entries<'a> = BTreeMap<&'a str, &'a Content>;
/// Renders the view `name` from `entries` into scratch below `scratch`; nothing if a
/// required slot it is made from is unfilled, or it is an entry's own and that entry
/// isn't there.
pub fn render(
schema: &Schema,
name: &str,
view: &ViewSpec,
entries: &Entries,
scratch: &Path,
) -> Result<Option<Scratch>> {
let entry = |address: &Address| {
entries
.get(address.channel.as_str())
.and_then(|content| content.get(&address.slot))
};
// A view made from a required slot that is unfilled isn't there (docs/design.md,
// section 7).
if view.from.iter().any(|address| {
schema.slot(address).is_some_and(|slot| slot.required) && entry(address).is_none()
}) {
return Ok(None);
}
match &view.renderer {
None => {
let address = &view.from[0];
let Some(bytes) = entry(address) else {
return Ok(None);
};
let out = Scratch::new(scratch, "view-")?;
crate::content::write_file(&out.path().join(&address.slot), bytes, 0o600)?;
Ok(Some(out))
}
Some(renderer) => {
let given = Given {
slots: view
.from
.iter()
.map(|address| {
let media_type = schema
.slot(address)
.map(|slot| slot.media_type.as_str())
.unwrap_or_default();
(
address.clone(),
(media_type, entry(address).map(Vec::as_slice)),
)
})
.collect(),
};
let outcome = program::run(renderer, Role::Renderer, name, &given, scratch)?;
// What it can't render, it fails on; and it may write regular files and
// directories only.
program::files_below(outcome.out.path())?;
Ok(Some(outcome.out))
}
}
}
/// How a view changes.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "lowercase")]
pub enum Change {
Unchanged,
Added,
Removed,
Changed,
}
/// One file of a view that changes.
#[derive(Debug, Clone, Serialize)]
pub struct FileChange {
pub path: PathBuf,
pub change: Change,
/// As diffutils says it (`diff`).
pub diff: String,
}
/// How what is in place (`old`) would become what was rendered (`new`): the view as a
/// whole, and its files. A secret view's files are named, but no diff is made.
pub fn compare(
old: Option<&Path>,
new: Option<&Path>,
secret: bool,
) -> Result<(Change, Vec<FileChange>)> {
let read = |dir: Option<&Path>| -> Result<BTreeMap<PathBuf, Vec<u8>>> {
match dir {
Some(dir) => program::files_below(dir),
None => Ok(BTreeMap::new()),
}
};
let (old_files, new_files) = (read(old)?, read(new)?);
let mut files = Vec::new();
for (path, bytes) in &new_files {
match old_files.get(path) {
Some(old) if old == bytes => {}
Some(old) => files.push(FileChange {
path: path.clone(),
change: Change::Changed,
diff: diff(path, Some(old), Some(bytes), secret),
}),
None => files.push(FileChange {
path: path.clone(),
change: Change::Added,
diff: diff(path, None, Some(bytes), secret),
}),
}
}
for (path, bytes) in &old_files {
if !new_files.contains_key(path) {
files.push(FileChange {
path: path.clone(),
change: Change::Removed,
diff: diff(path, Some(bytes), None, secret),
});
}
}
let change = match (old, new) {
(None, None) => Change::Unchanged,
(None, Some(_)) => Change::Added,
(Some(_), None) => Change::Removed,
(Some(_), Some(_)) if files.is_empty() => Change::Unchanged,
(Some(_), Some(_)) => Change::Changed,
};
Ok((change, files))
}
/// How a file changes, as diffutils' `diff -ru a b` says it: a unified diff for text in
/// a public view; for a secret view, or bytes that aren't text, only that the file
/// differs, or is only on one side.
fn diff(path: &Path, old: Option<&[u8]>, new: Option<&[u8]>, secret: bool) -> String {
let name = path.display().to_string();
let text = |bytes: Option<&[u8]>| match bytes {
None => Some(String::new()),
Some(bytes) => std::str::from_utf8(bytes).ok().map(str::to_owned),
};
let (old_text, new_text) = (text(old), text(new));
match (old_text, new_text) {
(Some(old), Some(new)) if !secret => similar::TextDiff::from_lines(&old, &new)
.unified_diff()
.header(&format!("a/{name}"), &format!("b/{name}"))
.to_string(),
_ => match (old, new) {
(None, _) => format!("Only in b: {name}\n"),
(_, None) => format!("Only in a: {name}\n"),
_ if secret => format!("Files a/{name} and b/{name} differ\n"),
_ => format!("Binary files a/{name} and b/{name} differ\n"),
},
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn diffs_as_diffutils_says_them() {
let path = Path::new("f");
assert_eq!(
diff(path, Some(b"a\n"), Some(b"b\n"), true),
"Files a/f and b/f differ\n"
);
assert_eq!(
diff(path, Some(b"\xff"), Some(b"\xfe"), false),
"Binary files a/f and b/f differ\n"
);
assert_eq!(diff(path, None, Some(b"k"), true), "Only in b: f\n");
assert_eq!(diff(path, Some(b"\xff"), None, false), "Only in a: f\n");
assert!(diff(path, Some(b"a\n"), Some(b"b\n"), false).contains("-a\n+b\n"));
}
}
|