Skip to main content

cordis_include/
lib.rs

1//! Config entry trees and loader files for the
2//! [cordis-rs](https://crates.io/crates/cordis-rs) plugin framework.
3//!
4//! This crate is the data half of porting upstream Cordis' loader: it maps
5//! between config files on disk and an in-memory tree of
6//! [`Entry`] nodes, each described by [`EntryOptions`]. It deliberately
7//! knows nothing about *where plugins come from* and never starts or stops
8//! fibers — that is `cordis-loader`'s job, plugged in through the
9//! [`PluginResolver`] trait defined here.
10//!
11//! # Example
12//!
13//! ```no_run
14//! use cordis_include::{Document, EntryOptions, EntryTree, LoaderFile};
15//!
16//! # fn main() -> cordis_include::Result<()> {
17//! let file = LoaderFile::open("cordis.yml")?;
18//! let mut document = file.read()?;
19//!
20//! let tree = EntryTree::new();
21//! let diff = tree.update(document.entries)?;
22//! for entry in &diff.created {
23//!     println!("new entry {} ({})", entry.path(), entry.name());
24//! }
25//!
26//! // Persist generated ids and later edits back to the file.
27//! document.entries = tree.serialize();
28//! file.write(&document)?;
29//! # Ok(())
30//! # }
31//! ```
32//!
33//! # File format
34//!
35//! A file holds an ordered entry list; nested `group` arrays make groups.
36//! Object key order is preserved on round-trip, entry fields serialize as
37//! `id`, `name`, `disabled`, `inject`, `group`, `config` (config last), and
38//! unknown top-level keys are kept untouched — files stay diff-friendly.
39//!
40//! ```yaml
41//! entries:
42//!   - id: sched
43//!     name: group
44//!     group:
45//!       - name: adapter-http
46//!         config:
47//!           port: 8080
48//!           host: ${{ env.HOST }}
49//! ```
50//!
51//! `${{ env.NAME }}` templates substitute environment variables when config
52//! is handed to a plugin ([`Entry::resolved_config`]); the file itself keeps
53//! the template text. `!!js` scalars parse through the crate's own YAML
54//! dialect (the [`yaml`] module) and round-trip as expression nodes
55//! ([`Node::Expr`]). At the same hand-off point every expression evaluates
56//! through the [`expr`] subset — the `process.*` references the shipped
57//! bundles use; injected-context expressions (`ctx.*`, `dshHomePath(…)`)
58//! fail with a clear subset error. The `disabled` field takes the same
59//! `!!js` form (see [`Disabled`]), evaluated at activation through
60//! [`Entry::resolved_disabled`].
61//!
62//! # Patch lists
63//!
64//! Entry lists compose from *patch* files — bare top-level YAML arrays of
65//! [`PatchOptions`] rows (`id`-targeted overrides and `insert` lists), the
66//! bundle/profile assembly model. See the [`patch`] module for the apply,
67//! composition, provenance, and dump mechanisms.
68//!
69//! # Suspension
70//!
71//! Two suspend counters break the reload feedback loop: a file-level guard
72//! ([`LoaderFile::suspend`]) suppresses physical writes, and an entry-level
73//! guard ([`Entry::suspend`]) tells the loader that an entry's changes came
74//! from the file and must not be written back. The `watch` feature adds
75//! [`FileWatcher`], a debounced watcher that skips events observed while
76//! the file is suspended.
77//!
78//! # Not in scope
79//!
80//! Plugin resolution beyond the [`PluginResolver`] contract (static
81//! registries and dynamic libraries live in `cordis-loader`), fiber
82//! lifecycle, and cascading group semantics (`cordis-group`).
83
84// `deny` instead of `forbid` because the YAML dialect module wraps
85// `unsafe-libyaml`'s C-translation parser; every unsafe operation lives in
86// that one module, item-scoped behind `#[allow(unsafe_code)]` with SAFETY
87// notes (the same pattern `cordis-loader` uses for libloading).
88#![deny(unsafe_code)]
89#![warn(missing_docs)]
90
91pub mod entry;
92pub mod error;
93pub mod expr;
94pub mod file;
95pub mod interpolate;
96pub mod node;
97pub mod options;
98pub mod patch;
99pub mod resolver;
100pub mod tree;
101#[cfg(feature = "watch")]
102pub mod watch;
103pub mod yaml;
104
105pub use entry::{Entry, EntrySuspendGuard};
106pub use error::{IncludeError, Result};
107pub use expr::{evaluate, evaluate_node};
108pub use file::{Document, FileFormat, FileSuspendGuard, LoaderFile};
109pub use node::{Node, NodeMap};
110pub use options::{Disabled, EntryOptions, GROUP_NAME, IMPORT_NAME};
111pub use patch::{
112    DumpLayer, PatchOptions, Provenance, apply_entry_patches, compose_layers,
113    compose_with_provenance, load_optional_patches, load_overlay_patches, render_config_dump,
114    render_dump,
115};
116pub use resolver::PluginResolver;
117pub use tree::{EntryTree, RemovedEntry, TreeDiff};
118#[cfg(feature = "watch")]
119pub use watch::FileWatcher;
120pub use yaml::{emit_document, emit_entry_list, parse_document, parse_entry_list, parse_node};
121
122/// Lock a mutex tolerantly, treating a poisoned lock as unlocked.
123///
124/// Mirrors the pattern used inside `cordis-rs`: a panic in one thread must
125/// not cascade into `unwrap` failures elsewhere. The guarded state may be
126/// mid-update, which is acceptable for config trees.
127pub(crate) fn lock<T>(mutex: &std::sync::Mutex<T>) -> std::sync::MutexGuard<'_, T> {
128    mutex.lock().unwrap_or_else(|error| error.into_inner())
129}