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