rusdox 1.0.0

Generate DOCX and PDF from YAML at Rust speed.
Documentation
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
//! Configuration model for RusDox document and preview generation.
//!
//! The recommended format is TOML because it supports comments and is easy to
//! hand-edit. JSON is also supported for machine-generated workflows.

use std::fs;
use std::path::{Path, PathBuf};

use serde::{Deserialize, Serialize};

use crate::{DocxError, Result};

/// Top-level RusDox configuration.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct RusdoxConfig {
    /// Friendly profile name shown by tooling.
    pub profile_name: String,
    /// Output folder behavior for generated artifacts.
    pub output: OutputConfig,
    /// Font and size settings for common document blocks.
    pub typography: TypographyConfig,
    /// Paragraph and cell spacing settings in twips (DOCX units).
    pub spacing: SpacingConfig,
    /// Shared color palette used by helper APIs.
    pub colors: ColorConfig,
    /// Table styling and sizing defaults.
    pub table: TableConfig,
    /// PDF preview rendering settings.
    pub pdf: PdfConfig,
}

impl Default for RusdoxConfig {
    fn default() -> Self {
        Self {
            profile_name: "RusDox Default".to_string(),
            output: OutputConfig::default(),
            typography: TypographyConfig::default(),
            spacing: SpacingConfig::default(),
            colors: ColorConfig::default(),
            table: TableConfig::default(),
            pdf: PdfConfig::default(),
        }
    }
}

impl RusdoxConfig {
    /// Loads a configuration from a file path.
    ///
    /// `.toml` and `.json` are supported.
    pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();
        let content = fs::read_to_string(path)?;
        let extension = path
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or_default()
            .to_ascii_lowercase();

        match extension.as_str() {
            "json" => Self::from_json_str(&content),
            "toml" | "" => Self::from_toml_str(&content),
            other => Err(DocxError::parse(format!(
                "unsupported config extension '{other}', expected .toml or .json"
            ))),
        }
    }

    /// Loads configuration if the file exists, otherwise returns defaults.
    pub fn load_from_path_or_default(path: impl AsRef<Path>) -> Result<Self> {
        let path = path.as_ref();
        if path.exists() {
            Self::load_from_path(path)
        } else {
            Ok(Self::default())
        }
    }

    /// Loads a local config when present, otherwise falls back to the user-level config path.
    ///
    /// If neither exists, defaults are returned.
    pub fn load_local_or_user_default(local_path: impl AsRef<Path>) -> Result<Self> {
        Self::load_local_or_user_default_with_user_path(
            local_path.as_ref(),
            default_user_config_path().as_deref(),
        )
    }

    /// Parses a TOML configuration string.
    pub fn from_toml_str(content: &str) -> Result<Self> {
        toml::from_str(content)
            .map_err(|error| DocxError::parse(format!("invalid TOML config: {error}")))
    }

    /// Parses a JSON configuration string.
    pub fn from_json_str(content: &str) -> Result<Self> {
        serde_json::from_str(content)
            .map_err(|error| DocxError::parse(format!("invalid JSON config: {error}")))
    }

    /// Serializes the current configuration to JSON.
    pub fn to_json_pretty(&self) -> Result<String> {
        serde_json::to_string_pretty(self)
            .map_err(|error| DocxError::parse(format!("failed to serialize JSON config: {error}")))
    }

    /// Serializes the current configuration to TOML.
    pub fn to_toml_pretty(&self) -> Result<String> {
        toml::to_string_pretty(self)
            .map_err(|error| DocxError::parse(format!("failed to serialize TOML config: {error}")))
    }

    /// Saves the current configuration to disk.
    ///
    /// `.toml` and `.json` are supported. If no extension is provided,
    /// TOML is used by default.
    pub fn save_to_path(&self, path: impl AsRef<Path>) -> Result<()> {
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }

        let extension = path
            .extension()
            .and_then(|ext| ext.to_str())
            .unwrap_or("toml")
            .to_ascii_lowercase();

        let content = match extension.as_str() {
            "json" => self.to_json_pretty()?,
            "toml" | "" => self.to_toml_pretty()?,
            other => {
                return Err(DocxError::parse(format!(
                    "unsupported config extension '{other}', expected .toml or .json"
                )))
            }
        };

        crate::io_utils::atomic_write(path, content.as_bytes())
    }

    /// Writes a commented default TOML template to disk.
    pub fn write_toml_template(path: impl AsRef<Path>) -> Result<()> {
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent)?;
        }
        crate::io_utils::atomic_write(path, Self::default_toml_template().as_bytes())
    }

    /// Returns the commented default TOML template.
    pub fn default_toml_template() -> &'static str {
        DEFAULT_TOML_TEMPLATE
    }

    fn load_local_or_user_default_with_user_path(
        local_path: &Path,
        user_path: Option<&Path>,
    ) -> Result<Self> {
        if local_path.exists() {
            return Self::load_from_path(local_path);
        }

        if let Some(user_path) = user_path {
            if user_path.exists() {
                return Self::load_from_path(user_path);
            }
        }

        Ok(Self::default())
    }
}

