gdnative_doc/
config.rs

1//! User configuration settings.
2
3use crate::Error;
4use serde::Deserialize;
5use std::{collections::HashMap, fs, path::PathBuf};
6
7/// Structure that holds user configuration settings.
8///
9/// Should be obtained via a `toml` configuration file.
10///
11/// # Example
12/// ```
13/// # use gdnative_doc::{Error, ConfigFile};
14/// # fn main() -> Result<(), Error> {
15/// const CONFIG_FILE_CONTENT: &str = r#"
16/// rename_classes = { RustName = "GDScriptName" }
17/// markdown_options = ["STRIKETHROUGH", "TABLES"]
18/// "#;
19///
20/// let config_file = ConfigFile::load_from_str(CONFIG_FILE_CONTENT)?;
21/// assert!(config_file.url_overrides.is_none());
22/// assert_eq!(config_file.rename_classes.unwrap()["RustName"], "GDScriptName");
23/// assert_eq!(
24///     config_file.markdown_options.unwrap(),
25///     &["STRIKETHROUGH".to_string(), "TABLES".to_string()]
26/// );
27/// # Ok(()) }
28/// ```
29///
30/// Note that if you are reading the configuration file from an on-disk file, you
31/// should prefer [`load_from_path`](ConfigFile::load_from_path).
32// Note: any update to this structure should be documented in
33// configuration_file-format.md.
34#[derive(Clone, Debug, Default, Deserialize, PartialEq, Eq)]
35pub struct ConfigFile {
36    /// Godot version used.
37    ///
38    /// Valid fields are "3.2", "3.3", "3.4" and "3.5".
39    ///
40    /// Defaults to "3.5".
41    pub godot_version: Option<String>,
42    /// List of items for which the linking url should be overriden.
43    pub url_overrides: Option<HashMap<String, String>>,
44    /// Renaming of types when going from Rust to Godot.
45    ///
46    /// This is useful because GDNative allows defining a `script_class_name` in the
47    /// `.gdns` file.
48    pub rename_classes: Option<HashMap<String, String>>,
49    /// Optional markdown options.
50    ///
51    /// # Valid options
52    /// - FOOTNOTES
53    /// - SMART_PUNCTUATION
54    /// - STRIKETHROUGH
55    /// - TABLES
56    /// - TASKLISTS
57    ///
58    /// # Default
59    /// No option enabled.
60    pub markdown_options: Option<Vec<String>>,
61    /// Control whether or not to include a comment in the generated files.
62    ///
63    /// The comment includes information such that the file was automatically
64    /// generated, the name of the source file it originated from...
65    ///
66    /// # Default
67    /// `true`
68    pub opening_comment: Option<bool>,
69}
70
71impl ConfigFile {
72    /// Load the config file from the given `path`.
73    pub fn load_from_path(path: PathBuf) -> Result<Self, Error> {
74        log::debug!("loading user config at {:?}", path);
75        Ok(toml::from_str(&match fs::read_to_string(&path) {
76            Ok(config) => config,
77            Err(err) => return Err(Error::Io(path, err)),
78        })?)
79    }
80
81    /// Load the config file from the given `config` string.
82    pub fn load_from_str(config: &str) -> Result<Self, Error> {
83        Ok(toml::from_str(config)?)
84    }
85
86    /// Convert the `String` list of options to `pulldown_cmark::Options`, logging
87    /// warnings on unrecognized options.
88    pub(crate) fn markdown_options(&self) -> Option<pulldown_cmark::Options> {
89        use pulldown_cmark::Options;
90        if let Some(options) = &self.markdown_options {
91            let mut markdown_options = Options::empty();
92            for option in options {
93                match option.as_str() {
94                    "FOOTNOTES" => markdown_options.insert(Options::ENABLE_FOOTNOTES),
95                    "SMART_PUNCTUATION" => {
96                        markdown_options.insert(Options::ENABLE_SMART_PUNCTUATION)
97                    }
98                    "STRIKETHROUGH" => markdown_options.insert(Options::ENABLE_STRIKETHROUGH),
99                    "TABLES" => markdown_options.insert(Options::ENABLE_TABLES),
100                    "TASKLISTS" => markdown_options.insert(Options::ENABLE_TASKLISTS),
101                    _ => log::warn!("unknown markdown option: {}", option),
102                }
103            }
104            Some(markdown_options)
105        } else {
106            None
107        }
108    }
109}