cargo-rdme 2.2.0

Cargo command to create your `README.md` from your crate's documentation
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
use cargo_rdme::find_first_file_in_ancestors;
use cargo_rdme::transform::{IntralinksConfig, IntralinksDocsConfig};
use clap::{ArgAction, value_parser};
use std::error::Error;
use std::ffi::OsString;
use std::fmt::{Display, Formatter};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use thiserror::Error;

const PROJECT_NAME: &str = env!("CARGO_PKG_NAME");
const VERSION: &str = env!("CARGO_PKG_VERSION");

#[derive(Debug)]
pub struct InvalidOptValue {
    value: String,
}

impl Display for InvalidOptValue {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.write_fmt(format_args!("invalid value \"{}\"", self.value))
    }
}

impl Error for InvalidOptValue {}

#[derive(PartialEq, Eq, Debug, Default, Clone, Copy)]
pub enum LineTerminatorOpt {
    #[default]
    Auto,
    Lf,
    CrLf,
}

impl FromStr for LineTerminatorOpt {
    type Err = InvalidOptValue;

    fn from_str(s: &str) -> Result<LineTerminatorOpt, InvalidOptValue> {
        match s {
            "auto" => Ok(LineTerminatorOpt::Auto),
            "lf" => Ok(LineTerminatorOpt::Lf),
            "crlf" => Ok(LineTerminatorOpt::CrLf),
            v => Err(InvalidOptValue { value: v.to_owned() }),
        }
    }
}

#[derive(PartialEq, Eq, Debug, Default, Clone)]
pub enum EntrypointOpt {
    #[default]
    Auto,
    Lib,
    BinDefault,
    BinName(String),
}

impl FromStr for EntrypointOpt {
    type Err = InvalidOptValue;

    fn from_str(s: &str) -> Result<EntrypointOpt, InvalidOptValue> {
        match s {
            "auto" => Ok(EntrypointOpt::Auto),
            "lib" => Ok(EntrypointOpt::Lib),
            "bin" => Ok(EntrypointOpt::BinDefault),
            v if v.starts_with("bin:") && v.len() > "bin:".len() => {
                let name = v["bin:".len()..].to_owned();
                Ok(EntrypointOpt::BinName(name))
            }
            v => Err(InvalidOptValue { value: v.to_owned() }),
        }
    }
}

#[derive(Debug)]
pub struct CmdOptions {
    workspace_project: Option<String>,
    entrypoint: Option<EntrypointOpt>,
    line_terminator: Option<LineTerminatorOpt>,
    check: bool,
    no_fail_on_warnings: bool,
    intralinks_strip_links: bool,
    force: bool,
    readme_path: Option<PathBuf>,
    manifest_path: Option<PathBuf>,
    intralinks_all_features: bool,
    intralinks_features: Option<Vec<String>>,
    intralinks_no_default_features: bool,
    heading_base_level: Option<u8>,
}

impl CmdOptions {
    pub fn manifest_path(&self) -> Option<&Path> {
        self.manifest_path.as_deref()
    }
}

fn get_cmd_args() -> Vec<OsString> {
    let mut args: Vec<OsString> = std::env::args_os().collect();
    let subcommand: &str = {
        let package_name = env!("CARGO_PKG_NAME");

        assert!(package_name.starts_with("cargo-"), "package name does not start with `cargo-`");

        &package_name["cargo-".len()..]
    };

    // When cargo executes an external subcommand it passes the name of the command itself as the
    // second argument.  Here we remove that so that we can simply run `cargo run` instead of
    // `cargo run -- rdme` for local development.
    if args.len() >= 2 && args[1] == subcommand {
        args.remove(1);
    }

    args
}

#[derive(Debug)]
pub enum Command {
    Run(CmdOptions),
    InstallRustToolchainForIntralinks,
}

