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