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. There is no expression evaluation.
54//!
55//! # Patch lists
56//!
57//! Entry lists compose from *patch* files — bare top-level YAML arrays of
58//! [`PatchOptions`] rows (`id`-targeted overrides and `insert` lists), the
59//! bundle/profile assembly model. See the [`patch`] module for the apply,
60//! composition, provenance, and dump mechanisms.
61//!
62//! # Suspension
63//!
64//! Two suspend counters break the reload feedback loop: a file-level guard
65//! ([`LoaderFile::suspend`]) suppresses physical writes, and an entry-level
66//! guard ([`Entry::suspend`]) tells the loader that an entry's changes came
67//! from the file and must not be written back. The `watch` feature adds
68//! [`FileWatcher`], a debounced watcher that skips events observed while
69//! the file is suspended.
70//!
71//! # Not in scope
72//!
73//! Plugin resolution beyond the [`PluginResolver`] contract (static
74//! registries and dynamic libraries live in `cordis-loader`), fiber
75//! lifecycle, and cascading group semantics (`cordis-group`).
76
77#![forbid(unsafe_code)]
78#![warn(missing_docs)]
79
80pub mod entry;
81pub mod error;
82pub mod file;
83pub mod interpolate;
84pub mod node;
85pub mod options;
86pub mod patch;
87pub mod resolver;
88pub mod tree;
89#[cfg(feature = "watch")]
90pub mod watch;
91
92pub use entry::{Entry, EntrySuspendGuard};
93pub use error::{IncludeError, Result};
94pub use file::{Document, FileFormat, FileSuspendGuard, LoaderFile};
95pub use node::{Node, NodeMap};
96pub use options::{EntryOptions, GROUP_NAME, IMPORT_NAME};
97pub use patch::{
98    DumpLayer, PatchOptions, Provenance, apply_entry_patches, compose_layers,
99    compose_with_provenance, load_optional_patches, load_overlay_patches, render_config_dump,
100    render_dump,
101};
102pub use resolver::PluginResolver;
103pub use tree::{EntryTree, RemovedEntry, TreeDiff};
104#[cfg(feature = "watch")]
105pub use watch::FileWatcher;
106
107/// Lock a mutex tolerantly, treating a poisoned lock as unlocked.
108///
109/// Mirrors the pattern used inside `cordis-rs`: a panic in one thread must
110/// not cascade into `unwrap` failures elsewhere. The guarded state may be
111/// mid-update, which is acceptable for config trees.
112pub(crate) fn lock<T>(mutex: &std::sync::Mutex<T>) -> std::sync::MutexGuard<'_, T> {
113    mutex.lock().unwrap_or_else(|error| error.into_inner())
114}