#[allow(clippy::too_many_lines)]
pub fn command() -> Command {
    use clap::{Arg, Command as ClapCommand};

    let cmd_opts = ClapCommand::new(PROJECT_NAME)
        .version(VERSION)
        .about("Create the README from your crate’s documentation.")
        .styles(clap_cargo::style::CLAP_STYLING)
        .subcommand(
            ClapCommand::new("install-rust-toolchain-for-intralinks")
                .about(
                    "Install the nightly rust toolchain needed for intralink resolution.\n\
                     \n\
                     If the toolchain is already installed this is command does nothing.",
                )
                .hide(true),
        )
        .arg(
            Arg::new("entrypoint")
                .long("entrypoint")
                .help("selects the source code entrypoint of the crate (e.g. auto, lib, bin, bin:<name>)")
                .value_parser(EntrypointOpt::from_str),
        )
        .arg(
            Arg::new("line-terminator")
                .long("line-terminator")
                .help("line terminator to use when writing the README file")
                .value_parser(LineTerminatorOpt::from_str),
        )
        .arg(
            Arg::new("readme-path")
                .long("readme-path")
                .short('r')
                .help("README file path to use (overrides what is specified in the project `Cargo.toml`)")
                .value_parser(value_parser!(PathBuf)),
        )
        .arg(
            Arg::new("manifest-path")
                .long("manifest-path")
                .help("path to `Cargo.toml`")
                .value_parser(value_parser!(PathBuf)),
        )
        .arg(
            Arg::new("workspace-project")
                .long("workspace-project")
                .short('w')
                .help("project to get the documentation from if your are using workspaces"),
        )
        .arg(
            Arg::new("check")
                .long("check")
                .short('c')
                .help("checks if the README is up to date (exit code 3 if there’s a mismatch, 4 if warnings were emitted)")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("no-fail-on-warnings")
                .long("no-fail-on-warnings")
                .help("do not exit with a error status code when checking if the README is up to date")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("intralinks-strip-links")
                .long("intralinks-strip-links")
                .help("remove the intralinks")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("intralinks-all-features")
                .long("intralinks-all-features")
                .help("enable all features when calling rustdoc to resolve intralinks")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("intralinks-features")
                .long("intralinks-features")
                .help("features to enable when calling rustdoc to resolve intralinks (comma separated list)")
                .value_delimiter(',')
                .action(ArgAction::Append),
        )
        .arg(
            Arg::new("intralinks-no-default-features")
                .long("intralinks-no-default-features")
                .help("disable default features when calling rustdoc to resolve intralinks")
                .action(ArgAction::SetTrue),
        )
        .arg(
            Arg::new("heading-base-level")
                .long("heading-base-level")
                .help("heading level to be added to the heading level in the rust documentation")
                .value_parser(value_parser!(u8)),
        )
        .arg(
            Arg::new("force")
                .long("force")
                .short('f')
                .help("force README update, even when there are uncommitted changes")
                .action(ArgAction::SetTrue),
        )
        .get_matches_from(get_cmd_args());

    if let Some(("install-rust-toolchain-for-intralinks", _)) = cmd_opts.subcommand() {
        return Command::InstallRustToolchainForIntralinks;
    }

    let workspace_project = cmd_opts.get_one::<String>("workspace-project").cloned();

    let line_terminator: Option<LineTerminatorOpt> =
        cmd_opts.get_one::<LineTerminatorOpt>("line-terminator").copied();

    let entrypoint = cmd_opts.get_one::<EntrypointOpt>("entrypoint").cloned();

    let readme_path = cmd_opts.get_one::<PathBuf>("readme-path").cloned();
    let manifest_path = cmd_opts.get_one::<PathBuf>("manifest-path").cloned();

    let intralinks_features =
        cmd_opts.get_many::<String>("intralinks-features").map(|v| v.cloned().collect());

    let heading_base_level = cmd_opts.get_one::<u8>("heading-base-level").copied();

    Command::Run(CmdOptions {
        workspace_project,
        entrypoint,
        line_terminator,
        check: cmd_opts.get_flag("check"),
        no_fail_on_warnings: cmd_opts.get_flag("no-fail-on-warnings"),
        intralinks_strip_links: cmd_opts.get_flag("intralinks-strip-links"),
        force: cmd_opts.get_flag("force"),
        readme_path,
        manifest_path,
        intralinks_all_features: cmd_opts.get_flag("intralinks-all-features"),
        intralinks_features,
        intralinks_no_default_features: cmd_opts.get_flag("intralinks-no-default-features"),
        heading_base_level,
    })
}

