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};
55
56use crate::core::source::Span;
57
58/// A settings block: `settings { ... }` with its typed children.
59#[derive(Debug, Clone)]
60pub struct Settings {
61    pub span: Option<Span>,
62    pub children: Vec<SettingsNode>,
63}
64
65/// One member of a settings group.
66#[derive(Debug, Clone)]
67pub enum SettingsNode {
68    /// User-authored mode data under `settings.workshop`.
69    Workshop {
70        children: Vec<SettingsNode>,
71        span: Option<Span>,
72    },
73    Group {
74        name: String,
75        children: Vec<SettingsNode>,
76        span: Option<Span>,
77    },
78    Number {
79        name: String,
80        value: f64,
81        span: Option<Span>,
82    },
83    Bool {
84        name: String,
85        value: bool,
86        span: Option<Span>,
87    },
88    /// A presence-only Workshop extension setting (for example `Beam Effects`).
89    Flag { name: String, span: Option<Span> },
90    String {
91        name: String,
92        value: String,
93        span: Option<Span>,
94    },
95    List {
96        name: String,
97        elements: Vec<SettingsListElement>,
98        span: Option<Span>,
99    },
100    /// A syntactically valid settings member whose semantic catalog entry is
101    /// not yet declared. The raw value is carried explicitly so parsing does
102    /// not fabricate a type or silently discard project settings.
103    Raw {
104        name: String,
105        value: String,
106        span: Option<Span>,
107    },
108    /// A member written as `name: value` with the value as authored and never
109    /// interpreted. At a catalogued path `name` is the canonical key and is
110    /// emitted in the target locale; elsewhere it is written as is. Carries
111    /// catalogued keys with values the catalog does not declare, and members
112    /// whose empty value still needs its `:`.
113    RawValue {
114        name: String,
115        value: String,
116        span: Option<Span>,
117    },
118    /// A member written as `name { ... }` with the block emitted verbatim:
119    /// the pass-through marker for non-scalar members at positions where a
120    /// plain `Group` is name-resolved or kind-checked (a hero group under a
121    /// team, a member block under a catalogued key). The name is written as
122    /// authored and children carry their written form; a typed leaf inside
123    /// still fails the written-form checks the opaque-group path applies.
124    RawGroup {
125        name: String,
126        children: Vec<SettingsNode>,
127        span: Option<Span>,
128    },
129}
130
131/// One element of a settings list (corpus lists are all strings).
132#[derive(Debug, Clone)]
133pub struct SettingsListElement {
134    pub value: String,
135    pub span: Option<Span>,
136}
137
138impl SettingsListElement {
139    fn clear_span(&mut self) {
140        self.span = None;
141    }
142}
143
144impl Settings {
145    /// Drop every span recorded on the block and its members.
146    ///
147    /// Called when the program's file table is rebound to a different
148    /// coordinate space (`SourceMap::apply`): the map carries no settings
149    /// entries, so the spans parsed against the previous text cannot be
150    /// re-anchored and must not survive as stale positions.
151    pub(crate) fn clear_spans(&mut self) {
152        self.span = None;
153        for node in &mut self.children {
154            node.clear_spans();
155        }
156    }
157}
158
159impl SettingsNode {
160    /// The source span of this node, if any.
161    pub fn span(&self) -> Option<Span> {
162        match self {
163            SettingsNode::Workshop { span, .. }
164            | SettingsNode::Group { span, .. }
165            | SettingsNode::Number { span, .. }
166            | SettingsNode::Bool { span, .. }
167            | SettingsNode::Flag { span, .. }
168            | SettingsNode::String { span, .. }
169            | SettingsNode::List { span, .. }
170            | SettingsNode::Raw { span, .. }
171            | SettingsNode::RawValue { span, .. }
172            | SettingsNode::RawGroup { span, .. } => *span,
173        }
174    }
175
176    /// Drop this node's span and, for containers, every descendant span.
177    fn clear_spans(&mut self) {
178        match self {
179            SettingsNode::Workshop { children, span }
180            | SettingsNode::Group { children, span, .. }
181            | SettingsNode::RawGroup { children, span, .. } => {
182                *span = None;
183                for node in children {
184                    node.clear_spans();
185                }
186            }
187            SettingsNode::Number { span, .. }
188            | SettingsNode::Bool { span, .. }
189            | SettingsNode::Flag { span, .. }
190            | SettingsNode::String { span, .. }
191            | SettingsNode::Raw { span, .. }
192            | SettingsNode::RawValue { span, .. } => *span = None,
193            SettingsNode::List { elements, span, .. } => {
194                *span = None;
195                for element in elements {
196                    element.clear_span();
197                }
198            }
199        }
200    }
201
202    /// The key name of this node.
203    pub fn name(&self) -> &str {
204        match self {
205            SettingsNode::Workshop { .. } => "workshop",
206            SettingsNode::Group { name, .. }
207            | SettingsNode::Number { name, .. }
208            | SettingsNode::Bool { name, .. }
209            | SettingsNode::Flag { name, .. }
210            | SettingsNode::String { name, .. }
211            | SettingsNode::List { name, .. }
212            | SettingsNode::Raw { name, .. }
213            | SettingsNode::RawValue { name, .. }
214            | SettingsNode::RawGroup { name, .. } => name,
215        }
216    }
217}