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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
//! [`Flockfile`]: the parsed document, and the two entry points that build one.
//!
//! [`Flockfile::parse`] returns the flock; [`Flockfile::parse_declared`] also
//! reports which keys each app table wrote, which the config alone cannot say.
use crate::config::AppConfig;
use super::{
declared_app::DeclaredApp, error::FlockfileError, format::FlockFormat, parse::parse_into,
raw::RawFlockfile,
};
/// A parsed Flockfile: the declared flock
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Flockfile {
/// App entries in declaration order
pub apps: Vec<AppConfig>,
}
impl Flockfile {
/// Parses Flockfile source text in the given format.
///
/// # Errors
/// - Format variants ([`FlockfileError::Toml`] etc.): backend parse
/// failure, carrying the backend's message. Json5 additionally rejects
/// sources nested past a depth of 64 before reaching the backend
/// parser, whose recursive-descent stack-overflows on deep input
/// rather than returning an error.
/// - [`FlockfileError::NoApps`]: parsed fine but declared no apps.
/// - [`FlockfileError::UnknownKeys`]: named a key no field claims.
pub fn parse(source: &str, format: FlockFormat) -> Result<Self, FlockfileError> {
Ok(Self {
apps: parse_nonempty_apps(source, format)?,
})
}
/// Parses `text` and reports, per app, which keys the document wrote.
///
/// Runs the same per-format parse and validation [`Flockfile::parse`]
/// does, then separately deserializes the same source into a
/// [`serde_json::Value`] and reads each app table's keys off it:
/// `AppConfig`'s `#[serde(default)]` erases which keys a document
/// actually named, which is exactly what the value pass recovers.
///
/// # Errors
/// Every error [`Flockfile::parse`] returns, for the same inputs.
pub fn parse_declared(
text: &str,
format: FlockFormat,
) -> Result<Vec<DeclaredApp>, FlockfileError> {
// Same reasoning as `Flockfile::parse`: this reads a Flockfile off
// disk (the reload/muster path in shep-cli), not a value off the
// wire, so a typo here must still be loud.
let apps = parse_nonempty_apps(text, format)?;
// A document that reached this point already parsed successfully
// into `RawFlockfile` above, so the same source deserializing into a
// generic `Value` cannot fail for a reason the `RawFlockfile` pass
// would not already have caught.
let value = parse_into::<serde_json::Value>(text, format)?;
let tables: Vec<Option<&serde_json::Map<String, serde_json::Value>>> = value
.get("app")
.and_then(serde_json::Value::as_array)
.map(|apps| apps.iter().map(serde_json::Value::as_object).collect())
.unwrap_or_default();
Ok(apps
.into_iter()
.enumerate()
.map(|(index, config)| {
let table = tables.get(index).copied().flatten();
let declared = table
.map(|t| t.keys().cloned().collect())
.unwrap_or_default();
let declared_env = table
.and_then(|t| t.get("env"))
.and_then(serde_json::Value::as_object)
.map(|e| e.keys().cloned().collect())
.unwrap_or_default();
DeclaredApp {
config,
declared,
declared_env,
}
})
.collect())
}
}
/// The apps a Flockfile declares, refusing a document that declares none.
///
/// Both public entry points start here. The top-level fields that are not
/// apps are discarded by name, so the next one to arrive — `$schema` and
/// `dog` both came in this way, and `deny_unknown_fields` means the next one
/// must too — is a change to `RawFlockfile` and to this destructure, rather
/// than to two callers that have to stay in step.
fn parse_nonempty_apps(
source: &str,
format: FlockFormat,
) -> Result<Vec<AppConfig>, FlockfileError> {
let RawFlockfile {
schema: _schema,
// Discarded by name. Whatever a dog wrote under `[dog]` is that
// dog's to read out of the file itself; shep only had to stop
// refusing the document for containing it.
dog: _dog,
apps,
} = RawFlockfile::new(source, format)?;
if apps.is_empty() {
return Err(FlockfileError::NoApps);
}
Ok(apps)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::flockfile::format::FlockFormat;
#[test]
fn toml_array_of_tables() {
let src = r#"
[[app]]
name = "web"
script = "./srv"
[[app]]
name = "worker"
script = "python3"
args = ["job.py"]
"#;
let flock = Flockfile::parse(src, FlockFormat::Toml).unwrap();
assert_eq!(flock.apps.len(), 2);
assert_eq!(flock.apps[1].name, "worker");
}
#[test]
fn json_and_json5_and_yaml() {
let json = r#"{ "app": [{ "name": "web", "script": "./srv" }] }"#;
assert_eq!(
Flockfile::parse(json, FlockFormat::Json)
.unwrap()
.apps
.len(),
1
);
let json5 = r#"{ app: [{ name: "web", script: "./srv" }], /* comment */ }"#;
assert_eq!(
Flockfile::parse(json5, FlockFormat::Json5)
.unwrap()
.apps
.len(),
1
);
let yaml = "app:\n - name: web\n script: ./srv\n";
assert_eq!(
Flockfile::parse(yaml, FlockFormat::Yaml)
.unwrap()
.apps
.len(),
1
);
}
#[test]
fn empty_app_list_is_an_error() {
assert_eq!(
Flockfile::parse("app: []\n", FlockFormat::Yaml).unwrap_err(),
FlockfileError::NoApps
);
}
#[test]
fn parse_errors_carry_the_backend_message() {
match Flockfile::parse("not toml [[", FlockFormat::Toml).unwrap_err() {
FlockfileError::Toml(msg) => assert!(!msg.is_empty()),
other => panic!("expected Toml error, got {other:?}"),
}
}
/// fails if the declared key set is inferred from values rather than read
/// from the document. `autorestart = true` is also the default, so a
/// parser that reports "fields that differ from Default" would miss it,
/// and a later file load would then overwrite an operator who had
/// turned it off.
#[test]
fn declared_reports_keys_the_document_wrote_even_at_their_default() {
let text = r#"
[[app]]
name = "web"
script = "./srv"
autorestart = true
"#;
let apps = Flockfile::parse_declared(text, FlockFormat::Toml).unwrap();
assert_eq!(apps.len(), 1);
let declared = &apps[0].declared;
assert!(declared.contains("autorestart"), "declared: {declared:?}");
assert!(declared.contains("name"));
assert!(declared.contains("script"));
assert!(
!declared.contains("max_memory"),
"a key nobody wrote is not declared"
);
assert_eq!(declared.len(), 3);
}
/// fails if env keys are not reported separately. `env` is the only map
/// of user-supplied keys in `AppConfig`, so the merge treats it one
/// level deeper than every other field.
#[test]
fn declared_env_reports_the_keys_inside_the_env_table() {
let text = r#"
[[app]]
name = "web"
script = "./srv"
env = { DB_HOST = "", NODE_ENV = "production" }
"#;
let apps = Flockfile::parse_declared(text, FlockFormat::Toml).unwrap();
assert_eq!(
apps[0].declared_env.iter().collect::<Vec<_>>(),
vec!["DB_HOST", "NODE_ENV"]
);
assert!(apps[0].declared.contains("env"));
}
/// fails if a format other than TOML loses the key set. All four go
/// through one generic intermediate, so a regression here means the
/// intermediate was bypassed for a format.
#[test]
fn declared_survives_every_parse_format() {
let cases: [(FlockFormat, &str); 4] = [
(
FlockFormat::Toml,
"[[app]]\nname = \"web\"\nscript = \"./srv\"\nautorestart = true\n",
),
(
FlockFormat::Yaml,
"app:\n - name: web\n script: ./srv\n autorestart: true\n",
),
(
FlockFormat::Json,
r#"{"app":[{"name":"web","script":"./srv","autorestart":true}]}"#,
),
(
FlockFormat::Json5,
"{ app: [{ name: \"web\", script: \"./srv\", autorestart: true }] }",
),
];
for (format, text) in cases {
let apps = Flockfile::parse_declared(text, format)
.unwrap_or_else(|e| panic!("{format:?} failed to parse: {e}"));
assert!(
apps[0].declared.contains("autorestart"),
"{format:?}: declared {:?}",
apps[0].declared
);
}
}
/// A typo in a Flockfile must still be loud. This is the whole reason
/// `deny_unknown_fields` was there.
#[test]
fn a_misspelled_flockfile_key_is_refused_and_named() {
let err = Flockfile::parse(
"[[app]]\nname = \"web\"\nscript = \"./srv\"\nmax_restrts = 5\n",
FlockFormat::Toml,
)
.expect_err("a typo must be refused");
let FlockfileError::UnknownKeys { keys } = err else {
panic!("expected UnknownKeys, got {err:?}");
};
assert!(
keys.iter().any(|k| k.contains("max_restrts")),
"got {keys:?}"
);
}
/// Nesting is why this uses serde_ignored rather than a key list.
///
/// `kind`/`target` are supplied alongside the typo: both are required by
/// `ProbeConfig` with no default, and omitting them would surface a
/// missing-field error instead of the unknown-key one this test means to
/// exercise.
#[test]
fn a_misspelled_key_inside_a_probe_is_also_named() {
let err = Flockfile::parse(
"[[app]]\nname = \"web\"\nscript = \"./srv\"\n[app.readiness_probe]\nkind = \"http\"\ntarget = \"http://localhost/x\"\ntimeuot = \"5s\"\n",
FlockFormat::Toml,
)
.expect_err("a nested typo must be refused");
let FlockfileError::UnknownKeys { keys } = err else {
panic!("expected UnknownKeys, got {err:?}");
};
// The variant alone would pass on any path at all, including one
// from a different app. Nesting is the reason this parse goes
// through `serde_ignored` rather than a flat key list, so the
// path is the thing worth asserting.
assert!(
keys.iter()
.any(|key| key.contains("readiness_probe") && key.contains("timeuot")),
"the nested path should name both the probe and the key: {keys:?}"
);
}
/// `dogs` takes a table per dog, not a bare value: `[app.dogs]` naming
/// a dog with a non-table value must be refused. `DogTable`'s own
/// refusal is field-agnostic (it does not know it is ever named
/// `dogs`), so what this proves is that the backend surfaces the
/// refusal rather than swallowing it, quoting the offending line.
#[test]
fn a_dogs_entry_that_is_not_a_table_is_refused() {
let err = Flockfile::parse(
"[[app]]\nname = \"web\"\nscript = \"./srv\"\n[app.dogs]\njobs = 5\n",
FlockFormat::Toml,
)
.expect_err("a bare value under dogs must be refused");
let FlockfileError::Toml(message) = err else {
panic!("expected Toml, got {err:?}");
};
assert!(
message.contains("table"),
"the refusal must say why: {message}"
);
assert!(
message.contains("jobs = 5"),
"the refusal must quote the offending line: {message}"
);
}
/// The top-level `dogs` key does not exist: only `[app.dogs.<name>]`,
/// nested under an app, does. A top-level `[dogs.jobs]` must fail the
/// same way `[build]` already does.
///
/// `RawFlockfile` refuses this through its own `deny_unknown_fields`,
/// which the backend's derived `Deserialize` enforces directly rather
/// than through the `serde_ignored` callback `UnknownKeys` collects
/// from: unlike a typo inside an app, this key is refused before
/// `RawFlockfile` is ever fully built, so the error is the backend's
/// own rather than `FlockfileError::UnknownKeys`.
#[test]
fn a_top_level_dogs_table_is_refused_naming_the_key() {
let err = Flockfile::parse(
"[dogs.jobs]\nconcurrency = 2\n\n[[app]]\nname = \"web\"\nscript = \"./srv\"\n",
FlockFormat::Toml,
)
.expect_err("a top-level dogs table is not app.dogs and must be refused");
assert!(
err.to_string().contains("dogs"),
"the refusal must name the key: {err}"
);
}
/// The design goal is one refusal naming every typo, not one refusal
/// per run. Two misspelled keys in one document must both show up in
/// the single [`FlockfileError::UnknownKeys`] this produces.
#[test]
fn two_misspelled_keys_are_both_named_in_one_error() {
let err = Flockfile::parse(
"[[app]]\nname = \"web\"\nscript = \"./srv\"\nmax_restrts = 5\ninstences = 2\n",
FlockFormat::Toml,
)
.expect_err("both typos must be refused");
let FlockfileError::UnknownKeys { keys } = err else {
panic!("expected UnknownKeys, got {err:?}");
};
assert!(
keys.iter().any(|k| k.contains("max_restrts")),
"got {keys:?}"
);
assert!(keys.iter().any(|k| k.contains("instences")), "got {keys:?}");
}
}