nucleation 0.10.13

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
483
484
485
486
487
488
//! Insign DSL extensions for the IO contract layer.
//!
//! Insign's grammar is `@geometry` + `#[target:]key=<json>` metadata (see the
//! `insign` crate). These parsers add the cell-authoring vocabulary on top of
//! that grammar — no new syntax, only new well-known keys:
//!
//! **Cell header** (global metadata):
//! ```text
//! #$global:cell.name="full_adder"
//! #$global:cell.version="1.2.0"
//! ```
//!
//! **Bus annotation** — one sign annotates a whole bus on an `io.*` region:
//! ```text
//! @io.a=rc([0,0,0],[0,0,6])
//! #type="input"
//! #data_type="unsigned"
//! #bus.width=4
//! #bus.bit_order="lsb_first"
//! #bus.face="east"
//! #bus.encoding="binary"      // optional, default binary
//! ```
//!
//! **Route zones** — sign a zone (`#route_zone="<name> include|exclude"`,
//! or the expanded `#route_zone.name=` / `#route_zone.mode=` pair):
//! ```text
//! @rc([0,64,0],[31,66,3])
//! #route_zone="bus_north include"
//! ```

use super::bus::BusEncoding;
use super::physical::Face;
use super::routing::{RouteZoneMode, RoutingRegion};
use crate::bounding_box::BoundingBox;
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::collections::HashMap;

/// Errors from IO-contract Insign parsing.
#[derive(Debug, thiserror::Error)]
pub enum InsignContractError {
    #[error("Insign compilation error: {0}")]
    Compile(String),

    #[error("Invalid value for '{key}' on region '{region}': {reason}")]
    InvalidValue {
        region: String,
        key: String,
        reason: String,
    },

    #[error("Missing required key '{key}' on region '{region}'")]
    MissingKey { region: String, key: String },
}

/// `#cell` header: name and optional version of the cell being authored.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct CellHeader {
    pub name: String,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub version: Option<String>,
}

/// Bit order of a bus annotation relative to position order.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BitOrder {
    #[default]
    LsbFirst,
    MsbFirst,
}

impl BitOrder {
    pub fn parse(s: &str) -> Option<Self> {
        match s.to_ascii_lowercase().as_str() {
            "lsb_first" | "lsb" | "little" => Some(BitOrder::LsbFirst),
            "msb_first" | "msb" | "big" => Some(BitOrder::MsbFirst),
            _ => None,
        }
    }
}

/// A whole-bus annotation attached to an `io.<name>` region.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BusAnnotation {
    /// Port name (the `io.` prefix stripped).
    pub name: String,
    pub width: u8,
    #[serde(default)]
    pub bit_order: BitOrder,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub face: Option<Face>,
    pub encoding: BusEncoding,
}

fn compile(input: &[([i32; 3], String)]) -> Result<::insign::DslMap, InsignContractError> {
    ::insign::compile(input).map_err(|e| InsignContractError::Compile(e.to_string()))
}

fn meta_str<'a>(
    metadata: &'a std::collections::BTreeMap<String, JsonValue>,
    key: &str,
) -> Option<&'a str> {
    metadata.get(key).and_then(|v| v.as_str())
}

/// Parse the `#cell` header from global metadata (`cell.name`,
/// `cell.version`). Returns `Ok(None)` when no header is present.
pub fn parse_cell_header(
    input: &[([i32; 3], String)],
) -> Result<Option<CellHeader>, InsignContractError> {
    let map = compile(input)?;
    parse_cell_header_from_map(&map)
}

/// Same as [`parse_cell_header`] but over an already-compiled map.
pub fn parse_cell_header_from_map(
    map: &::insign::DslMap,
) -> Result<Option<CellHeader>, InsignContractError> {
    let Some(global) = map.get("$global") else {
        return Ok(None);
    };
    let Some(name_val) = global.metadata.get("cell.name") else {
        // A version without a name is an authoring mistake worth flagging.
        if global.metadata.contains_key("cell.version") {
            return Err(InsignContractError::MissingKey {
                region: "$global".to_string(),
                key: "cell.name".to_string(),
            });
        }
        return Ok(None);
    };
    let name = name_val
        .as_str()
        .ok_or_else(|| InsignContractError::InvalidValue {
            region: "$global".to_string(),
            key: "cell.name".to_string(),
            reason: format!("expected string, got {}", name_val),
        })?
        .to_string();
    let version = match global.metadata.get("cell.version") {
        None => None,
        Some(v) => Some(
            v.as_str()
                .ok_or_else(|| InsignContractError::InvalidValue {
                    region: "$global".to_string(),
                    key: "cell.version".to_string(),
                    reason: format!("expected string, got {}", v),
                })?
                .to_string(),
        ),
    };
    Ok(Some(CellHeader { name, version }))
}

