bearout 0.2.0

A programmable contract engine for linked resources, documentation, and code
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// SPDX-License-Identifier: Apache-2.0

//! One side's contract projection: discovery, parsing, structural
//! validation, and the identifier graph over one tree. The candidate and
//! the comparison baseline go through the same steps; what differs is the
//! authority. Each side's own bootstrap decides which paths it classified
//! as resources and schema-less documents, while the candidate's limits
//! bound both sides and the candidate's registered schemas and shapes
//! interpret both. The baseline bootstrap is passive historical data: it
//! grants nothing, and no baseline rule module, generator, or template is
//! ever loaded or executed.

use std::collections::{BTreeMap, BTreeSet};

use serde_json::Value;

use crate::bootstrap::{Bootstrap, Limits, MANIFEST_NAME};
use crate::changes::{Classification, Surface, SurfaceEntry};
use crate::document::{self, Document};
use crate::envelope::{self, Resource};
use crate::graph::{self, Graph};
use crate::paths::ProjectPath;
use crate::policy::Policy;
use crate::report::Code as C;
use crate::report::{Diagnostic, Side};
use crate::shape::{self, Shape};
use crate::tree::ReadTree;
use crate::{markdown, references};

/// Everything read from one tree before policy interprets it.
pub struct Gathered {
    pub files: Vec<ProjectPath>,
    pub parsed: Vec<Parsed>,
    pub documents: Vec<Document>,
    pub document_paths: Vec<ProjectPath>,
    /// Every file whose bytes were read, with the digest of those bytes.
    pub surface: Surface,
}

/// One side after structural validation and graph construction.
pub struct Projection {
    /// Every resource that parsed, in path order.
    pub resources: Vec<Resource>,
    /// Whether each resource is structurally valid.
    pub validity: Vec<bool>,
    /// Every document that was read, in path order.
    pub documents: Vec<Document>,
    pub graph: Graph,
    /// Every file whose bytes were read, with the digest of those bytes.
    pub surface: Surface,
}

impl Projection {
    /// Positions of the structurally valid resources.
    #[must_use]
    pub fn valid_indexes(&self) -> Vec<usize> {
        self.validity
            .iter()
            .enumerate()
            .filter(|(_, valid)| **valid)
            .map(|(index, _)| index)
            .collect()
    }
}

/// Every resource root must be a directory of the tree.
pub fn check_resource_roots(tree: &dyn ReadTree, bootstrap: &Bootstrap) -> Result<(), String> {
    for root_path in &bootstrap.resource_roots {
        if !tree.is_dir(root_path) {
            return Err(format!(
                "resource root `{root_path}` is not a directory inside the project"
            ));
        }
    }
    Ok(())
}

/// Discover and parse one side. `bootstrap` selects the paths; `limits`
/// are always the candidate's. A discovery failure is fatal.
pub fn gather(
    tree: &dyn ReadTree,
    bootstrap: &Bootstrap,
    limits: &Limits,
    diagnostics: &mut Vec<Diagnostic>,
) -> Result<Gathered, String> {
    let files = discover(tree, bootstrap, limits)?;
    let document_paths = document::discover(tree, bootstrap, limits, &files)?;
    let mut surface = Surface::new();
    let parsed = parse_all(tree, limits, &files, &mut surface, diagnostics);
    let documents = read_documents(tree, limits, &document_paths, diagnostics);
    for document in &documents {
        surface.insert(
            document.path.clone(),
            SurfaceEntry {
                classification: Classification::Document,
                digest: document.digest.clone(),
                bytes: document.bytes,
            },
        );
    }
    Ok(Gathered {
        files,
        parsed,
        documents,
        document_paths,
        surface,
    })
}

/// Validate structure with the candidate's policy and shapes, then build
/// the identifier graph, on either side: duplicate identifiers and typed
/// relations are checked for the baseline too, since policy pairs records
/// through them. Markdown links, images, and anchors are checked for the
/// candidate only; the baseline's were checked when its revision was.
pub fn settle(
    tree: &dyn ReadTree,
    gathered: Gathered,
    policy: &Policy,
    shapes: &BTreeMap<String, Shape>,
    side: Side,
    diagnostics: &mut Vec<Diagnostic>,
) -> Projection {
    let Gathered {
        files,
        mut parsed,
        documents,
        document_paths,
        surface,
    } = gathered;
    validate_structure(&mut parsed, policy, shapes, side, diagnostics);
    let resources: Vec<Resource> = parsed
        .iter_mut()
        .map(|entry| std::mem::replace(&mut entry.resource, placeholder()))
        .collect();
    let validity: Vec<bool> = parsed.iter().map(|entry| entry.valid).collect();
    let graph = graph::build(&resources, &validity, shapes, diagnostics);
    if side == Side::Candidate {
        references::check(
            tree,
            &resources,
            &validity,
            &files,
            &documents,
            &document_paths,
            diagnostics,
        );
    }
    Projection {
        resources,
        validity,
        documents,
        graph,
        surface,
    }
}

