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
//! Where the text to format comes from, and what the formatter made of it.
//!
//! [`Inputs`] resolves the command line's positional arguments into either
//! standard input or a list of paths. [`ReadOnlyDir`] and [`assess`] turn one
//! file behind a directory capability into an [`Assessment`] — the text as
//! read, paired with the text the formatter would write — and [`write_back`]
//! replaces the file with the second.
//!
//! None of this reports. The line-ending counts an assessment measured travel
//! on it, and the boundary in the parent module, which acts on them, is what
//! logs them.
use PathBuf;
use anyhow;
use ;
use Dir;
use ;
use Formatter;
/// A directory capability that can only read.
///
/// The reporting modes receive this instead of a [`Dir`], so a wrong `match`
/// arm cannot write: read-only access is enforced by the type, not by
/// convention.
;
/// A file's current text paired with the text the formatter would write.
/// Where the text to format comes from.
///
/// An explicit answer rather than "the file list is empty", because the two
/// facts that emptiness would conflate are not the same: "no paths were named,
/// so read standard input" and "the selected source resolved to no paths". The
/// second is a legitimate outcome for a source that discovers its own inputs —
/// it must be able to name nothing and still exit
/// [`ExitStatus::Success`](super::reporting::ExitStatus::Success), rather than
/// fall through to a standard input that may be a terminal. See `AX-6`.
/// Reads a file and pairs its text with the formatted result.
///
/// Takes [`ReadOnlyDir`], so this function cannot write, and it emits nothing:
/// the line-ending counts it measures travel on the returned [`Assessment`],
/// and the boundary that acts on them reports them. `storage_key` is the bare
/// file name within the capability, which is the name this function reads by.
///
/// # Errors
///
/// Returns an error if the file cannot be read or is not valid UTF-8.
/// Writes the formatted text back, if the file still holds the text it was
/// assessed from.
///
/// Only reachable from [`Mode::InPlace`](super::reporting::Mode::InPlace), and
/// only for a file whose bytes would change: see [`Assessment::is_changed`].
/// The replacement is atomic and capability-scoped, and it renames a temporary
/// over the target, so an unconditional call would swap the inode of a file it
/// left byte-identical. See [`mdtablefix::io::replace_file`].
///
/// The conditional entry point is the one used here rather than a plain
/// replacement: the assessment is a reading of the file that a concurrent
/// writer can invalidate while the formatter runs, and writing the formatted
/// text over the writer's version would discard work this run never saw. A
/// target that moved on is left exactly as that writer left it, and `Ok(false)`
/// says so; the boundary decides what that means for the run.
///
/// # Errors
///
/// Returns an error if the file cannot be written, or if it cannot be read back
/// for the comparison.