/// Parse bus annotations from `io.*` regions carrying `bus.*` metadata.
pub fn parse_bus_annotations(
    input: &[([i32; 3], String)],
) -> Result<Vec<BusAnnotation>, InsignContractError> {
    let map = compile(input)?;
    parse_bus_annotations_from_map(&map)
}

/// Same as [`parse_bus_annotations`] but over an already-compiled map.
pub fn parse_bus_annotations_from_map(
    map: &::insign::DslMap,
) -> Result<Vec<BusAnnotation>, InsignContractError> {
    let mut buses = Vec::new();
    for (region_name, entry) in map.iter() {
        let Some(port_name) = region_name.strip_prefix("io.") else {
            continue;
        };
        let Some(width_val) = entry.metadata.get("bus.width") else {
            continue; // not a bus-annotated port
        };
        let width = width_val
            .as_u64()
            .filter(|w| (1..=255).contains(w))
            .ok_or_else(|| InsignContractError::InvalidValue {
                region: region_name.clone(),
                key: "bus.width".to_string(),
                reason: format!("expected integer 1-255, got {}", width_val),
            })? as u8;

        let bit_order = match meta_str(&entry.metadata, "bus.bit_order") {
            None => BitOrder::default(),
            Some(s) => BitOrder::parse(s).ok_or_else(|| InsignContractError::InvalidValue {
                region: region_name.clone(),
                key: "bus.bit_order".to_string(),
                reason: format!("expected lsb_first|msb_first, got '{}'", s),
            })?,
        };

        let face = match meta_str(&entry.metadata, "bus.face") {
            None => None,
            Some(s) => Some(
                Face::parse(s).ok_or_else(|| InsignContractError::InvalidValue {
                    region: region_name.clone(),
                    key: "bus.face".to_string(),
                    reason: format!("expected a direction name, got '{}'", s),
                })?,
            ),
        };

        let encoding = match meta_str(&entry.metadata, "bus.encoding") {
            None => BusEncoding::Binary1PerWire,
            Some(s) => BusEncoding::parse(s).ok_or_else(|| InsignContractError::InvalidValue {
                region: region_name.clone(),
                key: "bus.encoding".to_string(),
                reason: format!("expected binary|hex_analog, got '{}'", s),
            })?,
        };

        buses.push(BusAnnotation {
            name: port_name.to_string(),
            width,
            bit_order,
            face,
            encoding,
        });
    }
    Ok(buses)
}

/// Parse `#route_zone` annotations into named [`RoutingRegion`]s.
///
/// Accepted forms on any region with geometry:
/// - compact: `#route_zone="<name> include"` / `#route_zone="<name> exclude"`
///   (mode defaults to `include` when only a name is given)
/// - expanded: `#route_zone.name="<name>"` + `#route_zone.mode="include"`
pub fn parse_route_zones(
    input: &[([i32; 3], String)],
) -> Result<HashMap<String, RoutingRegion>, InsignContractError> {
    let map = compile(input)?;
    parse_route_zones_from_map(&map)
}