/// Returns the default user-level config path.
///
/// Linux/macOS: `~/rusdox/config.toml`
/// Windows: `%USERPROFILE%\rusdox\config.toml`
pub fn default_user_config_path() -> Option<PathBuf> {
    #[cfg(windows)]
    {
        std::env::var_os("USERPROFILE")
            .map(PathBuf::from)
            .map(|home| home.join("rusdox").join("config.toml"))
    }

    #[cfg(not(windows))]
    {
        std::env::var_os("HOME")
            .map(PathBuf::from)
            .map(|home| home.join("rusdox").join("config.toml"))
    }
}

/// Output path and preview behavior.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct OutputConfig {
    /// Target directory for generated `.docx` files.
    pub docx_dir: String,
    /// Target directory for generated `.pdf` previews.
    pub pdf_dir: String,
    /// Whether to emit PDF previews alongside DOCX output.
    pub emit_pdf_preview: bool,
}

impl Default for OutputConfig {
    fn default() -> Self {
        Self {
            docx_dir: "generated".to_string(),
            pdf_dir: "rendered".to_string(),
            emit_pdf_preview: true,
        }
    }
}

/// Typography defaults used by the style helpers.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct TypographyConfig {
    /// Default run font family used by helper methods.
    pub font_family: String,
    /// Cover title size in points.
    pub cover_title_size_pt: f32,
    /// Title size in points.
    pub title_size_pt: f32,
    /// Subtitle size in points.
    pub subtitle_size_pt: f32,
    /// Hero size in points.
    pub hero_size_pt: f32,
    /// Page heading size in points.
    pub page_heading_size_pt: f32,
    /// Section size in points.
    pub section_size_pt: f32,
    /// Body size in points.
    pub body_size_pt: f32,
    /// Tagline size in points.
    pub tagline_size_pt: f32,
    /// Note size in points.
    pub note_size_pt: f32,
    /// Table size in points.
    pub table_size_pt: f32,
    /// Metric label size in points.
    pub metric_label_size_pt: f32,
    /// Metric value size in points.
    pub metric_value_size_pt: f32,
}

impl Default for TypographyConfig {
    fn default() -> Self {
        Self {
            font_family: "Arial".to_string(),
            cover_title_size_pt: 30.0,
            title_size_pt: 26.0,
            subtitle_size_pt: 11.0,
            hero_size_pt: 14.0,
            page_heading_size_pt: 20.0,
            section_size_pt: 15.0,
            body_size_pt: 11.0,
            tagline_size_pt: 11.0,
            note_size_pt: 10.0,
            table_size_pt: 10.0,
            metric_label_size_pt: 10.0,
            metric_value_size_pt: 18.0,
        }
    }
}

/// Spacing defaults expressed in twips (1/20th point).
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct SpacingConfig {
    /// Cover title before, in twentieths of a point.
    pub cover_title_before_twips: u32,
    /// Cover title after, in twentieths of a point.
    pub cover_title_after_twips: u32,
    /// Title before, in twentieths of a point.
    pub title_before_twips: u32,
    /// Title after, in twentieths of a point.
    pub title_after_twips: u32,
    /// Subtitle after, in twentieths of a point.
    pub subtitle_after_twips: u32,
    /// Hero after, in twentieths of a point.
    pub hero_after_twips: u32,
    /// Page heading after, in twentieths of a point.
    pub page_heading_after_twips: u32,
    /// Section before, in twentieths of a point.
    pub section_before_twips: u32,
    /// Section after, in twentieths of a point.
    pub section_after_twips: u32,
    /// Body after, in twentieths of a point.
    pub body_after_twips: u32,
    /// Bullet after, in twentieths of a point.
    pub bullet_after_twips: u32,
    /// Label value after, in twentieths of a point.
    pub label_value_after_twips: u32,
    /// Tagline after, in twentieths of a point.
    pub tagline_after_twips: u32,
    /// Spacer after, in twentieths of a point.
    pub spacer_after_twips: u32,
    /// Note after, in twentieths of a point.
    pub note_after_twips: u32,
    /// Metric label before, in twentieths of a point.
    pub metric_label_before_twips: u32,
    /// Metric label after, in twentieths of a point.
    pub metric_label_after_twips: u32,
    /// Metric value after, in twentieths of a point.
    pub metric_value_after_twips: u32,
    /// Table header before, in twentieths of a point.
    pub table_header_before_twips: u32,
    /// Table header after, in twentieths of a point.
    pub table_header_after_twips: u32,
    /// Table data before, in twentieths of a point.
    pub table_data_before_twips: u32,
    /// Table data after, in twentieths of a point.
    pub table_data_after_twips: u32,
    /// Table status before, in twentieths of a point.
    pub table_status_before_twips: u32,
    /// Table status after, in twentieths of a point.
    pub table_status_after_twips: u32,
}

