nucleation 0.3.16

A high-performance Minecraft schematic parser and utility library
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Unified CircuitBuilder for streamlined executor creation
//!
//! This module provides a builder pattern for creating `TypedCircuitExecutor` instances
//! with a fluent API that combines IO layout definition and simulation configuration.
//!
//! # Example
//!
//! ```ignore
//! use nucleation::simulation::circuit_builder::CircuitBuilder;
//! use nucleation::simulation::typed_executor::{IoType, LayoutFunction, SortStrategy};
//!
//! let executor = CircuitBuilder::new(schematic)
//!     .with_input("a", IoType::UnsignedInt { bits: 8 }, region_a)?
//!     .with_input("b", IoType::UnsignedInt { bits: 8 }, region_b)?
//!     .with_output("sum", IoType::UnsignedInt { bits: 9 }, region_sum)?
//!     .with_options(SimulationOptions::default())
//!     .validate()?
//!     .build()?;
//!
//! // With custom sort strategy:
//! let executor = CircuitBuilder::new(schematic)
//!     .with_input_sorted("a", IoType::UnsignedInt { bits: 8 }, region_a, SortStrategy::YDescXZ)?
//!     .build()?;
//! ```

use crate::definition_region::DefinitionRegion;
use crate::simulation::typed_executor::{
    IoLayoutBuilder, IoType, LayoutFunction, SortStrategy, StateMode, TypedCircuitExecutor,
};
use crate::simulation::{MchprsWorld, SimulationOptions};
use crate::UniversalSchematic;
use std::collections::HashMap;

/// Validation errors for circuit builder
#[derive(Debug, Clone)]
pub enum ValidationError {
    /// Two IO regions overlap
    OverlappingRegions {
        name1: String,
        name2: String,
        overlap_count: usize,
    },
    /// No inputs defined
    NoInputs,
    /// No outputs defined
    NoOutputs,
    /// Input has no positions
    EmptyInput(String),
    /// Output has no positions
    EmptyOutput(String),
    /// Custom validation error
    Custom(String),
}

impl std::fmt::Display for ValidationError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ValidationError::OverlappingRegions {
                name1,
                name2,
                overlap_count,
            } => write!(
                f,
                "IO regions '{}' and '{}' overlap at {} positions",
                name1, name2, overlap_count
            ),
            ValidationError::NoInputs => write!(f, "No inputs defined"),
            ValidationError::NoOutputs => write!(f, "No outputs defined"),
            ValidationError::EmptyInput(name) => write!(f, "Input '{}' has no positions", name),
            ValidationError::EmptyOutput(name) => write!(f, "Output '{}' has no positions", name),
            ValidationError::Custom(msg) => write!(f, "{}", msg),
        }
    }
}

impl std::error::Error for ValidationError {}

/// Builder for creating TypedCircuitExecutor instances
pub struct CircuitBuilder {
    schematic: UniversalSchematic,
    layout_builder: IoLayoutBuilder,
    simulation_options: SimulationOptions,
    state_mode: StateMode,
    /// Store input regions for validation
    input_regions: HashMap<String, Vec<(i32, i32, i32)>>,
    /// Store output regions for validation
    output_regions: HashMap<String, Vec<(i32, i32, i32)>>,
}

impl CircuitBuilder {
    /// Create a new CircuitBuilder from a schematic
    pub fn new(schematic: UniversalSchematic) -> Self {
        Self {
            schematic,
            layout_builder: IoLayoutBuilder::new(),
            simulation_options: SimulationOptions::default(),
            state_mode: StateMode::Stateless,
            input_regions: HashMap::new(),
            output_regions: HashMap::new(),
        }
    }

