cuenv_cubes/
config.rs

1//! Formatter configuration generation
2//!
3//! This module handles automatic generation of formatter configuration files
4//! (biome.json, .prettierrc, rustfmt.toml, etc.) based on CUE schema format settings.
5
6use crate::cube::FormatConfig;
7use serde_json::json;
8
9/// Generate biome.json configuration from format config
10#[must_use]
11pub fn generate_biome_config(format: &FormatConfig) -> serde_json::Value {
12    json!({
13        "$schema": "https://biomejs.dev/schemas/1.4.1/schema.json",
14        "formatter": {
15            "enabled": true,
16            "indentStyle": format.indent,
17            "indentSize": format.indent_size.unwrap_or(2),
18            "lineWidth": format.line_width.unwrap_or(100)
19        },
20        "linter": {
21            "enabled": true
22        },
23        "javascript": {
24            "formatter": {
25                "quoteStyle": format.quotes.as_deref().unwrap_or("double"),
26                "trailingComma": format.trailing_comma.as_deref().unwrap_or("all"),
27                "semicolons": if format.semicolons.unwrap_or(true) { "always" } else { "asNeeded" }
28            }
29        }
30    })
31}
32
33/// Generate .prettierrc configuration from format config
34#[must_use]
35pub fn generate_prettier_config(format: &FormatConfig) -> serde_json::Value {
36    json!({
37        "useTabs": format.indent == "tab",
38        "tabWidth": format.indent_size.unwrap_or(2),
39        "printWidth": format.line_width.unwrap_or(100),
40        "singleQuote": format.quotes.as_deref() == Some("single"),
41        "trailingComma": format.trailing_comma.as_deref().unwrap_or("all"),
42        "semi": format.semicolons.unwrap_or(true)
43    })
44}
45
46/// Generate rustfmt.toml configuration from format config
47#[must_use]
48pub fn generate_rustfmt_config(format: &FormatConfig) -> String {
49    let edition = "2021"; // Default to 2021 edition
50    let max_width = format.line_width.unwrap_or(100);
51    let hard_tabs = format.indent == "tab";
52    let tab_spaces = format.indent_size.unwrap_or(4);
53
54    format!(
55        r#"edition = "{edition}"
56max_width = {max_width}
57hard_tabs = {hard_tabs}
58tab_spaces = {tab_spaces}
59use_small_heuristics = "Default"
60"#
61    )
62}
63
64#[cfg(test)]
65mod tests {
66    use super::*;
67
68    #[test]
69    fn test_generate_biome_config() {
70        let format = FormatConfig {
71            indent: "space".to_string(),
72            indent_size: Some(2),
73            line_width: Some(100),
74            quotes: Some("single".to_string()),
75            semicolons: Some(false),
76            trailing_comma: Some("es5".to_string()),
77        };
78
79        let config = generate_biome_config(&format);
80        assert_eq!(config["formatter"]["indentStyle"], "space");
81        assert_eq!(config["formatter"]["indentSize"], 2);
82        assert_eq!(config["javascript"]["formatter"]["quoteStyle"], "single");
83    }
84
85    #[test]
86    fn test_generate_prettier_config() {
87        let format = FormatConfig {
88            indent: "tab".to_string(),
89            indent_size: Some(4),
90            line_width: Some(120),
91            quotes: Some("single".to_string()),
92            semicolons: Some(false),
93            trailing_comma: Some("none".to_string()),
94        };
95
96        let config = generate_prettier_config(&format);
97        assert_eq!(config["useTabs"], true);
98        assert_eq!(config["tabWidth"], 4);
99        assert_eq!(config["singleQuote"], true);
100        assert_eq!(config["semi"], false);
101    }
102
103    #[test]
104    fn test_generate_rustfmt_config() {
105        let format = FormatConfig {
106            indent: "space".to_string(),
107            indent_size: Some(4),
108            line_width: Some(100),
109            ..Default::default()
110        };
111
112        let config = generate_rustfmt_config(&format);
113        assert!(config.contains("edition = \"2021\""));
114        assert!(config.contains("max_width = 100"));
115        assert!(config.contains("hard_tabs = false"));
116        assert!(config.contains("tab_spaces = 4"));
117    }
118}