mod decisions;
mod json;
mod markdown;
use std::collections::BTreeMap;
use crate::document::Document;
use crate::hash::{DocumentHashes, hash_indexed_with_views};
use crate::ids::ElementId;
use crate::index::IndexErrors;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Projections {
pub markdown: String,
pub json: Vec<u8>,
}
pub fn render(document: &Document) -> Result<Projections, IndexErrors> {
let indexed = document.index()?;
let (hashes, views) = hash_indexed_with_views(&indexed);
let labels = Labels::of(document);
Ok(Projections {
markdown: markdown::render(document, &hashes, &labels),
json: json::render(document, &hashes, &views, &labels),
})
}
pub(crate) struct Labels(BTreeMap<ElementId, String>);
impl Labels {
fn of(document: &Document) -> Self {
Self(
document
.bindings
.iter()
.map(|binding| (binding.element, binding.label.to_string()))
.collect(),
)
}
pub(crate) fn label(&self, element: impl Into<ElementId>) -> Option<&str> {
self.0.get(&element.into()).map(String::as_str)
}
pub(crate) fn name(&self, element: impl Into<ElementId>) -> String {
let element = element.into();
self.label(element)
.map_or_else(|| element.key().to_string(), str::to_owned)
}
}
fn state_root(hashes: &DocumentHashes) -> String {
hashes.roots.state.to_string()
}