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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::rule_config_serde::RuleConfig;
use crate::types::LineLength;
use serde::ser::Serializer;
use serde::{Deserialize, Deserializer, 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), except when a table exceeds
/// `max-width` and auto-compacts: the effective output style is then
/// `compact`, so the delimiter row is aligned to the header column widths.
///
/// 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,
/// Spaces placed on each side of a cell's content, between the cell and the
/// dividing pipe. Configured values must be between 0 and 255.
///
/// - Unset (default): each style keeps its own padding — one space for
/// `aligned`, `aligned-no-space` and `compact`, none for `tight`.
/// - Set: the value applies to every style, so `pad-width = 0` gives aligned
/// columns with pipes flush against the content, and `pad-width = 2`
/// gives every style two spaces of padding.
///
/// `aligned-no-space` keeps its defining trait regardless: its delimiter row
/// is never surrounded by padding, and instead carries the padding width in
/// extra dashes so the column stays the same width as the content rows.
///
/// # Examples
///
/// ```toml
/// [MD060]
/// style = "aligned"
/// pad-width = 0 # |Name |Age|
/// ```
///
/// ```toml
/// [MD060]
/// style = "aligned"
/// pad-width = 2 # | Name | Age |
/// ```
#[serde(default, rename = "pad-width", deserialize_with = "deserialize_pad_width")]
pub pad_width: Option<usize>,
}
fn deserialize_pad_width<'de, D>(deserializer: D) -> Result<Option<usize>, D::Error>
where
D: Deserializer<'de>,
{
let value = Option::<usize>::deserialize(deserializer)?;
if value.is_some_and(|width| width > usize::from(u8::MAX)) {
return Err(serde::de::Error::custom("pad-width must be between 0 and 255"));
}
Ok(value)
}
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,
pad_width: None,
}
}
}
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_pad_width_defaults_to_unset() {
let cfg: MD060Config = toml::from_str("").unwrap();
assert_eq!(cfg.pad_width, None, "pad-width is unset by default");
}
#[test]
fn test_pad_width_kebab_case_key() {
let cfg: MD060Config = toml::from_str("pad-width = 2").unwrap();
assert_eq!(cfg.pad_width, Some(2), "pad-width counts spaces on each side");
}
#[test]
fn test_pad_width_configuration_bounds() {
let cfg: MD060Config = toml::from_str("pad-width = 255").unwrap();
assert_eq!(cfg.pad_width, Some(255));
for width in ["256", "9223372036854775807"] {
let err = toml::from_str::<MD060Config>(&format!("pad-width = {width}")).unwrap_err();
assert!(err.to_string().contains("pad-width must be between 0 and 255"));
}
assert!(toml::from_str::<MD060Config>("pad-width = -1").is_err());
}
#[test]
fn test_pad_width_zero_is_distinct_from_unset() {
let cfg: MD060Config = toml::from_str("pad-width = 0").unwrap();
assert_eq!(cfg.pad_width, Some(0), "pad-width = 0 is a setting, not an absent one");
}
#[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");
}
}