Skip to main content

hemtt_config/
options.rs

1#![allow(clippy::use_self)] // serde false positive
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Default, Clone, Serialize, Deserialize)]
6/// Preset of options to use for parsing
7///
8/// HEMTT: Superset of BI with some QOL improvements
9/// BI: Match a strict definition of BI's parser
10pub enum Preset {
11    /// Superset of BI with some QOL improvements
12    Hemtt,
13    #[default]
14    /// Match a strict definition of BI's parser
15    Bi,
16}
17
18#[derive(Default, Debug, Clone, Serialize, Deserialize)]
19/// Options for parsing
20pub struct Options {
21    #[serde(default = "default_preset")]
22    /// Preset to use for config parsing
23    ///
24    /// BI: Match a strict definition of BI's parser
25    /// HEMTT: Superset of BI with some QOL improvements
26    preset: Preset,
27
28    /// Can arrays have trailing commas?
29    ///
30    /// See [`Options::array_allow_trailing_comma`]
31    array_allow_trailing_comma: Option<bool>,
32}
33
34impl Options {
35    #[must_use]
36    /// Create a new set of options from a preset
37    pub fn from_preset(preset: Preset) -> Self {
38        Self {
39            preset,
40            ..Default::default()
41        }
42    }
43
44    #[must_use]
45    /// Can arrays have trailing commas?
46    ///
47    /// Default (BI): `false`
48    /// Default (HEMTT): `true`
49    ///
50    /// When false, the following is invalid:
51    /// ```cpp
52    /// my_array[] = {
53    ///     1,
54    ///     2, // <- trailing comma on last element
55    /// };
56    /// ```
57    pub const fn array_allow_trailing_comma(&self) -> bool {
58        if let Some(allow) = self.array_allow_trailing_comma {
59            allow
60        } else {
61            match self.preset {
62                Preset::Hemtt => true,
63                Preset::Bi => false,
64            }
65        }
66    }
67}
68
69const fn default_preset() -> Preset {
70    Preset::Bi
71}