impl Default for SpacingConfig {
    fn default() -> Self {
        Self {
            cover_title_before_twips: 1_200,
            cover_title_after_twips: 180,
            title_before_twips: 240,
            title_after_twips: 120,
            subtitle_after_twips: 280,
            hero_after_twips: 240,
            page_heading_after_twips: 140,
            section_before_twips: 220,
            section_after_twips: 90,
            body_after_twips: 100,
            bullet_after_twips: 80,
            label_value_after_twips: 70,
            tagline_after_twips: 80,
            spacer_after_twips: 120,
            note_after_twips: 120,
            metric_label_before_twips: 60,
            metric_label_after_twips: 20,
            metric_value_after_twips: 80,
            table_header_before_twips: 50,
            table_header_after_twips: 50,
            table_data_before_twips: 40,
            table_data_after_twips: 40,
            table_status_before_twips: 40,
            table_status_after_twips: 40,
        }
    }
}

/// Palette used by helper functions and presets.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct ColorConfig {
    /// Ink.
    pub ink: String,
    /// Slate.
    pub slate: String,
    /// Muted.
    pub muted: String,
    /// Accent.
    pub accent: String,
    /// Gold.
    pub gold: String,
    /// Red.
    pub red: String,
    /// Green.
    pub green: String,
    /// Soft.
    pub soft: String,
    /// Pale.
    pub pale: String,
    /// Mint.
    pub mint: String,
    /// Amber.
    pub amber: String,
    /// Rose.
    pub rose: String,
    /// Table border.
    pub table_border: String,
}

impl Default for ColorConfig {
    fn default() -> Self {
        Self {
            ink: "0F172A".to_string(),
            slate: "475569".to_string(),
            muted: "64748B".to_string(),
            accent: "0F766E".to_string(),
            gold: "B45309".to_string(),
            red: "B91C1C".to_string(),
            green: "166534".to_string(),
            soft: "E2E8F0".to_string(),
            pale: "F8FAFC".to_string(),
            mint: "DCFCE7".to_string(),
            amber: "FEF3C7".to_string(),
            rose: "FEE2E2".to_string(),
            table_border: "CBD5E1".to_string(),
        }
    }
}

/// Table defaults used by helper APIs and PDF preview rendering.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct TableConfig {
    /// Default width, in twentieths of a point.
    pub default_width_twips: u32,
    /// Metric cell width, in twentieths of a point.
    pub metric_cell_width_twips: u32,
    /// Grid border size eighth points.
    pub grid_border_size_eighth_pt: u32,
    /// Card border size eighth points.
    pub card_border_size_eighth_pt: u32,
    /// PDF cell padding x points.
    pub pdf_cell_padding_x_pt: f32,
    /// PDF cell padding y points.
    pub pdf_cell_padding_y_pt: f32,
    /// PDF after spacing points.
    pub pdf_after_spacing_pt: f32,
    /// PDF grid stroke width points.
    pub pdf_grid_stroke_width_pt: f32,
}

impl Default for TableConfig {
    fn default() -> Self {
        Self {
            default_width_twips: 9_360,
            metric_cell_width_twips: 3_120,
            grid_border_size_eighth_pt: 8,
            card_border_size_eighth_pt: 10,
            pdf_cell_padding_x_pt: 7.0,
            pdf_cell_padding_y_pt: 6.0,
            pdf_after_spacing_pt: 12.0,
            pdf_grid_stroke_width_pt: 0.75,
        }
    }
}

