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