Skip to main content

ara_core/
lib.rs

1//! `ara-core`: the shared core of the ARA viewer runtime.
2//!
3//! This crate holds all parsing, normalization, binding resolution, and DAG
4//! layout for the ARA viewer. It is compiled to both native targets (used by
5//! `ara-cli`) and `wasm32-unknown-unknown` (used by the browser client), so it
6//! is the single source of truth that keeps the server and client from
7//! drifting.
8//!
9//! See <https://github.com/ARA-Labs/ara-cli>.
10
11mod claims;
12pub mod layout;
13pub mod manifest;
14mod parse;
15pub mod report;
16mod schema;
17
18pub use layout::{LayoutOptions, LayoutResult, NodePosition, Point, Rect};
19pub use manifest::{
20    Binding, BindingRole, Claim, ClaimId, Link, LinkKind, Manifest, Node, NodeFields, NodeId,
21    NodeKind,
22};
23pub use report::{Diagnostic, ParseReport, Severity};
24
25#[cfg(feature = "native")]
26pub use parse::parse_dir;
27pub use parse::parse_sources;
28
29/// Parses and lays out an in-memory ARA artifact.
30///
31/// On parse success, runs layout and returns the positioned manifest. On parse
32/// error (including cycles), returns the report unchanged and skips layout.
33pub fn parse_and_layout(
34    tree_yaml: &str,
35    claims_md: Option<&str>,
36    opts: &LayoutOptions,
37) -> Result<(Manifest, ParseReport), ParseReport> {
38    let (mut manifest, report) = parse_sources(tree_yaml, claims_md)?;
39    let result = layout::layout(&manifest, opts);
40    for np in result.positions {
41        if let Some(node) = manifest.nodes.iter_mut().find(|n| n.id == np.id) {
42            node.pos = Some(np.pos);
43        }
44    }
45    manifest.bounds = Some(result.bounds);
46    Ok((manifest, report))
47}
48
49/// Reads, parses, and lays out an ARA artifact directory. Native only.
50#[cfg(feature = "native")]
51pub fn parse_and_layout_dir(
52    dir: &std::path::Path,
53    opts: &LayoutOptions,
54) -> Result<(Manifest, ParseReport), ParseReport> {
55    let (mut manifest, report) = parse_dir(dir)?;
56    let result = layout::layout(&manifest, opts);
57    for np in result.positions {
58        if let Some(node) = manifest.nodes.iter_mut().find(|n| n.id == np.id) {
59            node.pos = Some(np.pos);
60        }
61    }
62    manifest.bounds = Some(result.bounds);
63    Ok((manifest, report))
64}
65
66/// Returns the version of `ara-core`, taken from the crate manifest.
67pub fn version() -> &'static str {
68    env!("CARGO_PKG_VERSION")
69}
70
71#[cfg(test)]
72mod tests {
73    use super::*;
74
75    #[test]
76    fn version_is_reported() {
77        assert_eq!(version(), env!("CARGO_PKG_VERSION"));
78    }
79}