/// PDF preview renderer settings.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(default)]
pub struct PdfConfig {
    /// Page width points.
    pub page_width_pt: f32,
    /// Page height points.
    pub page_height_pt: f32,
    /// Margin x points.
    pub margin_x_pt: f32,
    /// Margin top points.
    pub margin_top_pt: f32,
    /// Margin bottom points.
    pub margin_bottom_pt: f32,
    /// Default text size in points.
    pub default_text_size_pt: f32,
    /// Default line height points.
    pub default_line_height_pt: f32,
    /// Line height multiplier.
    pub line_height_multiplier: f32,
    /// Baseline factor.
    pub baseline_factor: f32,
    /// Text width bias regular.
    pub text_width_bias_regular: f32,
    /// Text width bias bold.
    pub text_width_bias_bold: f32,
}

impl Default for PdfConfig {
    fn default() -> Self {
        Self {
            page_width_pt: 612.0,
            page_height_pt: 792.0,
            margin_x_pt: 54.0,
            margin_top_pt: 54.0,
            margin_bottom_pt: 54.0,
            default_text_size_pt: 11.0,
            default_line_height_pt: 14.0,
            line_height_multiplier: 1.35,
            baseline_factor: 0.82,
            text_width_bias_regular: 1.0,
            text_width_bias_bold: 1.03,
        }
    }
}

const DEFAULT_TOML_TEMPLATE: &str = r#"# RusDox configuration template
# Most users should use the CLI wizard instead of editing this by hand first:
#   rusdox config wizard --level basic
#   rusdox config wizard --level advanced
# Create or edit a project-local override in the current folder:
#   rusdox config wizard --path ./rusdox.toml --level basic
# Print the user-level config path:
#   rusdox config path
# Config load order while rendering documents:
#   1. ./rusdox.toml
#   2. ~/rusdox/config.toml
#   3. built-in defaults
# Every field below is optional: missing values fall back to defaults.

profile_name = "RusDox Default"

[output]
# Directory for generated DOCX files
docx_dir = "generated"
# Directory for generated PDF preview files
pdf_dir = "rendered"
# Set false if you only want DOCX output
emit_pdf_preview = true

[typography]
# Font family used by helper builders
font_family = "Arial"
cover_title_size_pt = 30.0
title_size_pt = 26.0
subtitle_size_pt = 11.0
hero_size_pt = 14.0
page_heading_size_pt = 20.0
section_size_pt = 15.0
body_size_pt = 11.0
tagline_size_pt = 11.0
note_size_pt = 10.0
table_size_pt = 10.0
metric_label_size_pt = 10.0
metric_value_size_pt = 18.0

[spacing]
# DOCX spacing values in twips (1/20 point)
cover_title_before_twips = 1200
cover_title_after_twips = 180
title_before_twips = 240
title_after_twips = 120
subtitle_after_twips = 280
hero_after_twips = 240
page_heading_after_twips = 140
section_before_twips = 220
section_after_twips = 90
body_after_twips = 100
bullet_after_twips = 80
label_value_after_twips = 70
tagline_after_twips = 80
spacer_after_twips = 120
note_after_twips = 120
metric_label_before_twips = 60
metric_label_after_twips = 20
metric_value_after_twips = 80
table_header_before_twips = 50
table_header_after_twips = 50
table_data_before_twips = 40
table_data_after_twips = 40
table_status_before_twips = 40
table_status_after_twips = 40

[colors]
# Six-digit hex values without '#'
ink = "0F172A"
slate = "475569"
muted = "64748B"
accent = "0F766E"
gold = "B45309"
red = "B91C1C"
green = "166534"
soft = "E2E8F0"
pale = "F8FAFC"
mint = "DCFCE7"
amber = "FEF3C7"
rose = "FEE2E2"
table_border = "CBD5E1"

[table]
default_width_twips = 9360
metric_cell_width_twips = 3120
grid_border_size_eighth_pt = 8
card_border_size_eighth_pt = 10
pdf_cell_padding_x_pt = 7.0
pdf_cell_padding_y_pt = 6.0
pdf_after_spacing_pt = 12.0
pdf_grid_stroke_width_pt = 0.75

[pdf]
# Letter page size by default
page_width_pt = 612.0
page_height_pt = 792.0
margin_x_pt = 54.0
margin_top_pt = 54.0
margin_bottom_pt = 54.0
default_text_size_pt = 11.0
default_line_height_pt = 14.0
line_height_multiplier = 1.35
baseline_factor = 0.82
text_width_bias_regular = 1.0
text_width_bias_bold = 1.03
"#;

