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
//! User configuration settings.

use crate::Result;
use serde::Deserialize;
use std::collections::HashMap;

/// Structure that holds user configuration settings.
///
/// Should be obtained via a `toml` [configuration file](ConfigFile::read_from).
///
/// # Example
/// ```
/// # use gdnative_doc::{Result, ConfigFile};
/// # fn main() -> Result<()> {
/// const CONFIG_FILE_CONTENT: &str = r#"
/// rename_classes = { RustName = "GDScriptName" }
/// markdown_options = ["STRIKETHROUGH", "TABLES"]
/// "#;
///
/// let config_file = ConfigFile::read_from(CONFIG_FILE_CONTENT)?;
/// assert!(config_file.url_overrides.is_none());
/// assert_eq!(config_file.rename_classes.unwrap()["RustName"], "GDScriptName");
/// assert_eq!(
///     config_file.markdown_options.unwrap(),
///     &["STRIKETHROUGH".to_string(), "TABLES".to_string()]
/// );
/// # Ok(()) }
/// ```
#[derive(Clone, Debug, Deserialize, PartialEq, Eq)]
pub struct ConfigFile {
    /// List of items for which the linking url should be overriden.
    pub url_overrides: Option<HashMap<String, String>>,
    /// Renaming of types when going from Rust to Godot.
    ///
    /// This is useful because GDNative allows defining a `script_class_name` in the
    /// `.gdns` file.
    pub rename_classes: Option<HashMap<String, String>>,
    /// Optional markdown options.
    ///
    /// # Valid options
    /// - FOOTNOTES
    /// - SMART_PUNCTUATION
    /// - STRIKETHROUGH
    /// - TABLES
    /// - TASKLISTS
    ///
    /// # Default
    /// No option enabled.
    pub markdown_options: Option<Vec<String>>,
}

impl ConfigFile {
    /// Read `ConfigFile` from the given `toml` configuration file.
    pub fn read_from(config: &str) -> Result<Self> {
        Ok(toml::from_str(config)?)
    }

    /// Convert the `String` list of options to `pulldown_cmark::Options`, logging
    /// warnings on unrecognized options.
    pub(crate) fn markdown_options(&self) -> Option<pulldown_cmark::Options> {
        use pulldown_cmark::Options;
        if let Some(options) = &self.markdown_options {
            let mut markdown_options = Options::empty();
            for option in options {
                match option.as_str() {
                    "FOOTNOTES" => markdown_options.insert(Options::ENABLE_FOOTNOTES),
                    "SMART_PUNCTUATION" => {
                        markdown_options.insert(Options::ENABLE_SMART_PUNCTUATION)
                    }
                    "STRIKETHROUGH" => markdown_options.insert(Options::ENABLE_STRIKETHROUGH),
                    "TABLES" => markdown_options.insert(Options::ENABLE_TABLES),
                    "TASKLISTS" => markdown_options.insert(Options::ENABLE_TASKLISTS),
                    _ => log::warn!("unknown markdown option: {}", option),
                }
            }
            Some(markdown_options)
        } else {
            None
        }
    }
}