/// Project the comparison baseline. Its bootstrap, when present, is parsed
/// as passive historical data that only selects paths; a tree without a
/// bootstrap is an empty historical project. The candidate's limits,
/// policy, and shapes apply. Every diagnostic is tagged with the baseline
/// side; a discovery failure is fatal and names the baseline by `label`,
/// the revision name as supplied or, for a fixture, the unmodified source.
pub fn baseline(
    tree: &dyn ReadTree,
    label: &str,
    limits: &Limits,
    policy: &Policy,
    shapes: &BTreeMap<String, Shape>,
    diagnostics: &mut Vec<Diagnostic>,
) -> Result<Projection, String> {
    let fatal = |message: String| format!("baseline `{label}`: {message}");
    let manifest_path = ProjectPath::parse(MANIFEST_NAME).expect("constant path");
    let mut own = Vec::new();
    let gathered = if tree.exists(&manifest_path) {
        let text = tree
            .read_text(&manifest_path)
            .map_err(|error| fatal(format!("cannot read {MANIFEST_NAME}: {error}")))?;
        let bootstrap = crate::bootstrap::parse(&text)
            .map_err(|error| fatal(format!("{MANIFEST_NAME} is not usable: {error}")))?;
        check_resource_roots(tree, &bootstrap).map_err(fatal)?;
        let mut gathered = gather(tree, &bootstrap, limits, &mut own).map_err(fatal)?;
        gathered.surface.insert(
            manifest_path,
            SurfaceEntry::new(Classification::Manifest, text.as_bytes()),
        );
        gathered
    } else {
        Gathered {
            files: Vec::new(),
            parsed: Vec::new(),
            documents: Vec::new(),
            document_paths: Vec::new(),
            surface: Surface::new(),
        }
    };
    let projection = settle(tree, gathered, policy, shapes, Side::Baseline, &mut own);
    diagnostics.extend(
        own.into_iter()
            .map(|diagnostic| diagnostic.on_side(Side::Baseline)),
    );
    Ok(projection)
}

pub struct Parsed {
    resource: Resource,
    valid: bool,
}

/// An empty resource used to move parsed resources out of their entries.
fn placeholder() -> Resource {
    Resource {
        path: ProjectPath::root(),
        schema: String::new(),
        id: String::new(),
        refs: Vec::new(),
        fields: Value::Null,
        field_lines: BTreeMap::new(),
        body: String::new(),
        line_count: 0,
        doc: markdown::Document::default(),
        fragments: Vec::new(),
    }
}

fn discover(
    tree: &dyn ReadTree,
    bootstrap: &Bootstrap,
    limits: &Limits,
) -> Result<Vec<ProjectPath>, String> {
    let mut files = Vec::new();
    for root in &bootstrap.resource_roots {
        let found = tree
            .walk(root)
            .map_err(|error| format!("cannot walk resource root `{root}`: {error}"))?;
        files.extend(
            found
                .into_iter()
                .filter(|path| matches!(path.extension(), Some("md" | "toml"))),
        );
    }
    files.sort();
    files.dedup();
    if files.len() > limits.resources {
        return Err(format!(
            "{} resources exceed `limits.resources` = {}",
            files.len(),
            limits.resources
        ));
    }
    Ok(files)
}