    /// Create a CircuitBuilder pre-populated from Insign annotations in the schematic
    ///
    /// This parses Insign DSL annotations from signs in the schematic and
    /// pre-populates the IO layout.
    pub fn from_insign(schematic: UniversalSchematic) -> Result<Self, String> {
        use crate::insign;
        use crate::simulation::typed_executor::insign_io;

        // Extract signs from schematic
        let signs = insign::extract_signs(&schematic);
        let input: Vec<([i32; 3], String)> = signs.into_iter().map(|s| (s.pos, s.text)).collect();

        // Parse IO layout using insign_io
        let layout_builder = insign_io::parse_io_layout_from_insign(&input, &schematic)
            .map_err(|e| e.to_string())?;

        // Strip the annotation signs before the schematic reaches MCHPRS —
        // sign blocks are not simulable and panic redpiler's block mapping
        // (same treatment as create_executor_from_insign).
        let mut schematic = schematic;
        let air_block = crate::BlockState::new("minecraft:air");
        for (pos, _) in &input {
            schematic.set_block(pos[0], pos[1], pos[2], &air_block);
        }

        // Extract regions from the layout for validation
        let layout = layout_builder.build();
        let mut input_regions = HashMap::new();
        let mut output_regions = HashMap::new();

        for (name, mapping) in &layout.inputs {
            input_regions.insert(name.clone(), mapping.positions.clone());
        }
        for (name, mapping) in &layout.outputs {
            output_regions.insert(name.clone(), mapping.positions.clone());
        }

        // Rebuild the layout builder with the same data
        let mut new_builder = IoLayoutBuilder::new();
        for (name, mapping) in layout.inputs {
            new_builder =
                new_builder.add_input(name, mapping.io_type, mapping.layout, mapping.positions)?;
        }
        for (name, mapping) in layout.outputs {
            new_builder =
                new_builder.add_output(name, mapping.io_type, mapping.layout, mapping.positions)?;
        }

        Ok(Self {
            schematic,
            layout_builder: new_builder,
            simulation_options: SimulationOptions::default(),
            state_mode: StateMode::Stateless,
            input_regions,
            output_regions,
        })
    }

    /// Add an input with full control over type, layout, and region
    ///
    /// Uses the default sort strategy (YXZ - Y first, then X, then Z).
    /// For custom ordering, use `with_input_sorted`.
    pub fn with_input(
        self,
        name: impl Into<String>,
        io_type: IoType,
        layout: LayoutFunction,
        region: DefinitionRegion,
    ) -> Result<Self, String> {
        self.with_input_sorted(name, io_type, layout, region, SortStrategy::default())
    }

    /// Add an input with full control and custom sort strategy
    pub fn with_input_sorted(
        mut self,
        name: impl Into<String>,
        io_type: IoType,
        layout: LayoutFunction,
        region: DefinitionRegion,
        sort: SortStrategy,
    ) -> Result<Self, String> {
        let name = name.into();
        let raw_positions: Vec<_> = region.iter_positions().collect();
        let positions = sort.sort(&raw_positions);

        if positions.is_empty() {
            return Err(format!("Input '{}' has no positions", name));
        }

        self.input_regions.insert(name.clone(), positions.clone());
        self.layout_builder = self
            .layout_builder
            .add_input(name, io_type, layout, positions)?;
        Ok(self)
    }

    /// Add an input with automatic layout inference
    ///
    /// Uses the default sort strategy (YXZ - Y first, then X, then Z).
    /// For custom ordering, use `with_input_auto_sorted`.
    pub fn with_input_auto(
        self,
        name: impl Into<String>,
        io_type: IoType,
        region: DefinitionRegion,
    ) -> Result<Self, String> {
        self.with_input_auto_sorted(name, io_type, region, SortStrategy::default())
    }

    /// Add an input with automatic layout inference and custom sort strategy
    pub fn with_input_auto_sorted(
        mut self,
        name: impl Into<String>,
        io_type: IoType,
        region: DefinitionRegion,
        sort: SortStrategy,
    ) -> Result<Self, String> {
        let name = name.into();
        let raw_positions: Vec<_> = region.iter_positions().collect();
        let positions = sort.sort(&raw_positions);

        if positions.is_empty() {
            return Err(format!("Input '{}' has no positions", name));
        }

        self.input_regions.insert(name.clone(), positions.clone());
        self.layout_builder = self
            .layout_builder
            .add_input_auto(name, io_type, positions)?;
        Ok(self)
    }

