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    pub(crate) fn clear_spans(&mut self) {
147        self.span = None;
148        for node in &mut self.children {
149            node.clear_spans();
150        }
151    }
152}
153
154impl SettingsNode {
155    /// The source span of this node, if any.
156    pub fn span(&self) -> Option<Span> {
157        match self {
158            SettingsNode::Workshop { span, .. }
159            | SettingsNode::Group { span, .. }
160            | SettingsNode::Number { span, .. }
161            | SettingsNode::Bool { span, .. }
162            | SettingsNode::Flag { span, .. }
163            | SettingsNode::String { span, .. }
164            | SettingsNode::List { span, .. }
165            | SettingsNode::Raw { span, .. }
166            | SettingsNode::RawValue { span, .. }
167            | SettingsNode::RawGroup { span, .. } => *span,
168        }
169    }
170
171    fn clear_spans(&mut self) {
172        match self {
173            SettingsNode::Workshop { children, span }
174            | SettingsNode::Group { children, span, .. }
175            | SettingsNode::RawGroup { children, span, .. } => {
176                *span = None;
177                for node in children {
178                    node.clear_spans();
179                }
180            }
181            SettingsNode::Number { span, .. }
182            | SettingsNode::Bool { span, .. }
183            | SettingsNode::Flag { span, .. }
184            | SettingsNode::String { span, .. }
185            | SettingsNode::Raw { span, .. }
186            | SettingsNode::RawValue { span, .. } => *span = None,
187            SettingsNode::List { elements, span, .. } => {
188                *span = None;
189                for element in elements {
190                    element.clear_span();
191                }
192            }
193        }
194    }
195
196    /// The key name of this node.
197    pub fn name(&self) -> &str {
198        match self {
199            SettingsNode::Workshop { .. } => "workshop",
200            SettingsNode::Group { name, .. }
201            | SettingsNode::Number { name, .. }
202            | SettingsNode::Bool { name, .. }
203            | SettingsNode::Flag { name, .. }
204            | SettingsNode::String { name, .. }
205            | SettingsNode::List { name, .. }
206            | SettingsNode::Raw { name, .. }
207            | SettingsNode::RawValue { name, .. }
208            | SettingsNode::RawGroup { name, .. } => name,
209        }
210    }
211}