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 emit;
39pub mod extract;
40pub mod fsx;
41pub mod graph;
42pub mod index;
43// The link.md CLIENT (feature `link`, default-on): the five interconnect
44// verbs — resolve / sync / grant / propose / subscribe — spoken against a
45// user-configured hub. One binary, two specs (the git precedent); the db.md
46// FORMAT is untouched — a store never needs link.md to be valid db.md, and a
47// format-only consumer drops this module (and its HTTP/TLS closure) with
48// `default-features = false`. Deliberately NOT re-exported at the crate root:
49// the root re-exports are the format toolkit's locked interface, and the wire
50// client reads best module-qualified (`linkmd::HubConfig`).
51#[cfg(feature = "link")]
52pub mod linkmd;
53pub mod log;
54pub mod parser;
55pub mod query;
56pub mod render;
57pub mod stats;
58pub mod store;
59pub mod summary;
60pub mod time;
61pub mod ulid;
62pub mod validate;
63
64// ── Shared public types, re-exported at the crate root ──────────────────────
65//
66// These are the locked interface every other crate and module builds against.
67
68pub use assets::{AssetRecord, Declaration, ScanReport, StatusReport, VerifyReport};
69pub use extract::{ExtractError, Extracted, Format, MetaValue};
70pub use fsx::{write_atomic, write_atomic_new};
71pub use graph::ContextSlice;
72pub use index::{Index, IndexLevel, IndexRecord};
73pub use log::{Log, LogEntry, LogKind};
74pub use parser::{
75 Config, FieldSpec, Frontmatter, MarkdownLink, ParseError, Schema, Section, Shape, WikiLink,
76};
77pub use query::Query;
78pub use render::{Outline, Tree};
79pub use store::{infer_type_from_path, layer_for_type, Layer, NotAStore, Store, StoreError};
80pub use time::now;
81pub use validate::{Issue, Severity};
82
83/// Crate-wide result alias over [`Error`].
84pub type Result<T> = std::result::Result<T, Error>;
85
86/// Top-level error for `dbmd-core` operations.
87///
88/// Module-specific errors ([`ParseError`], [`StoreError`], [`NotAStore`])
89/// convert into this so a CLI command can bubble a single error type while
90/// preserving the structured variant for `--json` rendering.
91#[derive(Debug, thiserror::Error)]
92pub enum Error {
93 /// The path is not a db.md store (no `DB.md` at the root). Surfaced as the
94 /// machine-parseable code `NOT_A_STORE` with a non-zero exit.
95 #[error(transparent)]
96 NotAStore(#[from] NotAStore),
97
98 /// A store-level operation failed (walk, locate, shard, sidecar read).
99 #[error(transparent)]
100 Store(#[from] StoreError),
101
102 /// A markdown / frontmatter / `DB.md` parse failed.
103 #[error(transparent)]
104 Parse(#[from] ParseError),
105
106 /// A write was refused by a `DB.md ## Policies` rule (e.g. a frozen page).
107 /// Carries the structured validation code so the CLI can emit it verbatim.
108 #[error("write refused by policy ({code}): {message}")]
109 Policy {
110 /// The structured issue code, e.g. `"POLICY_FROZEN_PAGE"`.
111 code: &'static str,
112 /// Human-readable explanation.
113 message: String,
114 },
115
116 /// An underlying I/O failure.
117 #[error(transparent)]
118 Io(#[from] std::io::Error),
119}