    /// Add an output with full control over type, layout, and region
    ///
    /// Uses the default sort strategy (YXZ - Y first, then X, then Z).
    /// For custom ordering, use `with_output_sorted`.
    pub fn with_output(
        self,
        name: impl Into<String>,
        io_type: IoType,
        layout: LayoutFunction,
        region: DefinitionRegion,
    ) -> Result<Self, String> {
        self.with_output_sorted(name, io_type, layout, region, SortStrategy::default())
    }

    /// Add an output with full control and custom sort strategy
    pub fn with_output_sorted(
        mut self,
        name: impl Into<String>,
        io_type: IoType,
        layout: LayoutFunction,
        region: DefinitionRegion,
        sort: SortStrategy,
    ) -> Result<Self, String> {
        let name = name.into();
        let raw_positions: Vec<_> = region.iter_positions().collect();
        let positions = sort.sort(&raw_positions);

        if positions.is_empty() {
            return Err(format!("Output '{}' has no positions", name));
        }

        self.output_regions.insert(name.clone(), positions.clone());
        self.layout_builder = self
            .layout_builder
            .add_output(name, io_type, layout, positions)?;
        Ok(self)
    }

    /// Add an output with automatic layout inference
    ///
    /// Uses the default sort strategy (YXZ - Y first, then X, then Z).
    /// For custom ordering, use `with_output_auto_sorted`.
    pub fn with_output_auto(
        self,
        name: impl Into<String>,
        io_type: IoType,
        region: DefinitionRegion,
    ) -> Result<Self, String> {
        self.with_output_auto_sorted(name, io_type, region, SortStrategy::default())
    }

    /// Add an output with automatic layout inference and custom sort strategy
    pub fn with_output_auto_sorted(
        mut self,
        name: impl Into<String>,
        io_type: IoType,
        region: DefinitionRegion,
        sort: SortStrategy,
    ) -> Result<Self, String> {
        let name = name.into();
        let raw_positions: Vec<_> = region.iter_positions().collect();
        let positions = sort.sort(&raw_positions);

        if positions.is_empty() {
            return Err(format!("Output '{}' has no positions", name));
        }

        self.output_regions.insert(name.clone(), positions.clone());
        self.layout_builder = self
            .layout_builder
            .add_output_auto(name, io_type, positions)?;
        Ok(self)
    }

    /// Set simulation options
    pub fn with_options(mut self, options: SimulationOptions) -> Self {
        self.simulation_options = options;
        self
    }

    /// Set state mode
    pub fn with_state_mode(mut self, mode: StateMode) -> Self {
        self.state_mode = mode;
        self
    }

    /// Validate the circuit configuration
    ///
    /// Checks for:
    /// - Overlapping input/output regions
    /// - Empty inputs/outputs
    /// - Invalid configurations
    pub fn validate(&self) -> Result<&Self, ValidationError> {
        // Check for empty inputs
        if self.input_regions.is_empty() {
            return Err(ValidationError::NoInputs);
        }

        // Check for empty outputs
        if self.output_regions.is_empty() {
            return Err(ValidationError::NoOutputs);
        }

        // Check for empty regions
        for (name, positions) in &self.input_regions {
            if positions.is_empty() {
                return Err(ValidationError::EmptyInput(name.clone()));
            }
        }

        for (name, positions) in &self.output_regions {
            if positions.is_empty() {
                return Err(ValidationError::EmptyOutput(name.clone()));
            }
        }

        // Check for overlaps between all regions
        let all_regions: Vec<(&str, &Vec<(i32, i32, i32)>)> = self
            .input_regions
            .iter()
            .map(|(n, p)| (n.as_str(), p))
            .chain(self.output_regions.iter().map(|(n, p)| (n.as_str(), p)))
            .collect();

        for i in 0..all_regions.len() {
            for j in (i + 1)..all_regions.len() {
                let (name1, positions1) = all_regions[i];
                let (name2, positions2) = all_regions[j];

                let set1: std::collections::HashSet<_> = positions1.iter().collect();
                let overlap_count = positions2.iter().filter(|p| set1.contains(p)).count();

                if overlap_count > 0 {
                    return Err(ValidationError::OverlappingRegions {
                        name1: name1.to_string(),
                        name2: name2.to_string(),
                        overlap_count,
                    });
                }
            }
        }

        Ok(self)
    }

