Skip to main content

clippy_config/
metadata.rs

1use itertools::Itertools as _;
2use std::fmt::{self, Display};
3
4pub struct ConfMetadata {
5    pub name: &'static str,
6    pub default: String,
7    pub lints: &'static [&'static str],
8    pub doc: &'static str,
9    pub renamed_to: Option<&'static str>,
10}
11
12impl Display for ConfMetadata {
13    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
14        write!(f, "- `{}`: {}", self.name, self.doc)?;
15        if !self.default.is_empty() {
16            write!(f, "\n\n   (default: `{}`)", self.default)?;
17        }
18        Ok(())
19    }
20}
21
22impl ConfMetadata {
23    pub fn display_markdown_paragraph(&self) -> impl '_ + Display {
24        struct S<'a>(&'a ConfMetadata);
25        impl Display for S<'_> {
26            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27                write!(
28                    f,
29                    "## `{}`\n{}\n\n**Default Value:** `{}`\n\n---\n**Affected lints:**\n{}\n\n",
30                    self.0.name,
31                    self.0
32                        .doc
33                        .lines()
34                        .format_with("\n", |doc, f| f(&doc.strip_prefix(" ").unwrap_or(doc))),
35                    self.0.default,
36                    self.0.lints.iter().format_with("\n", |name, f| f(&format_args!(
37                        "* [`{name}`](https://rust-lang.github.io/rust-clippy/master/index.html#{name})"
38                    ))),
39                )
40            }
41        }
42        S(self)
43    }
44
45    pub fn display_markdown_link(&self) -> impl '_ + Display {
46        struct S<'a>(&'a ConfMetadata);
47        impl Display for S<'_> {
48            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49                write!(
50                    f,
51                    "[`{}`]: https://doc.rust-lang.org/clippy/lint_configuration.html#{}",
52                    self.0.name, self.0.name,
53                )
54            }
55        }
56        S(self)
57    }
58}