/// Same as [`parse_route_zones`] but over an already-compiled map.
pub fn parse_route_zones_from_map(
    map: &::insign::DslMap,
) -> Result<HashMap<String, RoutingRegion>, InsignContractError> {
    let mut zones: HashMap<String, RoutingRegion> = HashMap::new();
    for (region_name, entry) in map.iter() {
        let tag = if let Some(compact) = entry.metadata.get("route_zone") {
            let s = compact
                .as_str()
                .ok_or_else(|| InsignContractError::InvalidValue {
                    region: region_name.clone(),
                    key: "route_zone".to_string(),
                    reason: format!("expected \"<name> include|exclude\", got {}", compact),
                })?;
            let mut parts = s.split_whitespace();
            let name = parts.next().filter(|n| !n.is_empty()).ok_or_else(|| {
                InsignContractError::InvalidValue {
                    region: region_name.clone(),
                    key: "route_zone".to_string(),
                    reason: "missing zone name".to_string(),
                }
            })?;
            let mode = match parts.next() {
                None => RouteZoneMode::Include,
                Some(m) => {
                    RouteZoneMode::parse(m).ok_or_else(|| InsignContractError::InvalidValue {
                        region: region_name.clone(),
                        key: "route_zone".to_string(),
                        reason: format!("expected include|exclude, got '{}'", m),
                    })?
                }
            };
            Some((name.to_string(), mode))
        } else if let Some(name) = meta_str(&entry.metadata, "route_zone.name") {
            let mode = match meta_str(&entry.metadata, "route_zone.mode") {
                None => RouteZoneMode::Include,
                Some(m) => {
                    RouteZoneMode::parse(m).ok_or_else(|| InsignContractError::InvalidValue {
                        region: region_name.clone(),
                        key: "route_zone.mode".to_string(),
                        reason: format!("expected include|exclude, got '{}'", m),
                    })?
                }
            };
            Some((name.to_string(), mode))
        } else {
            None
        };

        let Some((name, mode)) = tag else { continue };
        let Some(boxes) = entry.bounding_boxes.as_ref() else {
            return Err(InsignContractError::MissingKey {
                region: region_name.clone(),
                key: "geometry (@rc/@ac)".to_string(),
            });
        };
        let zone = zones
            .entry(name.clone())
            .or_insert_with(|| RoutingRegion::new(name));
        let target = match mode {
            RouteZoneMode::Include => &mut zone.include,
            RouteZoneMode::Exclude => &mut zone.exclude,
        };
        for (min, max) in boxes {
            target.push(BoundingBox {
                min: (min[0], min[1], min[2]),
                max: (max[0], max[1], max[2]),
            });
        }
    }
    Ok(zones)
}

