dbmd_core/lib.rs
1//! `dbmd-core` — the reference library for **db.md**, the open database in
2//! plain files.
3//!
4//! db.md is one directory: raw evidence in `sources/`, atomic typed data plus
5//! curator-synthesized conclusions (`meta-type: conclusion`) in `records/`, and
6//! a single `DB.md` config file at the root. Records are markdown files with
7//! YAML frontmatter;
8//! relationships are wiki-links; the index is the derived, write-through
9//! `index.md` / `index.jsonl` catalog plus embedded ripgrep.
10//!
11//! This crate owns **all** toolkit logic. The `dbmd` binary (`dbmd-cli`) is a
12//! thin wrapper that parses args, calls into here, and formats output. Any
13//! Rust tool wanting to be db.md-aware can `cargo add dbmd-core` and get the
14//! full library — the same shape as ripgrep, where the `grep`/`ignore` libs do
15//! the work and `rg` is a thin CLI.
16//!
17//! # Hard invariants this crate is built to uphold
18//!
19//! - **Zero AI/LLM dependencies.** No provider SDKs, no API keys, no model
20//! calls, no embeddings, no vectors, no ANN — anywhere, ever. The agent
21//! driving `dbmd` is the semantic layer; `dbmd` is a deterministic tool.
22//! - **The interactive loop is O(changed), never O(store).** Loop ops
23//! ([`graph::backlinks`], [`validate::validate_working_set`],
24//! [`index::Index::on_write`], …) never call [`store::Store::walk`] on a
25//! non-empty changed set. The one documented exception is
26//! [`validate::validate_working_set`], which falls back to a full sweep only
27//! when handed an empty changed set (the vacuous-pass guard). Whole-store
28//! walks otherwise belong only to SWEEP ops ([`validate::validate_all`],
29//! [`index::Index::rebuild_all`], [`stats`]).
30//! - **Wiki-links are full store-relative paths.** A short-form wiki-link is a
31//! validation error ([`validate`] code `WIKI_LINK_SHORT_FORM`).
32//! - **Embedded ripgrep.** Free-text body search uses the `grep` + `ignore`
33//! crates in-process; the toolkit never bundles or shells out to `rg`.
34//! Structured loop reads ([`graph::backlinks`], [`query::Query`]) ride the
35//! `index.jsonl` sidecars instead, never a frontmatter tree scan.
36
37pub mod assets;
38pub mod edit;
39pub mod emit;
40pub mod extract;
41pub mod fsx;
42pub mod graph;
43pub mod index;
44// The link.md CLIENT (feature `link`, default-on): the five interconnect
45// verbs — resolve / sync / grant / propose / subscribe — spoken against a
46// user-configured hub. One binary, two specs (the git precedent); the db.md
47// FORMAT is untouched — a store never needs link.md to be valid db.md, and a
48// format-only consumer drops this module (and its HTTP/TLS closure) with
49// `default-features = false`. Deliberately NOT re-exported at the crate root:
50// the root re-exports are the format toolkit's locked interface, and the wire
51// client reads best module-qualified (`linkmd::HubConfig`).
52#[cfg(feature = "link")]
53pub mod linkmd;
54#[cfg(feature = "link")]
55mod linkmd_sync_policy;
56#[cfg(feature = "link")]
57pub mod linkmd_v2;
58pub mod log;
59pub mod parser;
60pub mod projection;
61pub mod query;
62pub mod render;
63pub mod stats;
64pub mod store;
65pub mod summary;
66pub mod time;
67pub mod ulid;
68pub mod validate;
69pub mod watch;
70
71// ── Shared public types, re-exported at the crate root ──────────────────────
72//
73// These are the locked interface every other crate and module builds against.
74
75pub use assets::{AssetRecord, Declaration, ScanReport, StatusReport, VerifyReport};
76pub use extract::{ExtractError, Extracted, Format, MetaValue};
77pub use fsx::{write_atomic, write_atomic_new};
78pub use graph::ContextSlice;
79pub use index::{Index, IndexLevel, IndexRecord};
80pub use log::{Log, LogEntry, LogKind};
81pub use parser::{
82 Config, FieldSpec, Frontmatter, MarkdownLink, ParseError, Schema, Section, Shape, WikiLink,
83};
84pub use query::Query;
85pub use render::{Outline, Tree};
86pub use store::{infer_type_from_path, layer_for_type, Layer, NotAStore, Store, StoreError};
87pub use time::now;
88pub use validate::{Issue, Severity};
89
90/// Crate-wide result alias over [`Error`].
91pub type Result<T> = std::result::Result<T, Error>;
92
93/// Top-level error for `dbmd-core` operations.
94///
95/// Module-specific errors ([`ParseError`], [`StoreError`], [`NotAStore`])
96/// convert into this so a CLI command can bubble a single error type while
97/// preserving the structured variant for `--json` rendering.
98#[derive(Debug, thiserror::Error)]
99pub enum Error {
100 /// The path is not a db.md store (no `DB.md` at the root). Surfaced as the
101 /// machine-parseable code `NOT_A_STORE` with a non-zero exit.
102 #[error(transparent)]
103 NotAStore(#[from] NotAStore),
104
105 /// A store-level operation failed (walk, locate, shard, sidecar read).
106 #[error(transparent)]
107 Store(#[from] StoreError),
108
109 /// A markdown / frontmatter / `DB.md` parse failed.
110 #[error(transparent)]
111 Parse(#[from] ParseError),
112
113 /// A write was refused by a `DB.md ## Policies` rule (e.g. a frozen page).
114 /// Carries the structured validation code so the CLI can emit it verbatim.
115 #[error("write refused by policy ({code}): {message}")]
116 Policy {
117 /// The structured issue code, e.g. `"POLICY_FROZEN_PAGE"`.
118 code: &'static str,
119 /// Human-readable explanation.
120 message: String,
121 },
122
123 /// An underlying I/O failure.
124 #[error(transparent)]
125 Io(#[from] std::io::Error),
126}