docs_symbols/lib.rs
1//! docs-symbols: symbol-anchored source extraction for docs sites
2//! (docs-core extension program, `SPEC.md` REQ-S).
3//!
4//! A docs compiler resolves ```include-symbol fences against real source
5//! files: this crate parses the file with tree-sitter, walks the AST by
6//! ordered name matching (modules, items, impls keyed on type name), and
7//! returns the symbol's exact byte span plus a position-independent
8//! anchor ID derived from the symbol path itself (REQ-S1/S3). Unknown
9//! paths fail with typed errors naming the offending segment (REQ-S2).
10//!
11//! ```
12//! use docs_symbols::{extract, Language};
13//!
14//! let source = "mod engine {\n fn process_order() -> u32 { 7 }\n}\n";
15//! let span = extract(source, Language::Rust, "engine::process_order").unwrap();
16//! assert_eq!(span.start_line, 2);
17//! assert!(span.code.contains("fn process_order"));
18//! assert!(span.anchor_id.starts_with("dsym-rust-"));
19//! ```
20//!
21//! # Scope (0.1, honest)
22//!
23//! In: Rust only (`Language::Rust`); item kinds function/struct/enum/
24//! trait/type/const/static/mod/macro; impl methods keyed on the impl's
25//! type name (inherent and trait impls alike); document order as the
26//! deterministic first-match tiebreak; strict parsing (any ERROR or
27//! MISSING node rejects the source).
28//! Out: other languages (grammars are added per language, each gated by
29//! its own tests), generic-parameter-qualified paths (`Vec::<u8>::new`),
30//! trait-name segments on trait impls, associated consts via type paths,
31//! and doc-comment inclusion in spans (the span is the item node). Each
32//! absence is a later increment, not silent.
33//!
34//! # Layer
35//!
36//! L0 - leaf. Zero estate-internal dependencies; the external floor is
37//! `tree-sitter` and `tree-sitter-rust`.
38
39#![deny(missing_docs)]
40#![forbid(unsafe_code)]
41
42mod error;
43mod resolve;
44mod span;
45
46pub use error::SymbolError;
47pub use resolve::extract;
48pub use span::Span;
49
50/// Languages with a grammar compiled into this crate.
51#[derive(Debug, Clone, Copy, PartialEq, Eq)]
52pub enum Language {
53 /// The Rust grammar (`tree-sitter-rust`).
54 Rust,
55}
56
57impl Language {
58 /// The language label used in anchor IDs and span metadata.
59 pub fn label(self) -> &'static str {
60 match self {
61 Self::Rust => "rust",
62 }
63 }
64
65 /// The compiled tree-sitter grammar.
66 pub fn grammar(self) -> tree_sitter::Language {
67 match self {
68 Self::Rust => tree_sitter_rust::LANGUAGE.into(),
69 }
70 }
71}
72
73/// The crate's version, for callers that report it.
74pub const VERSION: &str = env!("CARGO_PKG_VERSION");