/// All IO-contract annotations of a sign set, as one JSON value:
/// `{ "cell": ..., "buses": [...], "route_zones": {...} }`.
///
/// This is the bridge-facing entry point.
pub fn contracts_json(input: &[([i32; 3], String)]) -> Result<JsonValue, InsignContractError> {
    let map = compile(input)?;
    let cell = parse_cell_header_from_map(&map)?;
    let buses = parse_bus_annotations_from_map(&map)?;
    let zones = parse_route_zones_from_map(&map)?;
    Ok(serde_json::json!({
        "cell": cell,
        "buses": buses,
        "route_zones": zones,
    }))
}

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

    fn signs(texts: &[&str]) -> Vec<([i32; 3], String)> {
        texts
            .iter()
            .enumerate()
            .map(|(i, t)| ([i as i32 * 16, 64, 0], t.to_string()))
            .collect()
    }

    #[test]
    fn cell_header_parses_name_and_version() {
        let input = signs(&["#$global:cell.name=\"full_adder\"\n#$global:cell.version=\"1.2.0\""]);
        let header = parse_cell_header(&input).unwrap().unwrap();
        assert_eq!(header.name, "full_adder");
        assert_eq!(header.version.as_deref(), Some("1.2.0"));
    }

    #[test]
    fn cell_header_absent_is_none() {
        let input = signs(&["@rc([0,0,0],[1,1,1])\n#doc.label=\"x\""]);
        assert_eq!(parse_cell_header(&input).unwrap(), None);
    }

    #[test]
    fn cell_header_version_without_name_is_error() {
        let input = signs(&["#$global:cell.version=\"1.0\""]);
        assert!(matches!(
            parse_cell_header(&input),
            Err(InsignContractError::MissingKey { .. })
        ));
    }

    #[test]
    fn cell_header_non_string_name_is_error() {
        let input = signs(&["#$global:cell.name=42"]);
        assert!(matches!(
            parse_cell_header(&input),
            Err(InsignContractError::InvalidValue { .. })
        ));
    }

    #[test]
    fn bus_annotation_full_form() {
        let input = signs(&[concat!(
            "@io.a=rc([0,0,0],[0,0,6])\n",
            "#type=\"input\"\n#data_type=\"unsigned\"\n",
            "#bus.width=4\n#bus.bit_order=\"msb_first\"\n",
            "#bus.face=\"east\"\n#bus.encoding=\"hex_analog\""
        )]);
        let buses = parse_bus_annotations(&input).unwrap();
        assert_eq!(buses.len(), 1);
        let b = &buses[0];
        assert_eq!(b.name, "a");
        assert_eq!(b.width, 4);
        assert_eq!(b.bit_order, BitOrder::MsbFirst);
        assert_eq!(b.face, Some(Face::East));
        assert_eq!(b.encoding, BusEncoding::HexAnalog);
    }

    #[test]
    fn bus_annotation_defaults() {
        let input = signs(&["@io.d=rc([0,0,0],[7,0,0])\n#bus.width=8"]);
        let buses = parse_bus_annotations(&input).unwrap();
        let b = &buses[0];
        assert_eq!(b.bit_order, BitOrder::LsbFirst);
        assert_eq!(b.face, None);
        assert_eq!(b.encoding, BusEncoding::Binary1PerWire);
    }

    #[test]
    fn non_bus_io_region_is_skipped() {
        let input = signs(&["@io.clk=rc([0,0,0],[0,0,0])\n#type=\"input\"\n#data_type=\"bool\""]);
        assert!(parse_bus_annotations(&input).unwrap().is_empty());
    }

    #[test]
    fn bus_width_out_of_range_is_error() {
        let input = signs(&["@io.a=rc([0,0,0],[0,0,6])\n#bus.width=0"]);
        assert!(matches!(
            parse_bus_annotations(&input),
            Err(InsignContractError::InvalidValue { .. })
        ));
        let input = signs(&["@io.a=rc([0,0,0],[0,0,6])\n#bus.width=\"four\""]);
        assert!(parse_bus_annotations(&input).is_err());
    }

    #[test]
    fn route_zone_compact_form() {
        let input = signs(&[
            "@rc([0,0,0],[31,2,3])\n#route_zone=\"bus_north include\"",
            "@rc([10,0,1],[12,2,2])\n#route_zone=\"bus_north exclude\"",
        ]);
        let zones = parse_route_zones(&input).unwrap();
        assert_eq!(zones.len(), 1);
        let z = &zones["bus_north"];
        assert_eq!(z.include.len(), 1);
        assert_eq!(z.exclude.len(), 1);
        // sign 0 at [0,64,0]: include box spans [0,64,0]..[31,66,3]
        assert!(z.contains(0, 64, 0));
        // sign 1 at [16,64,0]: exclude box spans [26,64,1]..[28,66,2]
        assert!(!z.contains(27, 65, 1));
        assert!(!z.contains(40, 64, 0)); // outside include
    }

    #[test]
    fn route_zone_expanded_form_and_named_regions() {
        let input = signs(&[concat!(
            "@zones.n=rc([0,0,0],[7,0,7])\n",
            "#zones.n:route_zone.name=\"north\"\n",
            "#zones.n:route_zone.mode=\"exclude\""
        )]);
        let zones = parse_route_zones(&input).unwrap();
        let z = &zones["north"];
        assert!(z.include.is_empty());
        assert_eq!(z.exclude.len(), 1);
    }

    #[test]
    fn route_zone_mode_defaults_to_include() {
        let input = signs(&["@rc([0,0,0],[1,1,1])\n#route_zone=\"lane\""]);
        let zones = parse_route_zones(&input).unwrap();
        assert_eq!(zones["lane"].include.len(), 1);
    }

    #[test]
    fn route_zone_bad_mode_is_error() {
        let input = signs(&["@rc([0,0,0],[1,1,1])\n#route_zone=\"lane sideways\""]);
        assert!(matches!(
            parse_route_zones(&input),
            Err(InsignContractError::InvalidValue { .. })
        ));
    }

    #[test]
    fn route_zone_without_geometry_is_error() {
        // metadata attached to a named region that never gets geometry
        let input = signs(&["#lane.a:route_zone=\"lane include\""]);
        let res = parse_route_zones(&input);
        // insign itself may reject metadata on undefined regions; either way
        // we must not silently produce a zone with no boxes
        match res {
            Ok(zones) => assert!(zones.is_empty() || !zones.contains_key("lane")),
            Err(_) => {}
        }
    }

    #[test]
    fn contracts_json_shape() {
        let input = signs(&[
            "#$global:cell.name=\"cell_x\"",
            "@io.a=rc([0,0,0],[3,0,0])\n#bus.width=4",
            "@rc([0,0,0],[9,2,9])\n#route_zone=\"main include\"",
        ]);
        let json = contracts_json(&input).unwrap();
        assert_eq!(json["cell"]["name"], "cell_x");
        assert_eq!(json["buses"][0]["name"], "a");
        assert!(json["route_zones"]["main"]["include"].is_array());
    }
}