1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
use crate::rule_config_serde::RuleConfig;
use crate::types::LineLength;
use serde::ser::Serializer;
use serde::{Deserialize, Serialize};
/// Controls how cell text is aligned within padded columns.
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub enum ColumnAlign {
/// Use alignment indicators from delimiter row (`:---`, `:---:`, `---:`)
#[default]
Auto,
/// Force all columns to left-align text
Left,
/// Force all columns to center text
Center,
/// Force all columns to right-align text
Right,
}
impl Serialize for ColumnAlign {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match self {
ColumnAlign::Auto => serializer.serialize_str("auto"),
ColumnAlign::Left => serializer.serialize_str("left"),
ColumnAlign::Center => serializer.serialize_str("center"),
ColumnAlign::Right => serializer.serialize_str("right"),
}
}
}
impl<'de> Deserialize<'de> for ColumnAlign {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
match s.to_lowercase().as_str() {
"auto" => Ok(ColumnAlign::Auto),
"left" => Ok(ColumnAlign::Left),
"center" => Ok(ColumnAlign::Center),
"right" => Ok(ColumnAlign::Right),
_ => Err(serde::de::Error::custom(format!(
"Invalid column-align value: {s}. Valid options: auto, left, center, right"
))),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct MD060Config {
#[serde(default = "default_enabled")]
pub enabled: bool,
#[serde(
default = "default_style",
serialize_with = "serialize_style",
deserialize_with = "deserialize_style"
)]
pub style: String,
/// Maximum table width before auto-switching to compact mode.
///
/// - `0` (default): Inherit from MD013's `line-length` setting
/// - Non-zero: Explicit max width threshold
///
/// When a table's aligned width would exceed this limit, MD060 automatically
/// uses compact formatting instead (minimal padding) to prevent excessively
/// long lines. This matches the behavior of Prettier's table formatting.
///
/// # Examples
///
/// ```toml
/// [MD013]
/// line-length = 100
///
/// [MD060]
/// style = "aligned"
/// max-width = 0 # Uses MD013's line-length (100)
/// ```
///
/// ```toml
/// [MD060]
/// style = "aligned"
/// max-width = 120 # Explicit threshold, independent of MD013
/// ```
#[serde(default = "default_max_width", rename = "max-width")]
pub max_width: LineLength,
/// Controls how cell text is aligned within the padded column width.
///
/// - `auto` (default): Use alignment indicators from delimiter row (`:---`, `:---:`, `---:`)
/// - `left`: Force all columns to left-align text
/// - `center`: Force all columns to center text
/// - `right`: Force all columns to right-align text
///
/// Only applies when `style = "aligned"` or `style = "aligned-no-space"`.
///
/// # Examples
///
/// ```toml
/// [MD060]
/// style = "aligned"
/// column-align = "center" # Center all cell text
/// ```
#[serde(default, rename = "column-align")]
pub column_align: ColumnAlign,
/// Controls alignment specifically for the header row.
///
/// When set, overrides `column-align` for the header row only.
/// If not set, falls back to `column-align`.
///
/// # Examples
///
/// ```toml
/// [MD060]
/// style = "aligned"
/// column-align-header = "center" # Center header text
/// column-align-body = "left" # Left-align body text
/// ```
#[serde(default, rename = "column-align-header")]
pub column_align_header: Option<ColumnAlign>,
/// Controls alignment specifically for body rows (non-header, non-delimiter).
///
/// When set, overrides `column-align` for body rows only.
/// If not set, falls back to `column-align`.
///
/// # Examples
///
/// ```toml
/// [MD060]
/// style = "aligned"
/// column-align-header = "center" # Center header text
/// column-align-body = "left" # Left-align body text
/// ```
#[serde(default, rename = "column-align-body")]
pub column_align_body: Option<ColumnAlign>,
/// Controls whether the last column in body rows is loosely formatted.
///
/// - `false` (default): All columns padded to equal width across all rows.
/// - `true`: The last column width is capped at the header text width.
/// Body cells shorter than the header are padded to the header width.
/// Body cells longer than the header extend beyond without padding.
///
/// Only applies when `style = "aligned"` or `style = "aligned-no-space"`.
///
/// # Examples
///
/// ```toml
/// [MD060]
/// style = "aligned"
/// loose-last-column = true
/// ```
#[serde(default, rename = "loose-last-column")]
pub loose_last_column: bool,
/// Pads the delimiter row's dashes to match header column widths under
/// `compact` and `tight` styles.
///
/// - `false` (default): delimiter cells use the minimum dash count.
/// - `true`: delimiter pipe positions align with header pipe positions;
/// body rows remain compact/tight and are not padded.
///
/// No effect under `aligned` / `aligned-no-space` (those styles already
/// align the delimiter row by construction).
///
/// Mirrors markdownlint MD060's `aligned_delimiter` option; the snake_case
/// alias is accepted for cross-tool compatibility.
#[serde(default, rename = "aligned-delimiter", alias = "aligned_delimiter")]
pub aligned_delimiter: bool,
}
impl Default for MD060Config {
fn default() -> Self {
Self {
enabled: default_enabled(),
style: default_style(),
max_width: default_max_width(),
column_align: ColumnAlign::Auto,
column_align_header: None,
column_align_body: None,
loose_last_column: false,
aligned_delimiter: false,
}
}
}
fn default_enabled() -> bool {
false
}
fn default_style() -> String {
"any".to_string()
}
fn default_max_width() -> LineLength {
LineLength::from_const(0) // 0 = inherit from MD013
}
fn serialize_style<S>(style: &str, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(style)
}
fn deserialize_style<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: serde::Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let normalized = s.trim().to_ascii_lowercase().replace('_', "-");
let valid_styles = ["aligned", "aligned-no-space", "compact", "tight", "any"];
if valid_styles.contains(&normalized.as_str()) {
Ok(normalized)
} else {
Err(serde::de::Error::custom(format!(
"Invalid table format style: {s}. Valid options: aligned, aligned-no-space, compact, tight, any"
)))
}
}
impl RuleConfig for MD060Config {
const RULE_NAME: &'static str = "MD060";
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_style_accepts_hyphen_and_underscore_variants() {
let kebab_case: MD060Config = toml::from_str("style = \"aligned-no-space\"").unwrap();
assert_eq!(kebab_case.style, "aligned-no-space");
let snake_case: MD060Config = toml::from_str("style = \"aligned_no_space\"").unwrap();
assert_eq!(snake_case.style, "aligned-no-space");
}
#[test]
fn test_style_normalizes_case_for_compatibility() {
let uppercase: MD060Config = toml::from_str("style = \"ALIGNED_NO_SPACE\"").unwrap();
assert_eq!(uppercase.style, "aligned-no-space");
}
#[test]
fn test_aligned_delimiter_default_is_false() {
let cfg: MD060Config = toml::from_str("").unwrap();
assert!(!cfg.aligned_delimiter, "aligned-delimiter defaults to false");
}
#[test]
fn test_aligned_delimiter_kebab_case_key() {
let cfg: MD060Config = toml::from_str("aligned-delimiter = true").unwrap();
assert!(cfg.aligned_delimiter, "kebab-case aligned-delimiter is accepted");
}
#[test]
fn test_aligned_delimiter_snake_case_alias_for_markdownlint_parity() {
// markdownlint uses `aligned_delimiter` (snake_case). rumdl accepts both for compatibility.
let cfg: MD060Config = toml::from_str("aligned_delimiter = true").unwrap();
assert!(cfg.aligned_delimiter, "snake_case aligned_delimiter alias is accepted");
}
}