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//! # Suspension
56//!
57//! Two suspend counters break the reload feedback loop: a file-level guard
58//! ([`LoaderFile::suspend`]) suppresses physical writes, and an entry-level
59//! guard ([`Entry::suspend`]) tells the loader that an entry's changes came
60//! from the file and must not be written back. The `watch` feature adds
61//! [`FileWatcher`], a debounced watcher that skips events observed while
62//! the file is suspended.
63//!
64//! # Not in scope
65//!
66//! Plugin resolution beyond the [`PluginResolver`] contract (static
67//! registries and dynamic libraries live in `cordis-loader`), fiber
68//! lifecycle, and cascading group semantics (`cordis-group`).
69
70#![forbid(unsafe_code)]
71#![warn(missing_docs)]
72
73pub mod entry;
74pub mod error;
75pub mod file;
76pub mod interpolate;
77pub mod node;
78pub mod options;
79pub mod resolver;
80pub mod tree;
81#[cfg(feature = "watch")]
82pub mod watch;
83
84pub use entry::{Entry, EntrySuspendGuard};
85pub use error::{IncludeError, Result};
86pub use file::{Document, FileFormat, FileSuspendGuard, LoaderFile};
87pub use node::{Node, NodeMap};
88pub use options::{EntryOptions, IMPORT_NAME};
89pub use resolver::PluginResolver;
90pub use tree::{EntryTree, RemovedEntry, TreeDiff};
91#[cfg(feature = "watch")]
92pub use watch::FileWatcher;
93
94/// Lock a mutex tolerantly, treating a poisoned lock as unlocked.
95///
96/// Mirrors the pattern used inside `cordis-rs`: a panic in one thread must
97/// not cascade into `unwrap` failures elsewhere. The guarded state may be
98/// mid-update, which is acceptable for config trees.
99pub(crate) fn lock<T>(mutex: &std::sync::Mutex<T>) -> std::sync::MutexGuard<'_, T> {
100    mutex.lock().unwrap_or_else(|error| error.into_inner())
101}