Skip to main content

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