fn parse_all(
    tree: &dyn ReadTree,
    limits: &Limits,
    files: &[ProjectPath],
    surface: &mut Surface,
    diagnostics: &mut Vec<Diagnostic>,
) -> Vec<Parsed> {
    let mut parsed = Vec::new();
    for path in files {
        match tree.file_len(path) {
            Ok(len) if len > limits.resource_bytes => {
                diagnostics.push(Diagnostic::new(
                    C::Unreadable,
                    path.as_str(),
                    format!(
                        "resource is {len} bytes, above `limits.resource_bytes` = {}",
                        limits.resource_bytes
                    ),
                ));
                continue;
            }
            Ok(_) => {}
            Err(error) => {
                diagnostics.push(Diagnostic::new(
                    C::Unreadable,
                    path.as_str(),
                    format!("cannot read resource: {error}"),
                ));
                continue;
            }
        }
        let bytes = match tree.read(path) {
            Ok(bytes) => bytes,
            Err(error) => {
                diagnostics.push(Diagnostic::new(
                    C::Unreadable,
                    path.as_str(),
                    format!("cannot read resource: {error}"),
                ));
                continue;
            }
        };
        surface.insert(
            path.clone(),
            SurfaceEntry::new(Classification::Resource, &bytes),
        );
        let mut envelope_diagnostics = Vec::new();
        match envelope::parse(path, &bytes, &mut envelope_diagnostics) {
            Ok(resource) => {
                let valid = envelope_diagnostics.is_empty();
                diagnostics.extend(envelope_diagnostics);
                parsed.push(Parsed { resource, valid });
            }
            Err(diagnostic) => {
                diagnostics.push(diagnostic);
                diagnostics.extend(envelope_diagnostics);
            }
        }
    }
    parsed
}

fn read_documents(
    tree: &dyn ReadTree,
    limits: &Limits,
    paths: &[ProjectPath],
    diagnostics: &mut Vec<Diagnostic>,
) -> Vec<document::Document> {
    let mut documents = Vec::new();
    for path in paths {
        match document::read(tree, limits, path) {
            Ok(document) => documents.push(document),
            Err(diagnostic) => diagnostics.push(diagnostic),
        }
    }
    documents
}

/// Validate every parsed resource against its registered schema's shape:
/// fields, required sections, and fragments. A resource whose schema is
/// unregistered or whose shape failed to load is not valid.
fn validate_structure(
    parsed: &mut [Parsed],
    policy: &Policy,
    shapes: &BTreeMap<String, Shape>,
    side: Side,
    diagnostics: &mut Vec<Diagnostic>,
) {
    let unloadable: BTreeSet<&str> = policy
        .schemas
        .iter()
        .filter(|(id, schema)| schema.shape.is_some() && !shapes.contains_key(*id))
        .map(|(id, _)| id.as_str())
        .collect();
    for entry in parsed.iter_mut() {
        let resource = &entry.resource;
        let path = resource.path.as_str();
        if !policy.schemas.contains_key(&resource.schema) {
            let message = match side {
                Side::Candidate => format!(
                    "schema `{}` is not registered by the policy",
                    resource.schema
                ),
                Side::Baseline => format!(
                    "schema `{}` is not registered by the current policy; comparison interprets history with the candidate's schemas, so the policy must keep every schema its baseline uses",
                    resource.schema
                ),
            };
            diagnostics.push(Diagnostic::new(C::SchemaIdentity, path, message));
            entry.valid = false;
            continue;
        }
        if unloadable.contains(resource.schema.as_str()) {
            entry.valid = false;
            continue;
        }
        let Some(shape) = shapes.get(&resource.schema) else {
            continue;
        };
        let before = diagnostics.len();
        for violation in shape.check(&resource.fields) {
            let key = if violation.location.is_empty() {
                violation.unexpected.as_deref()
            } else {
                violation.location.split('.').next()
            };
            let line = key.and_then(|key| resource.field_lines.get(key)).copied();
            diagnostics
                .push(Diagnostic::new(C::ShapeViolation, path, describe(&violation)).at_line(line));
        }
        for title in &shape.sections {
            if !resource
                .doc
                .sections
                .iter()
                .any(|section| &section.title == title)
            {
                diagnostics.push(Diagnostic::new(
                    C::MissingSection,
                    path,
                    format!("body must contain a `{title}` section"),
                ));
            }
        }
        for fragment in &resource.fragments {
            match shape.check_fragment(&fragment.kind, &fragment.fields) {
                None => diagnostics.push(
                    Diagnostic::new(
                        C::FragmentMalformed,
                        path,
                        format!(
                            "fragment kind `{}` is not declared by schema `{}`",
                            fragment.kind, resource.schema
                        ),
                    )
                    .at_line(Some(fragment.line)),
                ),
                Some(violations) => {
                    for violation in violations {
                        diagnostics.push(
                            Diagnostic::new(
                                C::ShapeViolation,
                                path,
                                format!("fragment `{}`: {}", fragment.id, describe(&violation)),
                            )
                            .at_line(Some(fragment.line)),
                        );
                    }
                }
            }
        }
        if diagnostics.len() > before {
            entry.valid = false;
        }
    }
}

fn describe(violation: &shape::Violation) -> String {
    if violation.location.is_empty() {
        violation.message.clone()
    } else {
        format!("`{}`: {}", violation.location, violation.message)
    }
}