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