#[derive(Error, Debug)]
pub enum ConfigFileOptionsError {
    #[error("failed to read configuration file: {0}")]
    ErrorReadingConfigFile(PathBuf),
    #[error("failed to parse toml: {0}")]
    ErrorParsingToml(toml::de::Error),
    #[error("invalid field \"{0}\"")]
    InvalidField(&'static str),
    #[error("invalid entrypoint table")]
    InvalidEntrypointTable,
    #[error("`{new}` and `{old}` cannot be set at the same time")]
    ConflictingDocsOptions { new: &'static str, old: &'static str },
    #[error("`intralinks.docs.base-url` is required when `intralinks.docs.layout = \"flat\"`")]
    FlatLayoutRequiresBaseUrl,
    #[error("`intralinks.docs.version` cannot be set when `intralinks.docs.layout = \"flat\"`")]
    FlatLayoutRejectsVersion,
}

#[derive(Debug, PartialEq, Eq)]
pub struct ConfigFileOptions {
    line_terminator: Option<LineTerminatorOpt>,
    workspace_project: Option<String>,
    entrypoint: Option<EntrypointOpt>,
    readme_path: Option<PathBuf>,
    intralinks: Option<IntralinksConfig>,
    heading_base_level: Option<u8>,
}

#[allow(clippy::too_many_lines)]
fn config_file_options_from_str(
    config_str: &str,
    mut emit_deprecation_warning: impl FnMut(&str),
) -> Result<ConfigFileOptions, ConfigFileOptionsError> {
    let config_toml: toml::Value =
        toml::from_str(config_str).map_err(ConfigFileOptionsError::ErrorParsingToml)?;

    let workspace_project =
        config_toml.get("workspace-project").and_then(toml::Value::as_str).map(ToOwned::to_owned);

    let line_terminator = config_toml
        .get("line-terminator")
        .map(|v| {
            v.as_str().ok_or(ConfigFileOptionsError::InvalidField("line-terminator")).and_then(
                |str| {
                    LineTerminatorOpt::from_str(str)
                        .map_err(|_| ConfigFileOptionsError::InvalidField("line-terminator"))
                },
            )
        })
        .transpose()?;

    let entrypoint_table = config_toml.get("entrypoint").and_then(toml::Value::as_table);

    let entrypoint_type =
        entrypoint_table.and_then(|t| t.get("type")).and_then(toml::Value::as_str);
    let entrypoint_bin_name =
        entrypoint_table.and_then(|t| t.get("bin-name")).and_then(toml::Value::as_str);

    let entrypoint = match (entrypoint_type, entrypoint_bin_name) {
        (Some("lib"), None) => Some(EntrypointOpt::Lib),
        (Some("bin"), None) => Some(EntrypointOpt::BinDefault),
        (Some("bin"), Some(name)) => Some(EntrypointOpt::BinName(name.to_owned())),
        (None, None) => None,
        _ => return Err(ConfigFileOptionsError::InvalidEntrypointTable),
    };

    let readme_path =
        config_toml.get("readme-path").and_then(toml::Value::as_str).map(PathBuf::from);

    let heading_base_level: Option<u8> = match config_toml
        .get("heading-base-level")
        .and_then(toml::Value::as_integer)
        .map(TryInto::try_into)
    {
        None => None,
        Some(Ok(l)) => Some(l),
        Some(Err(_)) => return Err(ConfigFileOptionsError::InvalidField("heading-base-level")),
    };

    let intralinks_table = config_toml.get("intralinks").and_then(toml::Value::as_table);

    // TODO Remove `docs-rs-base-url` and `docs-rs-version` in 3.0.0. Superseded by the
    //      `[intralinks.docs]` table (with `layout`, `base-url`, `version`).
    let intralinks_docs_rs_base_url_deprecated =
        intralinks_table.and_then(|t| t.get("docs-rs-base-url")).and_then(toml::Value::as_str);
    let intralinks_docs_rs_version_deprecated =
        intralinks_table.and_then(|t| t.get("docs-rs-version")).and_then(toml::Value::as_str);

    let intralinks_docs_table =
        intralinks_table.and_then(|t| t.get("docs")).and_then(toml::Value::as_table);
    let intralinks_docs_layout =
        intralinks_docs_table.and_then(|t| t.get("layout")).and_then(toml::Value::as_str);
    let intralinks_docs_base_url =
        intralinks_docs_table.and_then(|t| t.get("base-url")).and_then(toml::Value::as_str);
    let intralinks_docs_version =
        intralinks_docs_table.and_then(|t| t.get("version")).and_then(toml::Value::as_str);

    if intralinks_docs_base_url.is_some() && intralinks_docs_rs_base_url_deprecated.is_some() {
        return Err(ConfigFileOptionsError::ConflictingDocsOptions {
            new: "intralinks.docs.base-url",
            old: "intralinks.docs-rs-base-url",
        });
    }

    if intralinks_docs_version.is_some() && intralinks_docs_rs_version_deprecated.is_some() {
        return Err(ConfigFileOptionsError::ConflictingDocsOptions {
            new: "intralinks.docs.version",
            old: "intralinks.docs-rs-version",
        });
    }

    let base_url = intralinks_docs_base_url.or(intralinks_docs_rs_base_url_deprecated);
    let version = intralinks_docs_version.or(intralinks_docs_rs_version_deprecated);

    let docs = match intralinks_docs_layout {
        Some("flat") => {
            if version.is_some() {
                return Err(ConfigFileOptionsError::FlatLayoutRejectsVersion);
            }
            let base_url =
                base_url.ok_or(ConfigFileOptionsError::FlatLayoutRequiresBaseUrl)?.to_owned();

            IntralinksDocsConfig::Flat { base_url }
        }
        Some("docs-rs") | None => IntralinksDocsConfig::DocsRs {
            base_url: base_url.map(ToOwned::to_owned),
            version: version.map(ToOwned::to_owned),
        },
        Some(_) => return Err(ConfigFileOptionsError::InvalidField("intralinks.docs.layout")),
    };

    if intralinks_docs_rs_base_url_deprecated.is_some() {
        emit_deprecation_warning(
            "`[intralinks].docs-rs-base-url` is deprecated: use `[intralinks.docs].base-url` instead.",
        );
    }

    if intralinks_docs_rs_version_deprecated.is_some() {
        emit_deprecation_warning(
            "`[intralinks].docs-rs-version` is deprecated: use `[intralinks.docs].version` instead.",
        );
    }

    let intralinks_strip_links =
        intralinks_table.and_then(|t| t.get("strip-links")).and_then(toml::Value::as_bool);
    let intralinks_all_features =
        intralinks_table.and_then(|t| t.get("all-features")).and_then(toml::Value::as_bool);
    let intralinks_features = intralinks_table
        .and_then(|t| t.get("features"))
        .and_then(toml::Value::as_array)
        .map(|array| array.iter().filter_map(|v| v.as_str()).map(ToOwned::to_owned).collect());
    let intralinks_no_default_features =
        intralinks_table.and_then(|t| t.get("no-default-features")).and_then(toml::Value::as_bool);
    let rustdoc_toolchain =
        intralinks_table.and_then(|t| t.get("rustdoc-toolchain")).and_then(toml::Value::as_str);

    let intralinks = intralinks_table.map(|_| IntralinksConfig {
        docs,
        strip_links: intralinks_strip_links,
        all_features: intralinks_all_features,
        features: intralinks_features,
        no_default_features: intralinks_no_default_features,
        rustdoc_toolchain: rustdoc_toolchain.map(ToOwned::to_owned),
    });

    Ok(ConfigFileOptions {
        line_terminator,
        workspace_project,
        entrypoint,
        readme_path,
        intralinks,
        heading_base_level,
    })
}

pub fn config_file_options(
    current_dir: impl AsRef<Path>,
    emit_deprecation_warning: impl FnMut(&str),
) -> Result<Option<ConfigFileOptions>, ConfigFileOptionsError> {
    find_first_file_in_ancestors(current_dir, ".cargo-rdme.toml")
        .map(|file_path| {
            let config_str = std::fs::read_to_string(&file_path)
                .map_err(|_| ConfigFileOptionsError::ErrorReadingConfigFile(file_path))?;

            config_file_options_from_str(&config_str, emit_deprecation_warning)
        })
        .transpose()
}

#[derive(PartialEq, Eq, Debug)]
pub struct Options {
    pub workspace_project: Option<String>,
    pub entrypoint: EntrypointOpt,
    pub line_terminator: LineTerminatorOpt,
    pub check: bool,
    pub no_fail_on_warnings: bool,
    pub force: bool,
    pub readme_path: Option<PathBuf>,
    pub manifest_path: Option<PathBuf>,
    pub intralinks: Option<IntralinksConfig>,
    pub heading_base_level: Option<u8>,
}

#[allow(clippy::needless_pass_by_value)]
pub fn merge_options(
    cmd_options: CmdOptions,
    config_file_options: Option<ConfigFileOptions>,
) -> Options {
    let mut config_file_options = config_file_options;

    Options {
        workspace_project: cmd_options
            .workspace_project
            .or_else(|| config_file_options.as_mut().and_then(|c| c.workspace_project.take())),
        entrypoint: cmd_options
            .entrypoint
            .or_else(|| config_file_options.as_mut().and_then(|c| c.entrypoint.take()))
            .unwrap_or_default(),
        line_terminator: cmd_options
            .line_terminator
            .or_else(|| config_file_options.as_ref().and_then(|c| c.line_terminator))
            .unwrap_or_default(),
        check: cmd_options.check,
        no_fail_on_warnings: cmd_options.no_fail_on_warnings,
        force: cmd_options.force,
        readme_path: cmd_options
            .readme_path
            .or_else(|| config_file_options.as_mut().and_then(|c| c.readme_path.take())),
        manifest_path: cmd_options.manifest_path,
        intralinks: Some(IntralinksConfig {
            docs: config_file_options
                .as_mut()
                .and_then(|c| c.intralinks.as_mut())
                .map(|il| std::mem::take(&mut il.docs))
                .unwrap_or_default(),
            strip_links: match cmd_options.intralinks_strip_links {
                true => Some(true),
                false => config_file_options
                    .as_ref()
                    .and_then(|c| c.intralinks.as_ref())
                    .and_then(|il| il.strip_links),
            },
            all_features: match cmd_options.intralinks_all_features {
                true => Some(true),
                false => config_file_options
                    .as_ref()
                    .and_then(|c| c.intralinks.as_ref())
                    .and_then(|il| il.all_features),
            },
            features: match cmd_options.intralinks_features {
                Some(features) => Some(features),
                None => config_file_options
                    .as_mut()
                    .and_then(|c| c.intralinks.as_mut())
                    .and_then(|il| il.features.take()),
            },
            no_default_features: match cmd_options.intralinks_no_default_features {
                true => Some(true),
                false => config_file_options
                    .as_ref()
                    .and_then(|c| c.intralinks.as_ref())
                    .and_then(|il| il.no_default_features),
            },
            rustdoc_toolchain: config_file_options
                .as_ref()
                .and_then(|c| c.intralinks.as_ref().and_then(|il| il.rustdoc_toolchain.clone())),
        }),
        heading_base_level: cmd_options
            .heading_base_level
            .or_else(|| config_file_options.and_then(|c| c.heading_base_level)),
    }
}

pub fn apply_envvar_overrides(options: &mut Options) {
    if let Ok(override_value) = std::env::var("CARGO_RDME_RUSTDOC_TOOLCHAIN") {
        options.intralinks.get_or_insert_default().rustdoc_toolchain = Some(override_value);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use cargo_rdme::transform::IntralinksDocsConfig;
    use indoc::indoc;
    use pretty_assertions::assert_eq;

    fn parse(config_str: &str) -> Result<ConfigFileOptions, ConfigFileOptionsError> {
        config_file_options_from_str(config_str, |_| {})
    }

    fn parse_collect_warnings(
        config_str: &str,
    ) -> Result<(ConfigFileOptions, Vec<String>), ConfigFileOptionsError> {
        let mut warnings: Vec<String> = Vec::new();
        let options =
            config_file_options_from_str(config_str, |msg| warnings.push(msg.to_owned()))?;

        Ok((options, warnings))
    }

    #[test]
    fn test_config_file_options_from_str() {
        let str = indoc! { r#"
            readme-path = "ReAdMe.md"
            workspace-project = "myproj"
            line-terminator = "crlf"
            heading-base-level = 3

            [entrypoint]
            type = "bin"
            bin-name = "baz"

            [intralinks]
            strip-links = true
            rustdoc-toolchain = "nightly-2026-08-03"

            all-features = true
            features = ["foo", "bar"]
            no-default-features = true

            [intralinks.docs]
            base-url = "https://internaldocs.rs"
            version = "1.0.0"
            "#
        };

        let config_file_opts = parse(str).unwrap();

        let expected = ConfigFileOptions {
            workspace_project: Some("myproj".to_owned()),
            entrypoint: Some(EntrypointOpt::BinName("baz".to_owned())),
            line_terminator: Some(LineTerminatorOpt::CrLf),
            readme_path: Some(PathBuf::from("ReAdMe.md")),
            intralinks: Some(IntralinksConfig {
                docs: IntralinksDocsConfig::DocsRs {
                    base_url: Some("https://internaldocs.rs".to_owned()),
                    version: Some("1.0.0".to_owned()),
                },
                strip_links: Some(true),
                all_features: Some(true),
                features: Some(vec!["foo".to_owned(), "bar".to_owned()]),
                no_default_features: Some(true),
                rustdoc_toolchain: Some("nightly-2026-08-03".to_owned()),
            }),
            heading_base_level: Some(3),
        };

        assert_eq!(config_file_opts, expected);
    }

    #[test]
    fn test_config_file_options_from_str_flat_layout() {
        let str = indoc! { r#"
            [intralinks.docs]
            layout = "flat"
            base-url = "https://rust.docs.kernel.org/next"
            "#
        };

        let config_file_opts = parse(str).unwrap();

        let expected_intralinks = IntralinksConfig {
            docs: IntralinksDocsConfig::Flat {
                base_url: "https://rust.docs.kernel.org/next".to_owned(),
            },
            strip_links: None,
            all_features: None,
            features: None,
            no_default_features: None,
            rustdoc_toolchain: None,
        };

        assert_eq!(config_file_opts.intralinks, Some(expected_intralinks));
    }

    #[test]
    fn test_config_file_options_from_str_deprecated_keys_emit_warnings() {
        let str = indoc! { r#"
            [intralinks]
            docs-rs-base-url = "https://internaldocs.rs"
            docs-rs-version = "1.0.0"
            "#
        };

        let (config_file_opts, warnings) = parse_collect_warnings(str).unwrap();

        let expected_intralinks = IntralinksConfig {
            docs: IntralinksDocsConfig::DocsRs {
                base_url: Some("https://internaldocs.rs".to_owned()),
                version: Some("1.0.0".to_owned()),
            },
            strip_links: None,
            all_features: None,
            features: None,
            no_default_features: None,
            rustdoc_toolchain: None,
        };

        assert_eq!(config_file_opts.intralinks, Some(expected_intralinks));
        assert_eq!(warnings.len(), 2);
        assert!(warnings[0].contains("docs-rs-base-url"));
        assert!(warnings[1].contains("docs-rs-version"));
    }

    #[test]
    fn test_config_file_options_from_str_conflicting_base_url() {
        let str = indoc! { r#"
            [intralinks]
            docs-rs-base-url = "https://internaldocs.rs"

            [intralinks.docs]
            base-url = "https://other.docs"
            "#
        };

        let err = parse(str).unwrap_err();
        assert!(matches!(err, ConfigFileOptionsError::ConflictingDocsOptions { .. }));
    }

    #[test]
    fn test_config_file_options_from_str_conflicting_version() {
        let str = indoc! { r#"
            [intralinks]
            docs-rs-version = "1.0.0"

            [intralinks.docs]
            version = "2.0.0"
            "#
        };

        let err = parse(str).unwrap_err();
        assert!(matches!(err, ConfigFileOptionsError::ConflictingDocsOptions { .. }));
    }

    #[test]
    fn test_config_file_options_from_str_flat_requires_base_url() {
        let str = indoc! { r#"
            [intralinks.docs]
            layout = "flat"
            "#
        };

        let err = parse(str).unwrap_err();
        assert!(matches!(err, ConfigFileOptionsError::FlatLayoutRequiresBaseUrl));
    }

    #[test]
    fn test_config_file_options_from_str_flat_rejects_version() {
        let str = indoc! { r#"
            [intralinks.docs]
            layout = "flat"
            base-url = "https://mydocs.example"
            version = "1.0.0"
            "#
        };

        let err = parse(str).unwrap_err();
        assert!(matches!(err, ConfigFileOptionsError::FlatLayoutRejectsVersion));
    }

    #[test]
    fn test_config_file_options_from_str_flat_rejects_deprecated_version() {
        let str = indoc! { r#"
            [intralinks]
            docs-rs-version = "1.0.0"

            [intralinks.docs]
            layout = "flat"
            base-url = "https://mydocs.example"
            "#
        };

        let err = parse(str).unwrap_err();
        assert!(matches!(err, ConfigFileOptionsError::FlatLayoutRejectsVersion));
    }

    #[test]
    fn test_config_file_options_from_str_invalid_layout() {
        let str = indoc! { r#"
            [intralinks.docs]
            layout = "bogus"
            "#
        };

        let err = parse(str).unwrap_err();
        assert!(matches!(err, ConfigFileOptionsError::InvalidField("intralinks.docs.layout")));
    }

    #[test]
    fn test_merge_cmd_wins_over_config_file() {
        let cmd_options = CmdOptions {
            workspace_project: Some("myproj".to_owned()),
            entrypoint: Some(EntrypointOpt::BinDefault),
            line_terminator: Some(LineTerminatorOpt::CrLf),
            check: true,
            no_fail_on_warnings: true,
            intralinks_strip_links: true,
            force: true,
            readme_path: Some(PathBuf::from("rEaDmE.md")),
            manifest_path: Some(PathBuf::from("foo/Cargo.toml")),
            intralinks_all_features: true,
            intralinks_features: Some(vec!["foo".to_owned(), "bar".to_owned()]),
            intralinks_no_default_features: true,
            heading_base_level: Some(4),
        };
        let config_file_options = ConfigFileOptions {
            workspace_project: Some("aproj".to_owned()),
            entrypoint: Some(EntrypointOpt::Lib),
            line_terminator: Some(LineTerminatorOpt::Lf),
            readme_path: Some(PathBuf::from("ReAdMe.md")),
            intralinks: Some(IntralinksConfig {
                docs: IntralinksDocsConfig::DocsRs {
                    base_url: Some("https://internaldocs.rs".to_owned()),
                    version: Some("1.0.0".to_owned()),
                },
                strip_links: Some(false),
                all_features: Some(false),
                features: Some(vec!["mumble".to_owned()]),
                no_default_features: Some(false),
                rustdoc_toolchain: Some("nightly-2026-08-03".to_owned()),
            }),
            heading_base_level: Some(3),
        };

        let options = merge_options(cmd_options, Some(config_file_options));

        let expected = Options {
            workspace_project: Some("myproj".to_owned()),
            entrypoint: EntrypointOpt::BinDefault,
            line_terminator: LineTerminatorOpt::CrLf,
            check: true,
            no_fail_on_warnings: true,
            force: true,
            readme_path: Some(PathBuf::from("rEaDmE.md")),
            manifest_path: Some(PathBuf::from("foo/Cargo.toml")),
            intralinks: Some(IntralinksConfig {
                docs: IntralinksDocsConfig::DocsRs {
                    base_url: Some("https://internaldocs.rs".to_owned()),
                    version: Some("1.0.0".to_owned()),
                },
                strip_links: Some(true),
                all_features: Some(true),
                features: Some(vec!["foo".to_owned(), "bar".to_owned()]),
                no_default_features: Some(true),
                rustdoc_toolchain: Some("nightly-2026-08-03".to_owned()),
            }),
            heading_base_level: Some(4),
        };

        assert_eq!(options, expected);
    }
}