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// The `PAPER.md` / `logic/*` / `evidence/` readers are consumed only by the
18// native `parse_dir`; gating them keeps the wasm client build (which only
19// deserializes the already-built manifest) free of dead-code warnings.
20#[cfg(feature = "native")]
21mod evidence;
22#[cfg(feature = "native")]
23mod paper;
24#[cfg(feature = "native")]
25mod sections;
26
27pub use layout::{LayoutOptions, LayoutResult, NodePosition, Point, Rect};
28pub use manifest::{
29    Binding, BindingRole, BuiltOn, Claim, ClaimId, Concept, Exhibit, ExhibitKind, Link, LinkKind,
30    Manifest, Node, NodeExhibit, NodeFields, NodeId, NodeKind, PaperMeta, Problem, Recipe,
31    RelatedWork,
32};
33pub use report::{Diagnostic, ParseReport, Severity};
34
35#[cfg(feature = "native")]
36pub use parse::parse_dir;
37pub use parse::parse_sources;
38
39/// Parses and lays out an in-memory ARA artifact.
40///
41/// On parse success, runs layout and returns the positioned manifest. On parse
42/// error (including cycles), returns the report unchanged and skips layout.
43pub fn parse_and_layout(
44    tree_yaml: &str,
45    claims_md: Option<&str>,
46    opts: &LayoutOptions,
47) -> Result<(Manifest, ParseReport), ParseReport> {
48    let (mut manifest, report) = parse_sources(tree_yaml, claims_md)?;
49    let result = layout::layout(&manifest, opts);
50    for np in result.positions {
51        if let Some(node) = manifest.nodes.iter_mut().find(|n| n.id == np.id) {
52            node.pos = Some(np.pos);
53        }
54    }
55    manifest.bounds = Some(result.bounds);
56    Ok((manifest, report))
57}
58
59/// Reads, parses, and lays out an ARA artifact directory. Native only.
60#[cfg(feature = "native")]
61pub fn parse_and_layout_dir(
62    dir: &std::path::Path,
63    opts: &LayoutOptions,
64) -> Result<(Manifest, ParseReport), ParseReport> {
65    let (mut manifest, report) = parse_dir(dir)?;
66    let result = layout::layout(&manifest, opts);
67    for np in result.positions {
68        if let Some(node) = manifest.nodes.iter_mut().find(|n| n.id == np.id) {
69            node.pos = Some(np.pos);
70        }
71    }
72    manifest.bounds = Some(result.bounds);
73    Ok((manifest, report))
74}
75
76/// Returns the version of `ara-core`, taken from the crate manifest.
77pub fn version() -> &'static str {
78    env!("CARGO_PKG_VERSION")
79}
80
81#[cfg(test)]
82mod tests {
83    use super::*;
84
85    #[test]
86    fn version_is_reported() {
87        assert_eq!(version(), env!("CARGO_PKG_VERSION"));
88    }
89}