From d53c274c1243cee6a8af1c0c3a825a945ef7a34d Mon Sep 17 00:00:00 2001 From: "John T. Wodder II" Date: Sun, 29 Oct 2023 15:48:11 -0400 Subject: [PATCH] Feature-gate applicable doctests --- src/lib.rs | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cfe86f5..a55ef87 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -189,16 +189,17 @@ impl Format { /// File extensions are matched case-insensitively and may optionally start /// with a period. If the given file extension does not correspond to a /// known file format, `None` is returned. - /// - /// # Example - /// - /// ``` - /// use cfgurate::Format; - /// - /// assert_eq!(Format::from_extension(".json"), Some(Format::Json)); - /// assert_eq!(Format::from_extension("YML"), Some(Format::Yaml)); - /// assert_eq!(Format::from_extension("cfg"), None); - /// ``` + #[cfg_attr(all(feature = "json", feature = "yaml"), doc = concat!( + "# Example\n", + "\n", + "```\n", + "use cfgurate::Format;\n", + "\n", + "assert_eq!(Format::from_extension(\".json\"), Some(Format::Json));\n", + "assert_eq!(Format::from_extension(\"YML\"), Some(Format::Yaml));\n", + "assert_eq!(Format::from_extension(\"cfg\"), None);\n", + "```\n", + ))] pub fn from_extension(ext: &str) -> Option { let ext = ext.strip_prefix('.').unwrap_or(ext).to_ascii_lowercase(); let ext = &*ext; @@ -209,18 +210,18 @@ impl Format { /// /// Only [enabled][Format::is_enabled] formats are supported by this /// method. - /// - /// # Example - /// - /// ``` - /// use cfgurate::Format; - /// - /// assert_eq!(Format::identify("path/to/file.json").unwrap(), Format::Json); - /// assert_eq!(Format::identify("path/to/file.RON").unwrap(), Format::Ron); - /// assert!(Format::identify("path/to/file.cfg").is_err()); - /// assert!(Format::identify("path/to/file").is_err()); - /// ``` - /// + #[cfg_attr(all(feature = "json", feature = "ron"), doc = concat!( + "# Example\n", + "\n", + "```\n", + "use cfgurate::Format;\n", + "\n", + "assert_eq!(Format::identify(\"path/to/file.json\").unwrap(), Format::Json);\n", + "assert_eq!(Format::identify(\"path/to/file.RON\").unwrap(), Format::Ron);\n", + "assert!(Format::identify(\"path/to/file.cfg\").is_err());\n", + "assert!(Format::identify(\"path/to/file\").is_err());\n", + "```\n", + ))] /// # Errors /// /// Returns an error if the given file path does not have an extension, the