#[cfg(test)]
mod tests {
    use std::fs;

    use tempfile::tempdir;

    use super::{
        default_user_config_path, ColorConfig, OutputConfig, RusdoxConfig, SpacingConfig,
        TypographyConfig,
    };
    use crate::DocxError;

    #[test]
    fn default_values_are_stable_and_complete() {
        let config = RusdoxConfig::default();
        assert_eq!(config.profile_name, "RusDox Default");
        assert_eq!(config.output.docx_dir, "generated");
        assert_eq!(config.output.pdf_dir, "rendered");
        assert!(config.output.emit_pdf_preview);
        assert_eq!(config.typography.font_family, "Arial");
        assert_eq!(config.typography.cover_title_size_pt, 30.0);
        assert_eq!(config.colors.accent, "0F766E");
        assert_eq!(config.spacing.page_heading_after_twips, 140);
        assert_eq!(config.table.default_width_twips, 9_360);
        assert_eq!(config.pdf.page_width_pt, 612.0);
    }

    #[test]
    fn from_toml_supports_partial_config_with_defaults() {
        let config = RusdoxConfig::from_toml_str(
            r#"
profile_name = "Custom"

[output]
docx_dir = "out/docx"
"#,
        )
        .expect("toml should parse");

        assert_eq!(config.profile_name, "Custom");
        assert_eq!(config.output.docx_dir, "out/docx");
        assert_eq!(config.output.pdf_dir, "rendered");
        assert!(config.output.emit_pdf_preview);
    }

    #[test]
    fn json_round_trip_preserves_key_fields() {
        let config = RusdoxConfig {
            profile_name: "JSON Profile".to_string(),
            output: OutputConfig {
                emit_pdf_preview: false,
                ..OutputConfig::default()
            },
            colors: ColorConfig {
                ink: "ABCDEF".to_string(),
                ..ColorConfig::default()
            },
            ..RusdoxConfig::default()
        };

        let json = config.to_json_pretty().expect("json serialize");
        let parsed = RusdoxConfig::from_json_str(&json).expect("json parse");
        assert_eq!(parsed, config);
    }

    #[test]
    fn toml_round_trip_preserves_key_fields() {
        let config = RusdoxConfig {
            profile_name: "TOML Profile".to_string(),
            typography: TypographyConfig {
                body_size_pt: 12.5,
                ..TypographyConfig::default()
            },
            spacing: SpacingConfig {
                section_after_twips: 333,
                ..SpacingConfig::default()
            },
            ..RusdoxConfig::default()
        };

        let toml = config.to_toml_pretty().expect("toml serialize");
        let parsed = RusdoxConfig::from_toml_str(&toml).expect("toml parse");
        assert_eq!(parsed, config);
    }

    #[test]
    fn load_from_path_uses_extension_based_parser() {
        let temp = tempdir().expect("temp dir");
        let toml_path = temp.path().join("config.toml");
        let json_path = temp.path().join("config.json");

        fs::write(
            &toml_path,
            r#"
profile_name = "Toml Path"
[output]
emit_pdf_preview = false
"#,
        )
        .expect("write toml");
        fs::write(
            &json_path,
            r#"{"profile_name":"Json Path","output":{"emit_pdf_preview":false}}"#,
        )
        .expect("write json");

        let toml_config = RusdoxConfig::load_from_path(&toml_path).expect("load toml");
        let json_config = RusdoxConfig::load_from_path(&json_path).expect("load json");

        assert_eq!(toml_config.profile_name, "Toml Path");
        assert!(!toml_config.output.emit_pdf_preview);
        assert_eq!(json_config.profile_name, "Json Path");
        assert!(!json_config.output.emit_pdf_preview);
    }

    #[test]
    fn load_from_path_without_extension_defaults_to_toml() {
        let temp = tempdir().expect("temp dir");
        let path = temp.path().join("rusdox_config");
        fs::write(
            &path,
            r#"
profile_name = "NoExt"
[output]
docx_dir = "x"
"#,
        )
        .expect("write file");

        let config = RusdoxConfig::load_from_path(&path).expect("load no-extension file");
        assert_eq!(config.profile_name, "NoExt");
        assert_eq!(config.output.docx_dir, "x");
    }