    /// Build the TypedCircuitExecutor
    pub fn build(self) -> Result<TypedCircuitExecutor, String> {
        let layout = self.layout_builder.build();

        // Register all IO positions as custom IO nodes so the compiler keeps
        // them addressable from the very first execution. Without this the
        // world only gained custom IO after the first reset(), so manual or
        // stateful use of a fresh executor could not drive its inputs.
        let mut options = self.simulation_options;
        for mapping in layout.inputs.values().chain(layout.outputs.values()) {
            for &(x, y, z) in &mapping.positions {
                let pos = crate::simulation::BlockPos::new(x, y, z);
                if !options.custom_io.contains(&pos) {
                    options.custom_io.push(pos);
                }
            }
        }

        let world = MchprsWorld::with_options(self.schematic, options.clone())?;

        let mut executor = TypedCircuitExecutor::from_layout_with_options(world, layout, options);
        executor.set_state_mode(self.state_mode);

        Ok(executor)
    }

    /// Build with validation (convenience method)
    pub fn build_validated(self) -> Result<TypedCircuitExecutor, String> {
        self.validate().map_err(|e| e.to_string())?;
        self.build()
    }

    /// Get the current number of inputs
    pub fn input_count(&self) -> usize {
        self.input_regions.len()
    }

    /// Get the current number of outputs
    pub fn output_count(&self) -> usize {
        self.output_regions.len()
    }

    /// Get the names of defined inputs
    pub fn input_names(&self) -> Vec<&str> {
        self.input_regions.keys().map(|s| s.as_str()).collect()
    }

    /// Get the names of defined outputs
    pub fn output_names(&self) -> Vec<&str> {
        self.output_regions.keys().map(|s| s.as_str()).collect()
    }
}

/// Create an executor from Insign annotations (convenience function)
pub fn create_circuit_from_insign(
    schematic: &UniversalSchematic,
) -> Result<TypedCircuitExecutor, String> {
    use crate::simulation::typed_executor::insign_io;
    insign_io::create_executor_from_insign(schematic).map_err(|e| e.to_string())
}

/// Create an executor from Insign annotations with options (convenience function)
pub fn create_circuit_from_insign_with_options(
    schematic: &UniversalSchematic,
    options: SimulationOptions,
) -> Result<TypedCircuitExecutor, String> {
    use crate::simulation::typed_executor::insign_io;
    insign_io::create_executor_from_insign_with_options(schematic, options)
        .map_err(|e| e.to_string())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::BlockState;

    fn create_test_schematic() -> UniversalSchematic {
        let mut schematic = UniversalSchematic::new("test".to_string());
        // Add some blocks so the world isn't empty
        for x in 0..10 {
            schematic.set_block(x, 0, 0, &BlockState::new("minecraft:stone".to_string()));
        }
        schematic
    }

    #[test]
    fn test_circuit_builder_creation() {
        let schematic = create_test_schematic();
        let builder = CircuitBuilder::new(schematic);

        assert_eq!(builder.input_count(), 0);
        assert_eq!(builder.output_count(), 0);
    }

    #[test]
    fn test_circuit_builder_validation_no_inputs() {
        let schematic = create_test_schematic();
        let builder = CircuitBuilder::new(schematic);

        let result = builder.validate();
        assert!(matches!(result, Err(ValidationError::NoInputs)));
    }
}