tiger-lib 1.19.0

Library used by the tools ck3-tiger, vic3-tiger, and imperator-tiger. This library holds the bulk of the code for them. It can be built either for ck3-tiger with the feature ck3, or for vic3-tiger with the feature vic3, or for imperator-tiger with the feature imperator, but not both at the same time.
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
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
use std::path::{Path, PathBuf};
use std::str::FromStr;

use image::{DynamicImage, Rgb, RgbImage};
use itertools::Itertools;

use crate::block::Block;
use crate::everything::Everything;
#[cfg(test)]
use crate::fileset::FileStage;
use crate::fileset::{FileEntry, FileHandler};
use crate::helpers::{TigerHashMap, TigerHashSet};
use crate::item::{Item, ItemExt};
use crate::parse::ParserMemory;
use crate::parse::csv::{parse_csv, read_csv};
use crate::pdxfile::PdxFile;
use crate::report::{ErrorKey, Severity, err, fatal, report, untidy, warn};
use crate::token::{Loc, Token};

pub type ProvId = u32;

#[derive(Clone, Debug, Default)]
pub struct ImperatorProvinces {
    /// Colors in the provinces.png
    colors: TigerHashSet<Rgb<u8>>,

    /// Kept for adjacency coordinate validation.
    provinces_png: Option<RgbImage>,

    /// Provinces defined in definition.csv.
    /// Imperator requires uninterrupted indices starting at 0, but we want to be able to warn
    /// and continue if they're not, so it's a hashmap.
    provinces: TigerHashMap<ProvId, Province>,

    /// Kept and used for error reporting.
    definition_csv: Option<FileEntry>,

    adjacencies: Vec<Adjacency>,

    impassable: TigerHashSet<ProvId>,

    sea_or_river: TigerHashSet<ProvId>,

    default_map: Option<Block>,

    pending_map_files: Vec<FileEntry>,
}

fn map_filename(token: Option<&Token>) -> Option<String> {
    token
        .map(|value| value.as_str().trim_matches('"').to_string())
        .filter(|value| !value.is_empty())
}

fn matches_entry(entry: &FileEntry, expected: Option<&str>) -> bool {
    let Some(expected) = expected else { return false };
    let expected = expected.trim();
    if expected.is_empty() {
        return false;
    }

    let expected_path = Path::new(expected);
    if expected_path == entry.path() {
        return true;
    }

    expected_path.file_name().is_some_and(|filename| filename == entry.filename())
}

fn is_map_key(key: &Token) -> bool {
    key.lowercase_is("definitions")
        || key.lowercase_is("provinces")
        || key.lowercase_is("positions")
        || key.lowercase_is("rivers")
        || key.lowercase_is("topology")
        || key.lowercase_is("adjacencies")
        || key.lowercase_is("areas")
        || key.lowercase_is("regions")
        || key.lowercase_is("ports")
        || key.lowercase_is("climate")
}

impl ImperatorProvinces {
    fn expected_map_filename(&self, key: &str) -> Option<String> {
        if let Some(block) = &self.default_map
            && let Some(value) = map_filename(block.get_field_value(key))
        {
            return Some(value);
        }

        match key {
            "definitions" => Some("definition.csv".to_string()),
            "provinces" => Some("provinces.png".to_string()),
            "positions" => Some("positions.txt".to_string()),
            "rivers" => Some("rivers.png".to_string()),
            "topology" => Some("heightmap.heightmap".to_string()),
            "adjacencies" => Some("adjacencies.csv".to_string()),
            "areas" => Some("areas.txt".to_string()),
            "regions" => Some("regions.txt".to_string()),
            "ports" => Some("ports.csv".to_string()),
            "climate" => Some("climate.txt".to_string()),
            _ => None,
        }
    }
    fn province_color(&self, provid: ProvId) -> Option<Rgb<u8>> {
        self.provinces.get(&provid).map(|p| p.color)
    }

    fn provinces_png_pixel(&self, coords: Coords) -> Option<Rgb<u8>> {
        let img = self.provinces_png.as_ref()?;

        let x = u32::try_from(coords.x).ok()?;
        let y = u32::try_from(coords.y).ok()?;

        // Map pixels are addressed as x,y from the top-left corner.
        if x >= img.width() || y >= img.height() {
            return None;
        }

        Some(*img.get_pixel(x, y))
    }

    fn parse_definition(&mut self, csv: &[Token]) {
        if let Some(province) = Province::parse(csv) {
            if self.provinces.contains_key(&province.id) {
                err(ErrorKey::DuplicateItem)
                    .msg("duplicate entry for this province id")
                    .loc(&province.comment)
                    .push();
            }
            self.provinces.insert(province.id, province);
        }
    }

    pub fn load_impassable(&mut self, block: &Block) {
        enum Expecting<'a> {
            Range(&'a Token),
            List(&'a Token),
            Nothing,
        }

        let mut expecting = Expecting::Nothing;
        for item in block.iter_items() {
            match expecting {
                Expecting::Nothing => {
                    if let Some((key, token)) = item.expect_assignment() {
                        if key.lowercase_is("sea_zones")
                            || key.lowercase_is("river_provinces")
                            || key.lowercase_is("impassable_terrain")
                            || key.lowercase_is("uninhabitable")
                            || key.lowercase_is("wasteland")
                            || key.lowercase_is("lakes")
                        {
                            if token.is("LIST") {
                                expecting = Expecting::List(key);
                            } else if token.is("RANGE") {
                                expecting = Expecting::Range(key);
                            } else {
                                expecting = Expecting::Nothing;
                            }
                        } else if !is_map_key(key) {
                            let msg = format!("unexpected key `{key}`");
                            warn(ErrorKey::UnknownField).msg(msg).loc(key).push();
                        }
                    }
                }
                Expecting::Range(key) => {
                    if let Some(block) = item.expect_block() {
                        let vec: Vec<&Token> = block.iter_values().collect();
                        if vec.len() != 2 {
                            err(ErrorKey::Validation).msg("invalid RANGE").loc(block).push();
                            expecting = Expecting::Nothing;
                            continue;
                        }
                        let from = vec[0].as_str().parse::<ProvId>();
                        let to = vec[1].as_str().parse::<ProvId>();
                        if from.is_err() || to.is_err() {
                            err(ErrorKey::Validation).msg("invalid RANGE").loc(block).push();
                            expecting = Expecting::Nothing;
                            continue;
                        }
                        for provid in from.unwrap()..=to.unwrap() {
                            self.impassable.insert(provid);
                            if key.is("sea_zones") || key.is("river_provinces") {
                                self.sea_or_river.insert(provid);
                            }
                        }
                    }
                    expecting = Expecting::Nothing;
                }
                Expecting::List(key) => {
                    if let Some(block) = item.expect_block() {
                        for token in block.iter_values() {
                            let provid = token.as_str().parse::<ProvId>();
                            if let Ok(provid) = provid {
                                self.impassable.insert(provid);
                                if key.is("sea_zones") || key.is("river_provinces") {
                                    self.sea_or_river.insert(provid);
                                }
                            } else {
                                err(ErrorKey::Validation)
                                    .msg("invalid LIST item")
                                    .loc(token)
                                    .push();
                                break;
                            }
                        }
                    }
                    expecting = Expecting::Nothing;
                }
            }
        }
    }

    pub fn verify_exists_implied(&self, key: &str, item: &Token, max_sev: Severity) {
        if let Ok(provid) = key.parse::<ProvId>() {
            if !self.provinces.contains_key(&provid) {
                let msg = format!("province {provid} not defined in map_data/definition.csv");
                report(ErrorKey::MissingItem, Item::Province.severity()).msg(msg).loc(item).push();
            }
        } else {
            let msg = "province id should be numeric";
            let sev = Item::Province.severity().at_most(max_sev);
            report(ErrorKey::Validation, sev).msg(msg).loc(item).push();
        }
    }

    pub fn exists(&self, key: &str) -> bool {
        if let Ok(provid) = key.parse::<ProvId>() {
            self.provinces.contains_key(&provid)
        } else {
            false
        }
    }

    pub fn iter_keys(&self) -> impl Iterator<Item = &Token> {
        self.provinces.values().map(|item| &item.key)
    }

    pub fn validate(&self, _data: &Everything) {
        for item in &self.adjacencies {
            item.validate(self);
        }
    }

    fn handle_adjacencies_content(&mut self, entry: &FileEntry, content: &str) {
        let mut seen_terminator = false;
        for csv in parse_csv(entry, 1, content) {
            if csv[0].is("-1") {
                seen_terminator = true;
            } else if seen_terminator {
                let msg = "the line with all `-1;` should be the last line in the file";
                warn(ErrorKey::ParseError).msg(msg).loc(&csv[0]).push();
                break;
            } else {
                self.adjacencies.extend(Adjacency::parse(&csv));
            }
        }
        if !seen_terminator {
            let msg = "Imperator needs a line with all `-1;` at the end of this file";
            err(ErrorKey::ParseError).msg(msg).loc(entry).push();
        }
    }

    fn handle_definitions_content(&mut self, entry: &FileEntry, content: &str) {
        self.definition_csv = Some(entry.clone());
        for csv in parse_csv(entry, 0, content) {
            self.parse_definition(&csv);
        }
    }

    fn handle_provinces_image(&mut self, img: DynamicImage, entry: &FileEntry) {
        match img {
            DynamicImage::ImageRgb8(img) => {
                for pixel in img.pixels().dedup() {
                    self.colors.insert(*pixel);
                }

                // Keep the full image for validating adjacency coordinates.
                self.provinces_png = Some(img);
            }
            other => {
                let msg = format!(
                    "`{}` has wrong color format `{:?}`, should be Rgb8",
                    entry.path().display(),
                    other.color()
                );
                err(ErrorKey::ImageFormat).msg(msg).loc(entry).push();
            }
        }
    }

    fn process_map_entry(&mut self, entry: &FileEntry) {
        let adjacencies = self.expected_map_filename("adjacencies");
        if matches_entry(entry, adjacencies.as_deref()) {
            let content = match read_csv(entry.fullpath()) {
                Ok(content) => content,
                Err(e) => {
                    err(ErrorKey::ReadError)
                        .msg(format!("could not read file: {e:#}"))
                        .loc(entry)
                        .push();
                    return;
                }
            };
            self.handle_adjacencies_content(entry, &content);
            return;
        }

        let definitions = self.expected_map_filename("definitions");
        if matches_entry(entry, definitions.as_deref()) {
            let content = match read_csv(entry.fullpath()) {
                Ok(content) => content,
                Err(e) => {
                    let msg = format!("could not read `{}`: {:#}", entry.path().display(), e);
                    err(ErrorKey::ReadError).msg(msg).loc(entry).push();
                    return;
                }
            };
            self.handle_definitions_content(entry, &content);
            return;
        }

        let provinces = self.expected_map_filename("provinces");
        if matches_entry(entry, provinces.as_deref()) {
            let img = match image::open(entry.fullpath()) {
                Ok(img) => img,
                Err(e) => {
                    let msg = format!("could not read `{}`: {e:#}", entry.path().display());
                    err(ErrorKey::ReadError).msg(msg).loc(entry).push();
                    return;
                }
            };
            self.handle_provinces_image(img, entry);
        }
    }

    fn process_pending_map_entries(&mut self) {
        let pending = std::mem::take(&mut self.pending_map_files);
        for entry in pending {
            self.process_map_entry(&entry);
        }
    }
}

#[derive(Debug)]
pub enum FileContent {
    DefaultMap(Block),
    Deferred,
}

impl FileHandler<FileContent> for ImperatorProvinces {
    fn subpath(&self) -> PathBuf {
        PathBuf::from("map_data")
    }

    fn load_file(&self, entry: &FileEntry, parser: &ParserMemory) -> Option<FileContent> {
        if entry.path().components().count() == 2 {
            if &*entry.filename().to_string_lossy() == "default.map" {
                return PdxFile::read_optional_bom(entry, parser).map(FileContent::DefaultMap);
            }
            return Some(FileContent::Deferred);
        }
        None
    }

    fn handle_file(&mut self, entry: &FileEntry, content: FileContent) {
        match content {
            FileContent::DefaultMap(block) => {
                self.default_map = Some(block.clone());
                self.load_impassable(&block);
            }
            FileContent::Deferred => {
                if self.default_map.is_some() {
                    self.process_map_entry(entry);
                } else {
                    self.pending_map_files.push(entry.clone());
                }
            }
        }
    }

    fn finalize(&mut self) {
        self.process_pending_map_entries();

        if self.definition_csv.is_none() {
            // Shouldn't happen, it should come from vanilla if not from the mod
            eprintln!("map_data/definition.csv is missing?!?");
            return;
        }
        let definition_csv = self.definition_csv.as_ref().unwrap();

        let mut seen_colors = TigerHashMap::default();
        #[allow(clippy::cast_possible_truncation)]
        for i in 1..self.provinces.len() as u32 {
            if let Some(province) = self.provinces.get(&i) {
                if let Some(k) = seen_colors.get(&province.color) {
                    let msg = format!("color was already used for id {k}");
                    warn(ErrorKey::Colors).msg(msg).loc(&province.comment).push();
                } else {
                    seen_colors.insert(province.color, i);
                }
            } else {
                let msg = format!("province ids must be sequential, but {i} is missing");
                err(ErrorKey::Validation).msg(msg).loc(definition_csv).push();
                return;
            }
        }
        for color in &self.colors {
            if !seen_colors.contains_key(color) {
                let Rgb(rgb) = color;
                let msg = format!(
                    "definitions.csv lacks entry for color ({}, {}, {})",
                    rgb[0], rgb[1], rgb[2]
                );
                untidy(ErrorKey::Colors).msg(msg).loc(definition_csv).push();
            }
        }
    }
}

#[derive(Copy, Clone, Debug, Default)]
pub struct Coords {
    x: i32,
    y: i32,
}

impl Coords {
    fn is_sentinel(self) -> bool {
        self.x == -1 && self.y == -1
    }
}

#[allow(dead_code)] // TODO
#[derive(Clone, Debug)]
pub struct Adjacency {
    line: Loc,
    from: ProvId,
    to: ProvId,
    /// Adjacency kind, should be `sea` or `river_large`.
    kind: Token,
    through: ProvId,
    /// start and stop are map coordinates (should be within provinces.png bounds) and should have the right color on provinces.png
    /// They can be -1 -1 though.
    start: Coords,
    stop: Coords,
    comment: Token,
}

fn verify<T: FromStr>(v: &Token, msg: &str) -> Option<T> {
    let r = v.as_str().parse().ok();
    if r.is_none() {
        err(ErrorKey::ParseError).msg(msg).loc(v).push();
    }
    r
}

impl Adjacency {
    pub fn parse(csv: &[Token]) -> Option<Self> {
        if csv.is_empty() {
            return None;
        }

        let line = csv[0].loc;

        if csv.len() != 9 {
            let msg = "wrong number of fields for this line, expected 9";
            err(ErrorKey::ParseError).msg(msg).loc(&csv[0]).push();
            return None;
        }

        let from = verify(&csv[0], "expected province id");
        let to = verify(&csv[1], "expected province id");
        let through = verify(&csv[3], "expected province id");
        let start_x = verify(&csv[4], "expected x coordinate");
        let start_y = verify(&csv[5], "expected y coordinate");
        let stop_x = verify(&csv[6], "expected x coordinate");
        let stop_y = verify(&csv[7], "expected y coordinate");

        Some(Adjacency {
            line,
            from: from?,
            to: to?,
            kind: csv[2].clone(),
            through: through?,
            start: Coords { x: start_x?, y: start_y? },
            stop: Coords { x: stop_x?, y: stop_y? },
            comment: csv[8].clone(),
        })
    }

    fn validate(&self, provinces: &ImperatorProvinces) {
        for prov in &[self.from, self.to, self.through] {
            if !provinces.provinces.contains_key(prov) {
                let msg = format!("province id {prov} not defined in definitions.csv");
                fatal(ErrorKey::Crash).msg(msg).loc(self.line).push();
            }
        }

        if !self.kind.lowercase_is("sea") && !self.kind.lowercase_is("river_large") {
            let msg = format!(
                "adjacency type `{}` is invalid; expected `sea` or `river_large`",
                self.kind.as_str()
            );
            err(ErrorKey::Validation).msg(msg).loc(&self.kind).push();
        }

        if self.start.is_sentinel() && self.stop.is_sentinel() {
            return;
        }

        let Some(img) = provinces.provinces_png.as_ref() else {
            // Can't validate coordinates without provinces.png.
            return;
        };

        let (w, h) = (img.width(), img.height());
        for (label, coords) in [("start", &self.start), ("stop", &self.stop)] {
            if coords.is_sentinel() {
                continue;
            }

            let x = u32::try_from(coords.x);
            let y = u32::try_from(coords.y);
            if x.is_err() || y.is_err() {
                let msg = format!(
                    "{label} coordinate ({}, {}) is out of bounds (image size {}x{})",
                    coords.x, coords.y, w, h
                );
                err(ErrorKey::Validation).msg(msg).loc(&self.comment).push();
                continue;
            }
            let (x, y) = (x.unwrap(), y.unwrap());
            if x >= w || y >= h {
                let msg = format!(
                    "{label} coordinate ({}, {}) is out of bounds (image size {}x{})",
                    coords.x, coords.y, w, h
                );
                err(ErrorKey::Validation).msg(msg).loc(&self.comment).push();
            }
        }

        if !self.start.is_sentinel() {
            let Some(expected_start) = provinces.province_color(self.from) else {
                return;
            };
            if let Some(actual) = provinces.provinces_png_pixel(self.start)
                && actual != expected_start
            {
                let Rgb([er, eg, eb]) = expected_start;
                let Rgb([ar, ag, ab]) = actual;
                let msg = format!(
                    "start coordinate is in the wrong province color: expected ({er}, {eg}, {eb}), got ({ar}, {ag}, {ab})"
                );
                err(ErrorKey::Validation).msg(msg).loc(&self.comment).push();
            }
        }

        if !self.stop.is_sentinel() {
            let Some(expected_stop) = provinces.province_color(self.to) else {
                return;
            };
            if let Some(actual) = provinces.provinces_png_pixel(self.stop)
                && actual != expected_stop
            {
                let Rgb([er, eg, eb]) = expected_stop;
                let Rgb([ar, ag, ab]) = actual;
                let msg = format!(
                    "stop coordinate is in the wrong province color: expected ({er}, {eg}, {eb}), got ({ar}, {ag}, {ab})"
                );
                err(ErrorKey::Validation).msg(msg).loc(&self.comment).push();
            }
        }
    }
}

#[derive(Clone, Debug)]
pub struct Province {
    key: Token,
    id: ProvId,
    color: Rgb<u8>,
    comment: Token,
}

impl Province {
    fn parse(csv: &[Token]) -> Option<Self> {
        if csv.is_empty() {
            return None;
        }

        if csv.len() < 5 {
            let msg = "too few fields for this line, expected 5";
            err(ErrorKey::ParseError).msg(msg).loc(&csv[0]).push();
            return None;
        }

        let id = verify(&csv[0], "expected province id")?;
        let r = verify(&csv[1], "expected red value")?;
        let g = verify(&csv[2], "expected green value")?;
        let b = verify(&csv[3], "expected blue value")?;
        let color = Rgb::from([r, g, b]);
        Some(Province { key: csv[0].clone(), id, color, comment: csv[4].clone() })
    }
}

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

    use std::path::PathBuf;
    use std::sync::{LazyLock, Mutex};

    use crate::fileset::FileKind;
    use crate::report::take_reports;

    static TEST_MUTEX: LazyLock<Mutex<()>> = LazyLock::new(|| Mutex::new(()));

    fn with_test_lock<T>(f: impl FnOnce() -> T) -> T {
        let _guard = TEST_MUTEX.lock().unwrap();
        let _ = take_reports();
        f()
    }

    fn loc(line: u32, column: u32) -> Loc {
        let mut loc = Loc::for_file(
            PathBuf::from("map_data/adjacencies.csv"),
            FileStage::NoStage,
            FileKind::Mod,
            PathBuf::from("C:/test/map_data/adjacencies.csv"),
        );
        loc.line = line;
        loc.column = column;
        loc
    }

    fn tok(s: &str, line: u32, column: u32) -> Token {
        Token::new(s, loc(line, column))
    }

    fn base_provinces(img: RgbImage, from_color: Rgb<u8>, to_color: Rgb<u8>) -> ImperatorProvinces {
        let _ = take_reports();

        let mut provinces = ImperatorProvinces::default();
        provinces.provinces_png = Some(img);
        provinces.provinces.insert(
            1,
            Province { key: tok("1", 1, 1), id: 1, color: from_color, comment: tok("c", 1, 1) },
        );
        provinces.provinces.insert(
            2,
            Province { key: tok("2", 1, 1), id: 2, color: to_color, comment: tok("c", 1, 1) },
        );
        provinces
    }

    fn adjacency(start: Coords, stop: Coords) -> Adjacency {
        Adjacency {
            line: loc(1, 1),
            from: 1,
            to: 2,
            kind: tok("sea", 1, 5),
            through: 1,
            start,
            stop,
            comment: tok("comment", 1, 10),
        }
    }

    fn adjacency_with_kind(kind: &str, start: Coords, stop: Coords) -> Adjacency {
        Adjacency { kind: tok(kind, 1, 5), ..adjacency(start, stop) }
    }

    fn take_msgs() -> Vec<String> {
        take_reports().into_iter().map(|(meta, _)| meta.msg).collect()
    }

    #[test]
    fn adjacency_start_out_of_bounds_errors() {
        with_test_lock(|| {
            let img = RgbImage::from_pixel(2, 2, Rgb([1, 2, 3]));
            let provinces = base_provinces(img, Rgb([1, 2, 3]), Rgb([9, 9, 9]));

            let adj = adjacency(Coords { x: 5, y: 0 }, Coords { x: -1, y: -1 });
            adj.validate(&provinces);

            let msgs = take_msgs();
            assert!(
                msgs.iter().any(|m| m.contains("start coordinate (5, 0) is out of bounds")),
                "reports were: {msgs:?}"
            );
        });
    }

    #[test]
    fn adjacency_start_wrong_color_errors() {
        with_test_lock(|| {
            let mut img = RgbImage::from_pixel(2, 2, Rgb([0, 0, 0]));
            img.put_pixel(0, 0, Rgb([9, 9, 9]));
            let provinces = base_provinces(img, Rgb([1, 2, 3]), Rgb([7, 8, 9]));

            let adj = adjacency(Coords { x: 0, y: 0 }, Coords { x: -1, y: -1 });
            adj.validate(&provinces);

            let msgs = take_msgs();
            assert!(
                msgs.iter().any(|m| m.contains("start coordinate is in the wrong province color")),
                "reports were: {msgs:?}"
            );
        });
    }

    #[test]
    fn adjacency_stop_wrong_color_errors_when_start_sentinel() {
        with_test_lock(|| {
            let mut img = RgbImage::from_pixel(2, 2, Rgb([0, 0, 0]));
            img.put_pixel(1, 1, Rgb([9, 9, 9]));
            let provinces = base_provinces(img, Rgb([1, 2, 3]), Rgb([7, 8, 9]));

            let adj = adjacency(Coords { x: -1, y: -1 }, Coords { x: 1, y: 1 });
            adj.validate(&provinces);

            let msgs = take_msgs();
            assert!(
                msgs.iter().any(|m| m.contains("stop coordinate is in the wrong province color")),
                "reports were: {msgs:?}"
            );
        });
    }

    #[test]
    fn adjacency_one_endpoint_sentinel_can_still_pass() {
        with_test_lock(|| {
            let mut img = RgbImage::from_pixel(2, 2, Rgb([0, 0, 0]));
            img.put_pixel(1, 0, Rgb([7, 8, 9]));
            let provinces = base_provinces(img, Rgb([1, 2, 3]), Rgb([7, 8, 9]));

            let adj = adjacency(Coords { x: -1, y: -1 }, Coords { x: 1, y: 0 });
            adj.validate(&provinces);

            let msgs = take_msgs();
            assert!(msgs.is_empty(), "reports were: {msgs:?}");
        });
    }

    #[test]
    fn adjacency_kind_invalid_errors_even_if_coords_sentinel() {
        with_test_lock(|| {
            let img = RgbImage::from_pixel(2, 2, Rgb([1, 2, 3]));
            let provinces = base_provinces(img, Rgb([1, 2, 3]), Rgb([9, 9, 9]));

            let adj = adjacency_with_kind("land", Coords { x: -1, y: -1 }, Coords { x: -1, y: -1 });
            adj.validate(&provinces);

            let msgs = take_msgs();
            assert!(
                msgs.iter().any(|m| m.contains("adjacency type `land` is invalid")),
                "reports were: {msgs:?}"
            );
        });
    }

    #[test]
    fn adjacency_kind_sea_and_river_large_are_allowed() {
        with_test_lock(|| {
            let mut img = RgbImage::from_pixel(2, 2, Rgb([0, 0, 0]));
            img.put_pixel(0, 0, Rgb([1, 2, 3]));
            img.put_pixel(1, 0, Rgb([7, 8, 9]));
            let provinces = base_provinces(img, Rgb([1, 2, 3]), Rgb([7, 8, 9]));

            for kind in ["sea", "river_large"] {
                let adj = adjacency_with_kind(kind, Coords { x: 0, y: 0 }, Coords { x: 1, y: 0 });
                adj.validate(&provinces);

                let msgs = take_msgs();
                assert!(msgs.is_empty(), "kind {kind} reports were: {msgs:?}");
            }
        });
    }
}