Skip to main content

workshop_rs/settings/
mod.rs

1//! The neutral settings carrier.
2//!
3//! A typed, non-serde tree for custom-game-settings blocks shared by
4//! validation and emission. The tree is a carrier: settings are carried and
5//! emitted, never interpreted by lowering/analysis. The fixture-evidenced
6//! emission table lives in [`table`].
7//!
8//! Extracted from the Wright-authored `wright-ir` crate; see
9//! [`docs/provenance.md`](https://github.com/wrightkit/workshop-rs/blob/main/docs/provenance.md).
10
11pub(crate) mod emitter;
12pub(crate) mod parser;
13
14pub(crate) mod reconciliation;
15pub mod schema;
16pub mod table;
17
18pub use schema::{
19    Applicability, EffectiveNumber, NumericBounds, NumericBoundsError, SettingDefinition,
20    SettingEvidenceKind, SettingId, SettingIdentity, SettingOccurrence, SettingOperationError,
21    SettingPresentation, SettingProvenance, SettingScope, SettingTarget, SettingTargetKind,
22    SettingValue, SettingValueDomain, TeamId, definition, definitions, definitions_by_id,
23};
24
25use crate::core::source::Span;
26
27/// A settings block: `settings { ... }` with its typed children.
28#[derive(Debug, Clone)]
29pub struct Settings {
30    pub span: Option<Span>,
31    pub children: Vec<SettingsNode>,
32}
33
34/// One member of a settings group.
35#[derive(Debug, Clone)]
36pub enum SettingsNode {
37    /// User-authored mode data under `settings.workshop`.
38    Workshop {
39        children: Vec<SettingsNode>,
40        span: Option<Span>,
41    },
42    Group {
43        name: String,
44        children: Vec<SettingsNode>,
45        span: Option<Span>,
46    },
47    Number {
48        name: String,
49        value: f64,
50        span: Option<Span>,
51    },
52    Bool {
53        name: String,
54        value: bool,
55        span: Option<Span>,
56    },
57    /// A presence-only Workshop extension setting (for example `Beam Effects`).
58    Flag { name: String, span: Option<Span> },
59    String {
60        name: String,
61        value: String,
62        span: Option<Span>,
63    },
64    List {
65        name: String,
66        elements: Vec<SettingsListElement>,
67        span: Option<Span>,
68    },
69    /// A syntactically valid settings member whose semantic catalog entry is
70    /// not yet declared. The raw value is carried explicitly so parsing does
71    /// not fabricate a type or silently discard project settings.
72    Raw {
73        name: String,
74        value: String,
75        span: Option<Span>,
76    },
77}
78
79/// One element of a settings list (corpus lists are all strings).
80#[derive(Debug, Clone)]
81pub struct SettingsListElement {
82    pub value: String,
83    pub span: Option<Span>,
84}
85
86impl SettingsNode {
87    /// The source span of this node, if any.
88    pub fn span(&self) -> Option<Span> {
89        match self {
90            SettingsNode::Workshop { span, .. }
91            | SettingsNode::Group { span, .. }
92            | SettingsNode::Number { span, .. }
93            | SettingsNode::Bool { span, .. }
94            | SettingsNode::Flag { span, .. }
95            | SettingsNode::String { span, .. }
96            | SettingsNode::List { span, .. }
97            | SettingsNode::Raw { span, .. } => *span,
98        }
99    }
100
101    /// The key name of this node.
102    pub fn name(&self) -> &str {
103        match self {
104            SettingsNode::Workshop { .. } => "workshop",
105            SettingsNode::Group { name, .. }
106            | SettingsNode::Number { name, .. }
107            | SettingsNode::Bool { name, .. }
108            | SettingsNode::Flag { name, .. }
109            | SettingsNode::String { name, .. }
110            | SettingsNode::List { name, .. }
111            | SettingsNode::Raw { name, .. } => name,
112        }
113    }
114}