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. Source-backed path and
6//! display data remain implementation details behind this module's APIs.
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 check;
12pub(crate) mod emitter;
13mod member;
14pub(crate) mod parser;
15
16pub(crate) mod reconciliation;
17pub(crate) mod schema;
18pub(crate) mod table;
19
20/// A segment of a path accepted by settings schema lookups.
21#[derive(Debug, Clone, Copy, Hash)]
22pub enum PathPart<'a> {
23    /// A literal key (mode names under `gamemodes` are literal keys too:
24    /// per-key subsets are exact-path entries).
25    Part(&'a str),
26    /// Any team slot (allTeams).
27    Team,
28    /// Any hero-config slot.
29    Hero,
30}
31
32impl<'b> PartialEq<PathPart<'b>> for PathPart<'_> {
33    fn eq(&self, other: &PathPart<'b>) -> bool {
34        match (self, other) {
35            (PathPart::Part(left), PathPart::Part(right)) => left == right,
36            (PathPart::Team, PathPart::Team) => true,
37            (PathPart::Hero, PathPart::Hero) => true,
38            _ => false,
39        }
40    }
41}
42
43impl Eq for PathPart<'_> {}
44
45pub use check::{
46    DiagnosticSeverity, SettingsDiagnostic, check_emission, check_emission_diagnostics,
47};
48pub use schema::{
49    Applicability, EffectiveNumber, NumericBounds, NumericBoundsError, SettingDefinition,
50    SettingEnumMember, SettingId, SettingIdentity, SettingOccurrence, SettingOperationError,
51    SettingPresentation, SettingScope, SettingSource, SettingSourceEdit, SettingSourceKind,
52    SettingTarget, SettingTargetKind, SettingValue, SettingValueDomain, TeamId, definition,
53    definitions, definitions_by_id, validate_catalog,
54};
55pub use table::{LocalizedSettingName, localized_setting_names};
56
57use crate::core::source::Span;
58
59/// A settings block: `settings { ... }` with its typed children.
60#[derive(Debug, Clone)]
61pub struct Settings {
62    pub span: Option<Span>,
63    pub children: Vec<SettingsNode>,
64}
65
66/// One member of a settings group.
67#[derive(Debug, Clone)]
68pub enum SettingsNode {
69    /// User-authored mode data under `settings.workshop`.
70    Workshop {
71        children: Vec<SettingsNode>,
72        span: Option<Span>,
73    },
74    Group {
75        name: String,
76        children: Vec<SettingsNode>,
77        span: Option<Span>,
78    },
79    Number {
80        name: String,
81        value: f64,
82        span: Option<Span>,
83    },
84    Bool {
85        name: String,
86        value: bool,
87        span: Option<Span>,
88    },
89    /// A presence-only Workshop extension setting (for example `Beam Effects`).
90    Flag { name: String, span: Option<Span> },
91    String {
92        name: String,
93        value: String,
94        span: Option<Span>,
95    },
96    List {
97        name: String,
98        elements: Vec<SettingsListElement>,
99        span: Option<Span>,
100    },
101    /// A syntactically valid settings member whose semantic catalog entry is
102    /// not yet declared. The raw value is carried explicitly so parsing does
103    /// not fabricate a type or silently discard project settings.
104    Raw {
105        name: String,
106        value: String,
107        span: Option<Span>,
108    },
109    /// A member written as `name: value` with the value as authored and never
110    /// interpreted. At a catalogued path `name` is the canonical key and is
111    /// emitted in the target locale; elsewhere it is written as is. Carries
112    /// catalogued keys with values the catalog does not declare, and members
113    /// whose empty value still needs its `:`.
114    RawValue {
115        name: String,
116        value: String,
117        span: Option<Span>,
118    },
119    /// A member written as `name { ... }` with the block emitted verbatim:
120    /// the pass-through marker for non-scalar members at positions where a
121    /// plain `Group` is name-resolved or kind-checked (a hero group under a
122    /// team, a member block under a catalogued key). The name is written as
123    /// authored and children carry their written form; a typed leaf inside
124    /// still fails the written-form checks the opaque-group path applies.
125    RawGroup {
126        name: String,
127        children: Vec<SettingsNode>,
128        span: Option<Span>,
129    },
130}
131
132/// One element of a settings list (corpus lists are all strings).
133#[derive(Debug, Clone)]
134pub struct SettingsListElement {
135    pub value: String,
136    pub span: Option<Span>,
137}
138
139impl SettingsListElement {
140    fn clear_span(&mut self) {
141        self.span = None;
142    }
143}
144
145impl Settings {
146    /// Drop every span recorded on the block and its members.
147    ///
148    /// Called when the program's file table is rebound to a different
149    /// coordinate space (`SourceMap::apply`): the map carries no settings
150    /// entries, so the spans parsed against the previous text cannot be
151    /// re-anchored and must not survive as stale positions.
152    pub(crate) fn clear_spans(&mut self) {
153        self.span = None;
154        for node in &mut self.children {
155            node.clear_spans();
156        }
157    }
158}
159
160impl SettingsNode {
161    /// The source span of this node, if any.
162    pub fn span(&self) -> Option<Span> {
163        match self {
164            SettingsNode::Workshop { span, .. }
165            | SettingsNode::Group { span, .. }
166            | SettingsNode::Number { span, .. }
167            | SettingsNode::Bool { span, .. }
168            | SettingsNode::Flag { span, .. }
169            | SettingsNode::String { span, .. }
170            | SettingsNode::List { span, .. }
171            | SettingsNode::Raw { span, .. }
172            | SettingsNode::RawValue { span, .. }
173            | SettingsNode::RawGroup { span, .. } => *span,
174        }
175    }
176
177    /// Drop this node's span and, for containers, every descendant span.
178    fn clear_spans(&mut self) {
179        match self {
180            SettingsNode::Workshop { children, span }
181            | SettingsNode::Group { children, span, .. }
182            | SettingsNode::RawGroup { children, span, .. } => {
183                *span = None;
184                for node in children {
185                    node.clear_spans();
186                }
187            }
188            SettingsNode::Number { span, .. }
189            | SettingsNode::Bool { span, .. }
190            | SettingsNode::Flag { span, .. }
191            | SettingsNode::String { span, .. }
192            | SettingsNode::Raw { span, .. }
193            | SettingsNode::RawValue { span, .. } => *span = None,
194            SettingsNode::List { elements, span, .. } => {
195                *span = None;
196                for element in elements {
197                    element.clear_span();
198                }
199            }
200        }
201    }
202
203    /// The key name of this node.
204    pub fn name(&self) -> &str {
205        match self {
206            SettingsNode::Workshop { .. } => "workshop",
207            SettingsNode::Group { name, .. }
208            | SettingsNode::Number { name, .. }
209            | SettingsNode::Bool { name, .. }
210            | SettingsNode::Flag { name, .. }
211            | SettingsNode::String { name, .. }
212            | SettingsNode::List { name, .. }
213            | SettingsNode::Raw { name, .. }
214            | SettingsNode::RawValue { name, .. }
215            | SettingsNode::RawGroup { name, .. } => name,
216        }
217    }
218}