Skip to main content

specfold_core/plugins/
payload.rs

1//! What plugins receive: a compact, stable view of the corpus and graph.
2
3use crate::corpus::Corpus;
4use crate::graph::Graph;
5use serde_json::{Value, json};
6
7pub fn docs(c: &Corpus, only: Option<&[usize]>) -> Value {
8    let pick: Vec<usize> = match only {
9        Some(list) => list.to_vec(),
10        None => (0..c.docs.len()).collect(),
11    };
12    Value::Array(pick.into_iter().map(|i| doc(c, i)).collect())
13}
14
15/// One document as every plugin sees it.
16pub fn doc(c: &Corpus, i: usize) -> Value {
17    let d = &c.docs[i];
18    json!({
19                    "id": d.id.to_string(),
20                    "type": d.id.ty,
21                    "owner": d.id.owner,
22                    "path": d.path,
23                    "chapters": d.chapters,
24                    "title": d.title,
25                    "status": d.status(),
26                    "frontmatter": d.fm(),
27        "anchors": d.anchors.iter().map(|a| &a.item).collect::<Vec<_>>(),
28    })
29}
30
31pub fn edges(c: &Corpus, g: &Graph) -> Value {
32    Value::Array(g.edges.iter().map(|e| json!({"from": c.docs[e.from].id.to_string(), "to": c.docs[e.to].id.to_string(), "kind": e.kind, "anchor": e.anchor})).collect())
33}