| Crates.io | sql_docs |
| lib.rs | sql_docs |
| version | 1.0.11 |
| created_at | 2025-12-30 04:03:53.897455+00 |
| updated_at | 2026-01-16 14:01:57.932886+00 |
| description | A crate for parsing comments from sql files and using them for documentation generation |
| homepage | |
| repository | https://github.com/rpg-alex/sql-docs |
| max_upload_size | |
| id | 2012044 |
| size | 172,741 |
This crate extracts documentation from SQL files by parsing:
sqlparser)Then comments are attached to the SQL structures they describe, producing a structured, queryable documentation model.
With a directory structured like this:
sql_dir/
└── users.sql
and the content of users.sql being:
/* Table storing user accounts */
CREATE TABLE users (
/* Primary key for each user */
id INTEGER PRIMARY KEY,
-- The user's login name
username VARCHAR(255) NOT NULL,
/* User's email address */
email VARCHAR(255) UNIQUE NOT NULL
);
A rudimentary implementation can be generated with:
use sql_docs::SqlDoc;
use sql_docs::error::DocError;
use std::{env, fs};
fn main() -> Result<(), DocError> {
// Temporary directory and file created for demonstration purposes
let base = env::temp_dir().join("tmp_dir");
let _ = fs::remove_dir_all(&base);
fs::create_dir_all(&base)?;
let example = base.join("example.sql");
fs::write(
&example,
r#"/* Table storing user accounts */
CREATE TABLE users (
/* Primary key for each user */
id INTEGER PRIMARY KEY,
-- The user's login name
username VARCHAR(255) NOT NULL,
/* User's email address */
email VARCHAR(255) UNIQUE NOT NULL
);"#,
)?;
// Extract documentation from a single file
let docs = SqlDoc::from_path(&example).build()?;
// Or extract recursively from a directory
// let docs = SqlDoc::from_dir(&base).build()?;
// Retrieve a specific table
let users = docs.table("users", None)?;
// Table name
assert_eq!(users.name(), "users");
// Optional table-level documentation
assert_eq!(users.doc(), Some("Table storing user accounts"));
// Path to the source file
assert_eq!(users.path(), Some(example.as_ref()));
let _ = fs::remove_dir_all(&base);
Ok(())
}
These are the primary entry points most users will interact with:
SqlDoc::from_path Build documentation from a single .sql file.SqlDoc::from_dir Recursively scan a directory for .sql files and build documentation.SqlDocBuilder::build Finalize the builder and produce a [SqlDoc].SqlDocBuilder::deny Exclude specific files by full path.SqlDocBuilder::flatten_multiline Flatten multiline comments into a single line.This crate is designed for generating documentation from SQL schemas by attaching comments to:
using only comments that immediately precede the items they describe.
This makes it well-suited for: