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
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
//! schematic parsing
use std::collections::hash_map::Entry;
use std::collections::HashMap;
use std::fmt::{self, Write};
use thiserror::Error;

use crate::block::ratios::{Io, IoBuilder};
use crate::block::{self, Block, Rotation, State, BLOCK_REGISTRY};
use crate::data::base64;
use crate::data::dynamic::{self, DynData};
use crate::data::renderer::*;
use crate::data::{self, DataRead, DataWrite, GridPos, Serializable};
use crate::item::storage::ItemStorage;
use crate::utils::array::Array2D;

/// biggest schematic
pub const MAX_DIMENSION: usize = 1024;
/// most possible blocks
pub const MAX_BLOCKS: u32 = 1024 * 1024;

/// a placement in a schematic
#[derive(Clone)]
pub struct Placement {
    pub block: &'static Block,
    pub rot: Rotation,
    state: Option<State>,
}

impl PartialEq for Placement {
    fn eq(&self, rhs: &Placement) -> bool {
        self.block == rhs.block && self.rot == rhs.rot
    }
}

impl fmt::Debug for Placement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        write!(f, "P<{}[*{}]>", self.block.name(), self.rot.ch())
    }
}

impl Placement {
    /// make a placement from a block
    #[must_use]
    pub const fn new(block: &'static Block) -> Self {
        Self {
            block,
            rot: Rotation::Up,
            state: None,
        }
    }

    /// gets the current state of this placement. you can cast it with `placement.block::get_state(placement.get_state()?)?`
    #[must_use]
    pub const fn get_state(&self) -> Option<&State> {
        self.state.as_ref()
    }

    /// get mutable state.
    pub fn get_state_mut(&mut self) -> Option<&mut State> {
        self.state.as_mut()
    }

    /// draws this placement in particular
    #[must_use]
    pub fn image(
        &self,
        context: Option<&RenderingContext>,
        rot: Rotation,
        s: Scale,
    ) -> ImageHolder<4> {
        self.block.image(self.get_state(), context, rot, s)
    }

    /// set the state
    pub fn set_state(&mut self, data: DynData) -> Result<Option<State>, block::DeserializeError> {
        let state = self.block.deserialize_state(data)?;
        Ok(std::mem::replace(&mut self.state, state))
    }

    /// rotate this
    pub fn set_rotation(&mut self, rot: Rotation) -> Rotation {
        std::mem::replace(&mut self.rot, rot)
    }
}

impl BlockState for Placement {
    fn get_block(&self) -> Option<&'static Block> {
        Some(self.block)
    }
}

impl RotationState for Placement {
    fn get_rotation(&self) -> Option<Rotation> {
        Some(self.rot)
    }
}

impl BlockState for Option<Placement> {
    fn get_block(&self) -> Option<&'static Block> {
        let Some(p) = self else {
            return None;
        };
        Some(p.block)
    }
}

impl RotationState for Option<Placement> {
    fn get_rotation(&self) -> Option<Rotation> {
        let Some(p) = self else {
            return None;
        };
        Some(p.rot)
    }
}

#[derive(Clone, Debug)]
/// a schematic.
pub struct Schematic {
    pub width: usize,
    pub height: usize,
    pub tags: HashMap<String, String>,
    /// schems can have holes, so [Option] is used.
    pub blocks: Array2D<Option<Placement>>,
}

impl PartialEq for Schematic {
    fn eq(&self, rhs: &Schematic) -> bool {
        self.width == rhs.width
            && self.height == rhs.height
            && self.blocks == rhs.blocks
            && self.tags == rhs.tags
    }
}

impl Schematic {
    #[must_use]
    /// create a new schematic, panicking if too big
    /// ```
    /// # use mindus::Schematic;
    /// let s = Schematic::new(5, 5);
    /// ```
    pub fn new(width: usize, height: usize) -> Self {
        match Self::try_new(width, height) {
            Ok(s) => s,
            Err(NewError::Width(w)) => panic!("invalid schematic width ({w})"),
            Err(NewError::Height(h)) => panic!("invalid schematic height ({h})"),
        }
    }

    /// the area around a point
    pub(crate) fn cross(&self, c: &PositionContext) -> Cross {
        let get = |x, y| {
            let b = self.get(x?, y?).ok()??;
            Some((b.get_block()?, b.get_rotation()?))
        };
        macro_rules! s {
            ($x:expr) => {
                Some($x)
            };
            ($a:expr => $b:expr) => {
                if $a < $b {
                    None
                } else {
                    Some($a - $b)
                }
            };
        }
        [
            get(s!(c.position.0), s!(c.position.1 + 1)),
            get(s!(c.position.0 + 1), s!(c.position.1)),
            get(s!(c.position.0), s!(c.position.1 => 1)),
            get(s!(c.position.0 => 1), s!(c.position.1)),
        ]
    }

    /// Ratios of this schematic.
    /// ```
    /// # use mindus::Schematic;
    /// # use mindus::block::ratios::ratios;
    /// assert_eq!(Schematic::deserialize_base64("bXNjaAF4nEWMSw7CMAxEh9REVSqx5hKcCLFISxaR0o9Sg8rtSTpFePPkmWfDwTWQyY8B5z75VdE9wzrkuGicJwA2+T6kFeb+aNDtym2MW8i4LJ/sNWo4dje8ksa31zmXuyv+Y4BTgRD2iIi9M+xM7WrUgnoNhYpQESpCxfKLrUo9FsISLX6vKgwhhCVK+wX5/BtM").unwrap().ratios(),
    ///     ratios![[Coal: 5.25, Lead: 10.5, Sand: 10.5, Water: 180] => [BlastCompound: 5.25, SporePod: 0.75]]);
    /// ```
    pub fn ratios(&self) -> Io {
        let mut io = IoBuilder::default();
        for p in self.blocks.iter().filter_map(|o| o.as_ref()) {
            io += p.block.io(p.state.as_ref());
        }
        io.into()
    }

    /// create a new schematic, erroring if too big
    /// ```
    /// # use mindus::Schematic;
    /// assert!(Schematic::try_new(500, 500).is_err() == true);
    /// ```
    pub fn try_new(width: usize, height: usize) -> Result<Self, NewError> {
        if width > MAX_DIMENSION {
            return Err(NewError::Width(width));
        }
        if height > MAX_DIMENSION {
            return Err(NewError::Height(height));
        }
        let mut tags = HashMap::<String, String>::new();
        tags.insert("name".to_string(), String::new());
        tags.insert("description".to_string(), String::new());
        tags.insert("labels".to_string(), "[]".to_string());
        Ok(Self {
            width,
            height,
            tags,
            blocks: Array2D::new(None, width, height),
        })
    }

    // #[must_use]
    // /// check if a rect is empty
    // /// ```
    // /// # use mindus::Schematic;
    // /// # use mindus::block::distribution::ROUTER;
    // /// let mut s = Schematic::new(5, 5);
    // /// s.put(0, 0, &ROUTER);
    // /// assert!(s.is_region_empty(1, 1, 4, 4));
    // /// s.put(2, 2, &ROUTER);
    // /// assert!(s.is_region_empty(1, 1, 4, 4) == false);
    // /// // out of bounds is empty
    // /// assert!(s.is_region_empty(25, 25, 0, 0));
    // /// ```
    // pub fn is_region_empty(&self, x: usize, y: usize, w: usize, h: usize) -> bool {
    //     if x >= self.width || y >= self.height || w == 0 || h == 0 {
    //         return true;
    //     }
    //     if w > 1 || h > 1 {
    //         for y in y..(y + h).min(self.height) {
    //             for x in x..(x + w).min(self.width) {
    //                 if self.get(x, y).unwrap().is_some() {
    //                     return false;
    //                 }
    //             }
    //         }
    //         true
    //     } else {
    //         self.get(x, y).unwrap().is_none()
    //     }
    // }

    /// gets a block
    /// ```
    /// # use mindus::Schematic;
    /// # use mindus::block::Rotation;
    /// # use mindus::block::DUO;
    ///
    /// let mut s = Schematic::new(5, 5);
    /// assert!(s.get(0, 0).unwrap().is_none());
    /// s.put(0, 0, &DUO);
    /// assert!(s.get(0, 0).unwrap().is_some());
    /// ```
    pub fn get(&self, x: usize, y: usize) -> Result<Option<&Placement>, PosError> {
        if x >= self.width || y >= self.height {
            return Err(PosError {
                x,
                y,
                w: self.width,
                h: self.height,
            });
        }
        Ok(self.blocks[x][y].as_ref())
    }

    /// gets a block, mutably
    pub fn get_mut(&mut self, x: usize, y: usize) -> Result<Option<&mut Placement>, PosError> {
        if x >= self.width || y >= self.height {
            return Err(PosError {
                x,
                y,
                w: self.width,
                h: self.height,
            });
        }
        Ok(self.blocks[x][y].as_mut())
    }

    /// put a block in (same as [`Schematic::set`], but less arguments and builder-ness). panics!!!
    /// ```
    /// # use mindus::Schematic;
    /// # use mindus::block::ROUTER;
    ///
    /// let mut s = Schematic::new(5, 5);
    /// s.put(0, 0, &ROUTER);
    /// assert!(s.get(0, 0).unwrap().is_some() == true);
    /// ```
    pub fn put(&mut self, x: usize, y: usize, block: &'static Block) -> &mut Self {
        self.set(x, y, block, DynData::Empty, Rotation::Up).unwrap();
        self
    }

    /// set a block
    /// ```
    /// # use mindus::Schematic;
    /// # use mindus::data::dynamic::DynData;
    /// # use mindus::block::Rotation;
    /// # use mindus::block::ROUTER;
    ///
    /// let mut s = Schematic::new(5, 5);
    /// s.set(0, 0, &ROUTER, DynData::Empty, Rotation::Right);
    /// assert!(s.get(0, 0).unwrap().is_some() == true);
    /// ```
    pub fn set(
        &mut self,
        x: usize,
        y: usize,
        block: &'static Block,
        data: DynData,
        rot: Rotation,
    ) -> Result<(), PlaceError> {
        let sz = usize::from(block.get_size());
        let off = (sz - 1) / 2;
        if x < off || y < off {
            return Err(PlaceError::Bounds {
                x,
                y,
                sz: block.get_size(),
                w: self.width,
                h: self.height,
            });
        }
        if self.width - x < sz - off || self.height - y < sz - off {
            return Err(PlaceError::Bounds {
                x,
                y,
                sz: block.get_size(),
                w: self.width,
                h: self.height,
            });
        }
        let state = block.deserialize_state(data)?;
        let p = Placement { block, rot, state };
        self.blocks[x][y] = Some(p);
        Ok(())
    }

    /// take out a block
    /// ```
    /// # use mindus::Schematic;
    /// # use mindus::block::DUO;
    /// # use mindus::block::Rotation;
    ///
    /// let mut s = Schematic::new(5, 5);
    /// s.put(0, 0, &DUO);
    /// assert!(s.get(0, 0).unwrap().is_some() == true);
    /// assert!(s.take(0, 0).unwrap().is_some() == true);
    /// assert!(s.get(0, 0).unwrap().is_none() == true);
    /// ```
    pub fn take(&mut self, x: usize, y: usize) -> Result<Option<Placement>, PosError> {
        if x >= self.width || y >= self.height {
            return Err(PosError {
                x,
                y,
                w: self.width,
                h: self.height,
            });
        }
        let b = self.blocks[x][y].take();
        Ok(b)
    }

    /// iterate over all the blocks
    pub fn block_iter(&self) -> impl Iterator<Item = (GridPos, &Placement)> {
        self.blocks.iter().enumerate().filter_map(|(i, p)| {
            let Some(p) = p else {
                return None;
            };
            Some((GridPos(i / self.height, i % self.height), p))
        })
    }

    #[must_use]
    /// see how much this schematic costs.
    /// returns (cost, `is_sandbox`)
    /// ```
    /// # use mindus::Schematic;
    /// # use mindus::block::Rotation;
    /// # use mindus::block::CYCLONE;
    ///
    /// let mut s = Schematic::new(5, 5);
    /// s.put(1, 1, &CYCLONE);
    /// assert_eq!(s.compute_total_cost().0.get_total(), 405);
    /// ```
    pub fn compute_total_cost(&self) -> (ItemStorage, bool) {
        let mut cost = ItemStorage::new();
        let mut sandbox = false;
        for &Placement { block, .. } in self.blocks.iter().filter_map(|b| b.as_ref()) {
            if let Some(curr) = block.get_build_cost() {
                cost.add_all(&curr, u32::MAX);
            } else {
                sandbox = true;
            }
        }
        (cost, sandbox)
    }
}

/// error created by creating a new schematic
#[derive(Copy, Clone, Debug, Eq, PartialEq, Error)]
pub enum NewError {
    #[error("invalid schematic width ({0})")]
    Width(usize),
    #[error("invalid schematic height ({0})")]
    Height(usize),
}
/// error created by doing stuff out of bounds
#[derive(Copy, Clone, Debug, Eq, PartialEq, Error)]
#[error("position {x} / {y} out of bounds {w} / {h}")]
pub struct PosError {
    pub x: usize,
    pub y: usize,
    pub w: usize,
    pub h: usize,
}

#[derive(Debug, Error)]
pub enum PlaceError {
    #[error("invalid block placement {x} / {y} (size {sz}) within {w} / {h}")]
    Bounds {
        x: usize,
        y: usize,
        sz: u8,
        w: usize,
        h: usize,
    },
    #[error("overlapping an existing block at {x} / {y}")]
    Overlap { x: usize, y: usize },
    #[error("block state deserialization failed")]
    Deserialize(#[from] block::DeserializeError),
}

#[derive(Debug, Error)]
pub enum ResizeError {
    #[error("invalid target width ({0})")]
    TargetWidth(u16),
    #[error("invalid target height ({0})")]
    TargetHeight(u16),
    #[error("horizontal offset {dx} not in [-{new_w}, {old_w}]")]
    XOffset { dx: i16, old_w: u16, new_w: u16 },
    #[error("vertical offset {dy} not in [-{new_h}, {old_h}]")]
    YOffset { dy: i16, old_h: u16, new_h: u16 },
    #[error(transparent)]
    Truncated(#[from] TruncatedError),
}

#[derive(Error, Debug)]
pub struct TruncatedError {
    right: u16,
    top: u16,
    left: u16,
    bottom: u16,
}

impl fmt::Display for TruncatedError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        macro_rules! fmt_dir {
            ($f:ident, $first:ident, $name:expr, $value:expr) => {
                if $value != 0 {
                    if $first {
                        f.write_str(" (")?;
                        $first = false;
                    } else {
                        f.write_str(", ")?;
                    }
                    write!(f, "{}: {}", $name, $value)?;
                }
            };
        }

        f.write_str("truncated blocks")?;
        let mut first = true;
        fmt_dir!(f, first, "right", self.right);
        fmt_dir!(f, first, "top", self.top);
        fmt_dir!(f, first, "left", self.left);
        fmt_dir!(f, first, "bottom", self.bottom);
        if !first {
            f.write_char(')')?;
        }
        Ok(())
    }
}

const SCHEMATIC_HEADER: u32 =
    ((b'm' as u32) << 24) | ((b's' as u32) << 16) | ((b'c' as u32) << 8) | (b'h' as u32);

impl Serializable for Schematic {
    type ReadError = ReadError;
    type WriteError = WriteError;
    fn deserialize(buff: &mut DataRead<'_>) -> Result<Schematic, Self::ReadError> {
        let hdr = buff.read_u32()?;
        if hdr != SCHEMATIC_HEADER {
            return Err(ReadError::Header(hdr));
        }
        let version = buff.read_u8()?;
        if version > 1 {
            return Err(ReadError::Version(version));
        }
        let buff = buff.deflate()?;
        let mut buff = DataRead::new(&buff);
        let w = buff.read_i16()? as usize;
        let h = buff.read_i16()? as usize;
        if w > MAX_DIMENSION || h > MAX_DIMENSION {
            return Err(ReadError::Dimensions(w, h));
        }
        let mut schematic = Schematic::new(w, h);
        buff.read_map(&mut schematic.tags)?;
        let num_table = buff.read_i8()?;
        if num_table < 0 {
            return Err(ReadError::TableSize(num_table));
        }
        let mut block_table = Vec::with_capacity(num_table as usize);
        for _ in 0..num_table {
            let name = buff.read_utf()?;
            match BLOCK_REGISTRY.get(name) {
                None => return Err(ReadError::NoSuchBlock(name.to_owned())),
                Some(b) => block_table.push(b),
            }
        }
        let num_blocks = buff.read_i32()?;
        if num_blocks < 0 || num_blocks as u32 > MAX_BLOCKS {
            return Err(ReadError::BlockCount(num_blocks));
        }
        for _ in 0..num_blocks {
            let idx = buff.read_i8()?;
            if idx < 0 || idx as usize >= block_table.len() {
                return Err(ReadError::BlockIndex(idx, block_table.len()));
            }
            let pos = GridPos::from(buff.read_u32()?);
            let block = block_table[idx as usize];
            let config = if version < 1 {
                block.data_from_i32(buff.read_i32()?, pos)?
            } else {
                DynData::deserialize(&mut buff)?
            };
            let rot = Rotation::from(buff.read_u8()?);
            schematic.set(pos.0, pos.1, block, config, rot)?;
        }
        Ok(schematic)
    }

    fn serialize(&self, buff: &mut DataWrite<'_>) -> Result<(), Self::WriteError> {
        // write the header first just in case
        buff.write_u32(SCHEMATIC_HEADER)?;
        buff.write_u8(1)?;

        let mut rbuff = DataWrite::default();
        // don't have to check dimensions because they're already limited to MAX_DIMENSION
        rbuff.write_i16(self.width as i16)?;
        rbuff.write_i16(self.height as i16)?;
        if self.tags.len() > u8::MAX as usize {
            return Err(WriteError::TagCount(self.tags.len()));
        }
        rbuff.write_u8(self.tags.len() as u8)?;
        for (k, v) in &self.tags {
            rbuff.write_utf(k)?;
            rbuff.write_utf(v)?;
        }
        // use string keys here to avoid issues with different block refs with the same name
        let mut block_map = HashMap::new();
        let mut block_table = Vec::new();
        let mut block_count = 0i32;
        for curr in self.blocks.iter().filter_map(|b| b.as_ref()) {
            block_count += 1;
            if let Entry::Vacant(e) = block_map.entry(curr.block.name()) {
                e.insert(block_table.len() as u32);
                block_table.push(curr.block.name());
            }
        }
        if block_table.len() > i8::MAX as usize {
            return Err(WriteError::TableSize(block_table.len()));
        }
        // else: implies contents are also valid i8 (they're strictly less than the map length)
        rbuff.write_i8(block_table.len() as i8)?;
        for &name in &block_table {
            rbuff.write_utf(name)?;
        }
        // don't have to check data.blocks.len() because dimensions don't allow exceeding MAX_BLOCKS
        rbuff.write_i32(block_count)?;
        for (pos, curr) in self.block_iter() {
            rbuff.write_i8(block_map[curr.block.name()] as i8)?;
            rbuff.write_u32(pos.into())?;
            let data = match curr.state {
                None => DynData::Empty,
                Some(ref s) => curr.block.serialize_state(s)?,
            };
            data.serialize(&mut rbuff)?;
            rbuff.write_u8(curr.rot.into())?;
        }
        rbuff.inflate(buff)?;
        Ok(())
    }
}

#[derive(Debug, Error)]
pub enum ReadError {
    #[error("failed to read from buffer")]
    Read(#[from] data::ReadError),
    #[error("incorrect header ({0:08X})")]
    Header(u32),
    #[error("unsupported version ({0})")]
    Version(u8),
    #[error("invalid schematic dimensions ({0} / {1})")]
    Dimensions(usize, usize),
    #[error("invalid block table size ({0})")]
    TableSize(i8),
    #[error("unknown block {0:?}")]
    NoSuchBlock(String),
    #[error("invalid total block count ({0})")]
    BlockCount(i32),
    #[error("invalid block index ({0} / {1})")]
    BlockIndex(i8, usize),
    #[error("block config conversion failed")]
    BlockConfig(#[from] block::DataConvertError),
    #[error("failed to read block data")]
    ReadState(#[from] dynamic::ReadError),
    #[error("deserialized block could not be placed")]
    Placement(#[from] PlaceError),
    #[error(transparent)]
    Decompress(#[from] super::DecompressError),
}

#[derive(Debug, Error)]
pub enum WriteError {
    #[error("failed to write data to buffer")]
    Write(#[from] data::WriteError),
    #[error("tag list too long ({0})")]
    TagCount(usize),
    #[error("block table too long ({0})")]
    TableSize(usize),
    #[error(transparent)]
    StateSerialize(#[from] block::SerializeError),
    #[error("failed to write block data")]
    WriteState(#[from] dynamic::WriteError),
    #[error(transparent)]
    Compress(#[from] super::CompressError),
}

impl Schematic {
    /// deserializes a schematic from base64
    /// ```
    /// # use mindus::*;
    /// let string = "bXNjaAF4nGNgZmBmZmDJS8xNZeBOyslPzlYAkwzcKanFyUWZBSWZ+XkMDAxsOYlJqTnFDEzRsYwMfAWJlTn5iSm6RfmlJalFQGlGEGJkZWSYxQAAcBkUPA==";
    /// let s = Schematic::deserialize_base64(string).unwrap();
    /// assert!(s.get(1, 1).unwrap().unwrap().block.name() == "payload-router");
    /// ```
    pub fn deserialize_base64(data: &str) -> Result<Schematic, R64Error> {
        let mut buff = vec![0; data.len() / 4 * 3 + 1];
        let n_out = base64::decode(data.as_bytes(), buff.as_mut())?;
        Ok(Self::deserialize(&mut DataRead::new(&buff[..n_out]))?)
    }

    /// serialize a schematic to base64
    pub fn serialize_base64(&self) -> Result<String, W64Error> {
        let mut buff = DataWrite::default();
        self.serialize(&mut buff)?;
        let buff = buff.get_written();
        // round up because of padding
        let mut text = vec![0; 4 * (buff.len() / 3 + usize::from(buff.len() % 3 != 0))];
        let n_out = base64::encode(buff, text.as_mut())?;
        // trailing zeros are valid UTF8, but not valid base64
        assert_eq!(n_out, text.len());
        // SAFETY: base64 encoding outputs pure ASCII (hopefully)
        Ok(unsafe { String::from_utf8_unchecked(text) })
    }
}

#[derive(Debug, Error)]
pub enum R64Error {
    #[error("base-64 decoding failed")]
    Base64(#[from] base64::DecodeError),
    #[error(transparent)]
    Content(#[from] ReadError),
}

#[derive(Debug, Error)]
pub enum W64Error {
    #[error("base-64 encoding failed")]
    Base64(#[from] base64::EncodeError),
    #[error(transparent)]
    Content(#[from] WriteError),
}

#[cfg(test)]
mod test {
    use super::*;
    #[track_caller]
    fn unwrap_pretty<T, E: std::fmt::Display + std::error::Error>(r: Result<T, E>) -> T {
        match r {
            Ok(t) => t,
            Err(e) => {
                use std::error::Error;
                eprintln!("{e}");
                let mut err_ref = &e as &dyn Error;
                loop {
                    let Some(next) = err_ref.source() else {
                        panic!();
                    };
                    eprintln!("\tFrom: {next}");
                    err_ref = next;
                }
            }
        }
    }

    macro_rules! test_schem {
        ($name:ident, $($val:expr);+;) => {
            #[test]
            fn $name() {
                $(
                    let parsed = unwrap_pretty(Schematic::deserialize_base64($val));
                    println!("\x1b[38;5;2mdeserialized\x1b[0m {}", parsed.tags.get("name").unwrap());
                    let unparsed = unwrap_pretty(parsed.serialize_base64());
                    println!("\x1b[38;5;2mserialized\x1b[0m {}", parsed.tags.get("name").unwrap());
                    let parsed2 = unwrap_pretty(Schematic::deserialize_base64(&unparsed));
                    println!("\x1b[38;5;2mredeserialized\x1b[0m {}", parsed.tags.get("name").unwrap());
                    if parsed != parsed2 {
                        #[cfg(feature = "bin")]
                        parsed2.render().save("p2.png");
                        #[cfg(feature = "bin")]
                        parsed.render().save("p1.png");
                        panic!("DIFFERENT! see `p1.png` != `p2.png`")
                    }
                )*
            }
        };
    }

    test_schem! {
        ser_de,
        "bXNjaAF4nCVNy07DMBCcvC1c4MBnoNz4G8TBSSxRycSRbVr646iHlmUc2/KOZ3dmFo9QDdrVfFkMb9Gsi5mgFxvncNzS0a8Aemcm6yLq948Bz2eTbBjtTwpmTj7gafs00Y6zX0/2Qt6dzLdLeNj8mbrVLxZ6ciamcQlH59BHH5iAYTKJeOGCF6AisFSoBxF55V+hJm1Lvwca8lpVIuzlS0eGLoMqTGUG6OLRJes3Mw40E5ijc2QedkPuU3DfLX0eHriDsgMapaScu9zkT26o5Uq8EmV/zS5vi4tr/wHvJE7M";
        "bXNjaAF4nE2MzWrEMAyEJ7bjdOnPobDQvfUF8kSlhyTWFlOv3VWcQvv0lRwoawzSjL4ZHOAtXJ4uhEdi+oz8ek5bDCvuA60Lx68aSwbg0zRTWmHe3j2emWI+F14ojEvJYYsVD5RoqVzSzy8xDjNNlzGXQHi5gVO8SvnIZasCnW4uM8fwQf9tT9+Ua1OUV0GBI9ozHToY6IeDtaIACxkOnaoe1rVrg2RV1cP0CuycLA5+LxuUU+U055W0Yrb4sEcGNQ3u1NTh9iHmH6qaOTI=";
        "bXNjaAF4nE2R226kQAxEzW1oYC5kopV23/IDfMw+R3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/7X7yMuOv0qjQqNCo0KjQqNCo0KjQqNCo0KjQqNCo0KjQqNCo0KjQqNCoEJWFHp987V9uXjv/9y4GAOhu6pc=";
        "bXNjaAF4nGNgY2BjZmDJS8xNZWBLTswrSyxm4E5JLU4uyiwoyczPY2BgYMtJTErNKWZgio5lhKthYOBkAAE+IDZjIB8wUWoAC2UGMFHqBSaoF1QYGTycJjFMUFHxVPBkmpQyiYXhpAonQ4OnEAPDJBVWBhXPW0wek7bkTlRhvLXNk4khdzYLQ8M2sAEUeoGFUi+wUBoLLJR5AQDzuCAp";
        "bXNjaAF4nEWNQRLCIAxFf5O0LhxdewlP5LighQUzCIyl97chVmHx8nmZDyYIQ7J7BUgqruLsw7q8Y22xZABTcnNIK+jxZJyWkv0WGy51S2u4H/Fak2vB/zJww/8MIAVZYh2Gw+jtCx2s+O7pE6nZB0V3bD1sTqtITe8Uc2JOzIm50RpH/U9Bht19AOy5Ge4=";
        "bXNjaAF4nBXKPQ6AIAwG0I+fuLjrKTyRcUDo0ASKKdXzq8kbHwJCQJTUCLGxEOZCIytfxl0ATDWdVAf8fjgsqRQ2fmhTyl2G6Z2t69fcz62I/gVp0BSJ";
    }

    #[test]
    fn block_iter() {
        macro_rules! test_iter {
            ($it:ident, $($val:expr;)*) => {
                $(assert_eq!($it.next().map(|(pos, p)| (pos, p.block)), $val);)*
            };
        }
        macro_rules! pair {
            ($x:literal,$y:literal,$v:expr) => {
                Some((GridPos($x, $y), &$v))
            };
            () => {
                None
            };
        }
        use crate::block::*;
        let mut s = Schematic::new(3, 3);
        s.put(0, 0, &DISTRIBUTOR)
            .put(0, 1, &JUNCTION)
            .put(1, 1, &PHASE_CONVEYOR)
            .put(2, 0, &ROUTER)
            .put(1, 1, &CONVEYOR);
        let mut it = s.block_iter();
        test_iter![
            it,
            pair!(0, 0, DISTRIBUTOR);
            pair!(0, 1, JUNCTION);
            pair!(1, 1, CONVEYOR);
            pair!(2, 0, ROUTER);
            pair!( );
        ];
        let s = Schematic::deserialize_base64("bXNjaAF4nDXKywqAIBQA0fFRBH1itDC7C8E01IT+vgia1VkMFmOwyR3C0N0VG/Mu1ZdwtpATMEa3SazoZdVMPqcudy7/DJovpV4peAAt0xF6").unwrap();
        let mut it = s.block_iter();
        test_iter![it,
            pair!(0, 0, CONVEYOR);
            pair!(2, 1, VAULT);
            pair!();
        ];
    }
}