    #[test]
    fn unsupported_extension_errors_on_load_and_save() {
        let temp = tempdir().expect("temp dir");
        let load_path = temp.path().join("config.yaml");
        fs::write(&load_path, "profile_name: x").expect("write yaml");

        let load_error = RusdoxConfig::load_from_path(&load_path).expect_err("must fail");
        assert!(
            matches!(load_error, DocxError::Parse(message) if message.contains("unsupported config extension"))
        );

        let save_path = temp.path().join("config.ini");
        let save_error = RusdoxConfig::default()
            .save_to_path(&save_path)
            .expect_err("must fail");
        assert!(
            matches!(save_error, DocxError::Parse(message) if message.contains("unsupported config extension"))
        );
    }

    #[test]
    fn load_from_path_or_default_returns_default_when_missing() {
        let temp = tempdir().expect("temp dir");
        let missing = temp.path().join("missing.toml");
        let config = RusdoxConfig::load_from_path_or_default(missing).expect("default config");
        assert_eq!(config, RusdoxConfig::default());
    }

    #[test]
    fn load_local_or_user_default_prefers_local_then_user_then_default() {
        let temp = tempdir().expect("temp dir");
        let local_path = temp.path().join("local.toml");
        let user_path = temp.path().join("user.toml");

        fs::write(
            &user_path,
            r#"
profile_name = "User"
"#,
        )
        .expect("write user config");

        let from_user =
            RusdoxConfig::load_local_or_user_default_with_user_path(&local_path, Some(&user_path))
                .expect("load user fallback");
        assert_eq!(from_user.profile_name, "User");

        fs::write(
            &local_path,
            r#"
profile_name = "Local"
"#,
        )
        .expect("write local config");

        let from_local =
            RusdoxConfig::load_local_or_user_default_with_user_path(&local_path, Some(&user_path))
                .expect("load local config");
        assert_eq!(from_local.profile_name, "Local");

        fs::remove_file(&local_path).expect("remove local config");
        fs::remove_file(&user_path).expect("remove user config");

        let defaulted =
            RusdoxConfig::load_local_or_user_default_with_user_path(&local_path, Some(&user_path))
                .expect("load default config");
        assert_eq!(defaulted, RusdoxConfig::default());
    }

    #[test]
    fn save_to_path_creates_parent_directories_and_writes_toml_json() {
        let temp = tempdir().expect("temp dir");
        let nested_toml = temp.path().join("a/b/c/config.toml");
        let nested_json = temp.path().join("a/b/c/config.json");

        let config = RusdoxConfig {
            profile_name: "Persisted".to_string(),
            ..RusdoxConfig::default()
        };
        config.save_to_path(&nested_toml).expect("save toml");
        config.save_to_path(&nested_json).expect("save json");

        let parsed_toml = RusdoxConfig::load_from_path(&nested_toml).expect("load toml");
        let parsed_json = RusdoxConfig::load_from_path(&nested_json).expect("load json");

        assert_eq!(parsed_toml.profile_name, "Persisted");
        assert_eq!(parsed_json.profile_name, "Persisted");
    }

    #[test]
    fn write_toml_template_writes_expected_content() {
        let temp = tempdir().expect("temp dir");
        let path = temp.path().join("template/rusdox.toml");
        RusdoxConfig::write_toml_template(&path).expect("write template");

        let content = fs::read_to_string(&path).expect("read template");
        assert!(content.contains("# RusDox configuration template"));
        assert!(content.contains("rusdox config wizard --level basic"));
        assert!(content.contains("rusdox config wizard --path ./rusdox.toml --level basic"));
        assert!(content.contains("Config load order while rendering documents"));
        assert!(content.contains("[output]"));
        assert!(content.contains("[pdf]"));
        assert_eq!(content, RusdoxConfig::default_toml_template());
    }

    #[test]
    fn default_user_config_path_shape_is_consistent() {
        if let Some(path) = default_user_config_path() {
            assert!(path.ends_with(std::path::Path::new("rusdox").join("config.toml")));
        }
    }

    #[test]
    fn invalid_toml_and_json_return_parse_errors() {
        let toml_error = RusdoxConfig::from_toml_str("not = [valid").expect_err("invalid toml");
        let json_error = RusdoxConfig::from_json_str("{invalid json").expect_err("invalid json");

        assert!(
            matches!(toml_error, DocxError::Parse(message) if message.contains("invalid TOML"))
        );
        assert!(
            matches!(json_error, DocxError::Parse(message) if message.contains("invalid JSON"))
        );
    }
}