cmakefmt/lib.rs
1// SPDX-FileCopyrightText: Copyright 2026 Puneet Matharu
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5#![cfg_attr(docsrs, feature(doc_cfg))]
6
7//! `cmakefmt` is a fast, configurable CMake formatter.
8//!
9//! # Quick start
10//!
11//! Format a CMake source string with the default configuration:
12//!
13//! ```
14//! use cmakefmt::{format_source, Config};
15//!
16//! let cmake = "CMAKE_MINIMUM_REQUIRED(VERSION 3.20)\n";
17//! let formatted = format_source(cmake, &Config::default()).unwrap();
18//! assert_eq!(formatted, "cmake_minimum_required(VERSION 3.20)\n");
19//! ```
20//!
21//! To customise formatting, modify [`Config`] fields before passing it in:
22//!
23//! ```
24//! use cmakefmt::{format_source, Config, CaseStyle};
25//!
26//! let mut config = Config::default();
27//! config.line_width = 100;
28//! config.command_case = CaseStyle::Upper;
29//!
30//! let cmake = "target_link_libraries(mylib PUBLIC dep1)\n";
31//! let formatted = format_source(cmake, &config).unwrap();
32//! assert_eq!(formatted, "TARGET_LINK_LIBRARIES(mylib PUBLIC dep1)\n");
33//! ```
34//!
35//! # Organisation
36//!
37//! | Module | Purpose |
38//! |--------|---------|
39//! | [`config`] | Runtime configuration types and config-file loading |
40//! | [`error`] | Error and result types |
41//! | [`formatter`] | Source-to-source formatting pipeline |
42//! | [`parser`] | CMake parser and AST definitions |
43//! | [`spec`] | Built-in and user-extensible command registry |
44//!
45//! # Entry points (re-exported at the crate root)
46//!
47//! | Item | Purpose |
48//! |------|---------|
49//! | [`format_source`] / [`format_source_with_registry`] | Format a source string |
50//! | [`format_parsed_file`] | Format an already-parsed [`parser::ast::File`] |
51//! | [`format_source_with_debug`] / [`format_source_with_registry_debug`] | Format + collect debug decision log |
52//! | [`Config`], [`CommandConfig`], [`PerCommandConfig`] | Runtime configuration |
53//! | [`CaseStyle`], [`DangleAlign`], [`LineEnding`], [`FractionalTabPolicy`], [`Experimental`] | Config enums |
54//! | [`CommandRegistry`] | Built-in + user-override command specs |
55//! | [`Error`], [`Result`] | Crate-level error types |
56//!
57//! # Features
58//!
59//! | Feature | Default | Purpose |
60//! |---------|---------|---------|
61//! | `cli` | ✔ | Enables the `cmakefmt` binary plus CLI-oriented public API (`convert_legacy_config_files`, `default_config_template_for`, `generate_json_schema`, `render_effective_config`, `DumpConfigFormat`). Implies `lsp`. |
62//! | `lsp` | ✔ (via `cli`) | Compiles the `lsp::run` Language Server Protocol entry point. |
63//!
64//! The crate also has a separate target path: when compiled for
65//! `wasm32`, `wasm::format` and friends are exposed via
66//! `wasm-bindgen` for the browser playground.
67
68/// Runtime formatter configuration and config-file loading.
69pub mod config;
70/// Shared error types used across parsing, config loading, and formatting.
71pub mod error;
72/// Source-to-source formatting pipeline.
73pub mod formatter;
74/// CMake parser and AST definitions.
75pub mod parser;
76/// Built-in and user-extensible command specification registry.
77pub mod spec;
78
79// Recursive CMake file-discovery helpers used by the CLI. Not part of the
80// library embedding API; hidden from generated documentation.
81#[cfg(feature = "cli")]
82#[doc(hidden)]
83pub mod files;
84
85// AST / parse-tree dumping helpers used by the `dump` subcommand.
86#[cfg(feature = "cli")]
87#[doc(hidden)]
88pub mod dump;
89
90// LSP server — only compiled when the `lsp` feature is enabled.
91#[cfg(feature = "lsp")]
92#[cfg_attr(docsrs, doc(cfg(feature = "lsp")))]
93pub mod lsp;
94
95// WASM entry point — only compiled for wasm32 targets.
96#[cfg(target_arch = "wasm32")]
97#[cfg_attr(docsrs, doc(cfg(target_arch = "wasm32")))]
98pub mod wasm;
99
100// ── Configuration ────────────────────────────────────────────────────────────
101
102pub use config::{
103 CaseStyle, CommandConfig, Config, ContinuationAlign, DangleAlign, Experimental,
104 FractionalTabPolicy, LineEnding, PerCommandConfig,
105};
106
107pub use config::default_config_template;
108
109#[cfg(all(not(target_arch = "wasm32"), feature = "cli"))]
110#[cfg_attr(docsrs, doc(cfg(feature = "cli")))]
111pub use config::{
112 convert_legacy_config_files, default_config_template_for, generate_json_schema,
113 render_effective_config, DumpConfigFormat,
114};
115
116// ── Errors ───────────────────────────────────────────────────────────────────
117
118pub use error::{Error, Result};
119
120// ── Formatting ───────────────────────────────────────────────────────────────
121
122pub use formatter::{
123 format_parsed_file, format_source, format_source_with_debug, format_source_with_registry,
124 format_source_with_registry_debug,
125};
126
127// ── Registry ─────────────────────────────────────────────────────────────────
128
129pub use spec::registry::CommandRegistry;