Skip to main content

cmakefmt/
lib.rs

1// SPDX-FileCopyrightText: Copyright 2026 Puneet Matharu
2//
3// SPDX-License-Identifier: MIT OR Apache-2.0
4
5//! `cmakefmt` is a fast, configurable CMake formatter.
6//!
7//! # Quick start
8//!
9//! Format a CMake source string with the default configuration:
10//!
11//! ```
12//! use cmakefmt::{format_source, Config};
13//!
14//! let cmake = "CMAKE_MINIMUM_REQUIRED(VERSION 3.20)\n";
15//! let formatted = format_source(cmake, &Config::default()).unwrap();
16//! assert_eq!(formatted, "cmake_minimum_required(VERSION 3.20)\n");
17//! ```
18//!
19//! To customise formatting, modify [`Config`] fields before passing it in:
20//!
21//! ```
22//! use cmakefmt::{format_source, Config, CaseStyle};
23//!
24//! let mut config = Config::default();
25//! config.line_width = 100;
26//! config.command_case = CaseStyle::Upper;
27//!
28//! let cmake = "target_link_libraries(mylib PUBLIC dep1)\n";
29//! let formatted = format_source(cmake, &config).unwrap();
30//! assert_eq!(formatted, "TARGET_LINK_LIBRARIES(mylib PUBLIC dep1)\n");
31//! ```
32//!
33//! # Organisation
34//!
35//! | Module | Purpose |
36//! |--------|---------|
37//! | [`config`] | Runtime configuration types and config-file loading |
38//! | [`error`] | Error and result types |
39//! | [`formatter`] | Source-to-source formatting pipeline |
40//! | [`parser`] | CMake parser and AST definitions |
41//! | [`spec`] | Built-in and user-extensible command registry |
42
43/// Runtime formatter configuration and config-file loading.
44pub mod config;
45/// Shared error types used across parsing, config loading, and formatting.
46pub mod error;
47/// Source-to-source formatting pipeline.
48pub mod formatter;
49/// CMake parser and AST definitions.
50pub mod parser;
51/// Built-in and user-extensible command specification registry.
52pub mod spec;
53
54// Recursive CMake file-discovery helpers used by the CLI.  Not part of the
55// library embedding API; hidden from generated documentation.
56#[cfg(feature = "cli")]
57#[doc(hidden)]
58pub mod files;
59
60// LSP server — only compiled when the `lsp` feature is enabled.
61#[cfg(feature = "lsp")]
62pub mod lsp;
63
64// WASM entry point — only compiled for wasm32 targets.
65#[cfg(target_arch = "wasm32")]
66pub mod wasm;
67
68// ── Configuration ────────────────────────────────────────────────────────────
69
70pub use config::{
71    CaseStyle, CommandConfig, Config, DangleAlign, FractionalTabPolicy, LineEnding,
72    PerCommandConfig,
73};
74
75#[cfg(all(not(target_arch = "wasm32"), feature = "cli"))]
76pub use config::{
77    convert_legacy_config_files, default_config_template, default_config_template_for,
78    generate_json_schema, render_effective_config, DumpConfigFormat,
79};
80
81// ── Errors ───────────────────────────────────────────────────────────────────
82
83pub use error::{Error, Result};
84
85// ── Formatting ───────────────────────────────────────────────────────────────
86
87pub use formatter::{
88    format_file, format_source, format_source_with_debug, format_source_with_registry,
89    format_source_with_registry_debug,
90};
91
92// ── Registry ─────────────────────────────────────────────────────────────────
93
94pub use spec::registry::CommandRegistry;