pact-plugin-driver 1.2.2

Pact support library that provides an interface for interacting with Pact plugins
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
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
//! Manages the catalogue of features provided by plugins

use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};
use std::sync::Mutex;

use itertools::Itertools;
use lazy_static::lazy_static;
use maplit::hashset;
use pact_models::content_types::ContentType;
use regex::Regex;
use serde::{Deserialize, Serialize};
use tracing::{debug, error, instrument, trace, warn};

use crate::content::{ContentGenerator, ContentMatcher};
use crate::plugin_models::PactPluginManifest;
use crate::proto::catalogue_entry::EntryType;
use crate::proto::CatalogueEntry as ProtoCatalogueEntry;
use crate::proto_v2::catalogue_entry::EntryType as EntryTypeV2;

lazy_static! {
  static ref CATALOGUE_REGISTER: Mutex<HashMap<String, CatalogueEntry>> = Mutex::new(HashMap::new());
}

/// Type of catalogue entry
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum CatalogueEntryType {
  /// Content matcher (based on content type)
  CONTENT_MATCHER,
  /// Content generator (based on content type)
  CONTENT_GENERATOR,
  /// Network transport
  TRANSPORT,
  /// Matching rule for a content field/value
  MATCHER,
  /// Type of interaction
  INTERACTION,
  /// Generator for a content field/value. Only representable on the V2 plugin interface - the V1
  /// `EntryType` enum has no equivalent. See proposal 006 (Field-level matchers and generators).
  GENERATOR
}

impl CatalogueEntryType {
  /// Return the protobuf type for this entry type.
  ///
  /// This maps to the *V1* enum, which cannot represent every entry type: `GENERATOR` has no V1
  /// equivalent and is reported as `Matcher`. Use [`CatalogueEntryType::to_proto_value`] instead,
  /// which returns the wire value and is lossless for both interface versions.
  #[deprecated(
    since = "1.2.0",
    note = "Use to_proto_value, which can represent entry types that only exist on the V2 interface"
  )]
  pub fn to_proto_type(&self) -> EntryType {
    match self {
      CatalogueEntryType::CONTENT_MATCHER => EntryType::ContentMatcher,
      CatalogueEntryType::CONTENT_GENERATOR => EntryType::ContentGenerator,
      CatalogueEntryType::TRANSPORT => EntryType::Transport,
      CatalogueEntryType::MATCHER => EntryType::Matcher,
      CatalogueEntryType::INTERACTION => EntryType::Interaction,
      CatalogueEntryType::GENERATOR => EntryType::Matcher
    }
  }

  /// The V2 protobuf enum for this entry type. V2 is the canonical source of the wire values: it
  /// mirrors V1 for the entry types both versions share, and adds the ones V1 never had.
  fn to_proto_enum(self) -> EntryTypeV2 {
    match self {
      CatalogueEntryType::CONTENT_MATCHER => EntryTypeV2::ContentMatcher,
      CatalogueEntryType::CONTENT_GENERATOR => EntryTypeV2::ContentGenerator,
      CatalogueEntryType::TRANSPORT => EntryTypeV2::Transport,
      CatalogueEntryType::MATCHER => EntryTypeV2::Matcher,
      CatalogueEntryType::INTERACTION => EntryTypeV2::Interaction,
      CatalogueEntryType::GENERATOR => EntryTypeV2::Generator
    }
  }

  fn from_proto_enum(entry_type: EntryTypeV2) -> CatalogueEntryType {
    match entry_type {
      EntryTypeV2::ContentMatcher => CatalogueEntryType::CONTENT_MATCHER,
      EntryTypeV2::ContentGenerator => CatalogueEntryType::CONTENT_GENERATOR,
      EntryTypeV2::Transport => CatalogueEntryType::TRANSPORT,
      EntryTypeV2::Matcher => CatalogueEntryType::MATCHER,
      EntryTypeV2::Interaction => CatalogueEntryType::INTERACTION,
      EntryTypeV2::Generator => CatalogueEntryType::GENERATOR
    }
  }

  /// The protobuf enum value for this entry type, for setting the `type` field of a
  /// `CatalogueEntry` message. Lossless for every entry type, including those a V1 plugin will
  /// not recognise - a V1 plugin decodes an unknown value as an unrecognised enum and ignores the
  /// entry, which is the correct outcome, whereas mapping it onto some other V1 type would have
  /// the plugin act on an entry that is not what it thinks it is.
  pub fn to_proto_value(self) -> i32 {
    self.to_proto_enum() as i32
  }

  /// The entry type for a protobuf enum value, or `None` if the value is not one this driver
  /// understands (an entry type added by a later interface version). Deliberately not defaulting
  /// to `CONTENT_MATCHER` the way prost's generated accessor does: silently mis-typing an entry
  /// is worse than ignoring one.
  pub fn from_proto_value(value: i32) -> Option<CatalogueEntryType> {
    EntryTypeV2::try_from(value).ok().map(CatalogueEntryType::from_proto_enum)
  }

  /// The protobuf enum value name for this entry type, e.g. `"CONTENT_MATCHER"`. This is the form
  /// a Lua plugin uses in the catalogue entries returned from its `init` function.
  pub fn as_proto_name(&self) -> &'static str {
    self.to_proto_enum().as_str_name()
  }

  /// The entry type for a protobuf enum value name, e.g. `"CONTENT_MATCHER"`, or `None` if the
  /// name is not one this driver understands.
  pub fn from_proto_name(name: &str) -> Option<CatalogueEntryType> {
    EntryTypeV2::from_str_name(name).map(CatalogueEntryType::from_proto_enum)
  }
}

impl Display for CatalogueEntryType {
  fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
    match self {
      CatalogueEntryType::CONTENT_MATCHER => write!(f, "content-matcher"),
      CatalogueEntryType::CONTENT_GENERATOR => write!(f, "content-generator"),
      CatalogueEntryType::TRANSPORT => write!(f, "transport"),
      CatalogueEntryType::MATCHER => write!(f, "matcher"),
      CatalogueEntryType::INTERACTION => write!(f, "interaction"),
      CatalogueEntryType::GENERATOR => write!(f, "generator"),
    }
  }
}

impl From<&str> for CatalogueEntryType {
  fn from(s: &str) -> Self {
    match s {
      "content-matcher" => CatalogueEntryType::CONTENT_MATCHER,
      "content-generator" => CatalogueEntryType::CONTENT_GENERATOR,
      "interaction" => CatalogueEntryType::INTERACTION,
      "matcher" => CatalogueEntryType::MATCHER,
      "transport" => CatalogueEntryType::TRANSPORT,
      "generator" => CatalogueEntryType::GENERATOR,
      _ => {
        let message = format!("'{}' is not a valid CatalogueEntryType value", s);
        error!("{}", message);
        panic!("{}", message)
      }
    }
  }
}

impl From<String> for CatalogueEntryType {
  fn from(s: String) -> Self {
    Self::from(s.as_str())
  }
}

impl From<EntryType> for CatalogueEntryType {
  fn from(t: EntryType) -> Self {
    match t {
      EntryType::ContentMatcher => CatalogueEntryType::CONTENT_MATCHER,
      EntryType::ContentGenerator => CatalogueEntryType::CONTENT_GENERATOR,
      EntryType::Transport => CatalogueEntryType::TRANSPORT,
      EntryType::Matcher => CatalogueEntryType::MATCHER,
      EntryType::Interaction => CatalogueEntryType::INTERACTION
    }
  }
}

/// Provider of the catalogue entry
#[derive(Clone, Copy, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[allow(non_camel_case_types)]
pub enum CatalogueEntryProviderType {
  /// Core Pact framework
  CORE,
  /// Plugin
  PLUGIN
}

/// Catalogue entry
#[derive(Clone, Debug, Deserialize, Serialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct CatalogueEntry {
  /// Type of entry
  pub entry_type: CatalogueEntryType,
  /// Provider of the entry
  pub provider_type: CatalogueEntryProviderType,
  /// Plugin manifest
  pub plugin: Option<PactPluginManifest>,
  /// Entry key
  pub key: String,
  /// assocaited Entry values
  pub values: HashMap<String, String>
}

/// Register the entries in the global catalogue
pub fn register_plugin_entries(plugin: &PactPluginManifest, catalogue_list: &Vec<ProtoCatalogueEntry>) {
  trace!("register_plugin_entries({:?}, {:?})", plugin, catalogue_list);

  let mut guard = CATALOGUE_REGISTER.lock().unwrap();

  for entry in catalogue_list {
    // Deliberately reading the raw field rather than prost's `entry.r#type()` accessor: the
    // accessor is generated against the V1 enum and maps anything it doesn't recognise to the
    // default (`CONTENT_MATCHER`), which would silently register a V2-only entry type - a
    // `GENERATOR`, say - as a content matcher.
    let entry_type = match CatalogueEntryType::from_proto_value(entry.r#type) {
      Some(entry_type) => entry_type,
      None => {
        warn!(
          "Ignoring catalogue entry '{}' from plugin '{}': {} is not a catalogue entry type this driver understands",
          entry.key, plugin.name, entry.r#type
        );
        continue;
      }
    };
    let key = format!("plugin/{}/{}/{}", plugin.name, entry_type, entry.key);
    guard.insert(key.clone(), CatalogueEntry {
      entry_type,
      provider_type: CatalogueEntryProviderType::PLUGIN,
      plugin: Some(plugin.clone()),
      key: entry.key.clone(),
      values: entry.values.iter().map(|(k, v)| (k.clone(), v.clone())).collect()
    });
  }

  debug!("Updated catalogue entries:\n{}", guard.keys().sorted().join("\n"))
}

/// Register the core Pact framework entries in the global catalogue
pub fn register_core_entries(entries: &Vec<CatalogueEntry>) {
  trace!("register_core_entries({:?})", entries);

  let mut inner = CATALOGUE_REGISTER.lock().unwrap();

  let mut updated_keys = hashset!();
  for entry in entries {
    let key = format!("core/{}/{}", entry.entry_type, entry.key);
    if !inner.contains_key(&key) {
      inner.insert(key.clone(), entry.clone());
      updated_keys.insert(key.clone());
    }
  }

  if !updated_keys.is_empty() {
    debug!("Updated catalogue entries:\n{}", updated_keys.iter().sorted().join("\n"));
  }
}

/// Lookup an entry in the catalogue by the key, matched the same way [`resolve_capability`] does:
/// by name - the whole catalogue key, or a trailing run of its `/`-separated components.
///
/// Unlike [`resolve_capability`], this takes the first match when more than one entry matches, and
/// `HashMap` iteration order is randomised per process. Prefer [`resolve_capability`] wherever the
/// expected entry type is known and a deterministic answer matters.
pub fn lookup_entry(key: &str) -> Option<CatalogueEntry> {
  let inner = CATALOGUE_REGISTER.lock().unwrap();
  inner.iter()
    .find(|(k, _)| names_catalogue_key(k, key))
    .map(|(_, v)| v.clone())
}

/// Where a resolved catalogue entry's capability should be dispatched. Shared by every transport
/// that lets a plugin call back into a capability by catalogue entry key - the gRPC `PluginHost`
/// service, and the Lua/WASM host functions - so there is exactly one place that decides "who
/// provides this entry". See proposal 007 ("One resolver, [multiple] call directions").
#[derive(Debug, Clone)]
pub enum ResolvedCapability {
  /// A host-registered core handler, keyed by the unprefixed catalogue entry key.
  Core(String),
  /// A running plugin, identified by its manifest.
  Plugin(Box<PactPluginManifest>)
}

/// Does `entry_key` name this catalogue key? Either the whole key (`core/content-matcher/xml`),
/// or a trailing run of its `/`-separated components (`content-matcher/xml`, or just `xml`).
///
/// Components are compared whole, never as substrings: `"type"` names `core/matcher/type` but not
/// `core/matcher/content-type`, which is the `content-type` rule and has nothing to do with it.
fn names_catalogue_key(catalogue_key: &str, entry_key: &str) -> bool {
  let key_parts = catalogue_key.split('/').collect::<Vec<_>>();
  let query_parts = entry_key.split('/').collect::<Vec<_>>();
  query_parts.len() <= key_parts.len()
    && key_parts[key_parts.len() - query_parts.len()..] == query_parts[..]
}

/// Resolve a callback's catalogue entry key to a dispatch target, the same way
/// [`crate::content::ContentMatcher::is_core`]/[`crate::content::ContentGenerator::is_core`] do
/// for the driver's own outbound calls.
///
/// `entry_key` is matched against the catalogue by name - the whole catalogue key
/// (`core/content-matcher/xml`) or a trailing run of its components (`content-matcher/xml`,
/// `xml`), compared component by component. Core matching rules and generators are registered
/// under the name the rule carries in a request (`type`, `regex`, `content-type`), so a bare rule
/// name resolves to the core entry without any further convention.
///
/// A short name that matches both a core entry and a plugin's own - a plugin registering
/// `matcher/date` alongside the core `date` rule - is an ambiguity error, not a silent pick;
/// either caller disambiguates with more of the key (`core/matcher/date`,
/// `plugin/my-plugin/matcher/date`).
///
/// Unlike [`lookup_entry`], this does not just take the first `HashMap` hit when more than one
/// entry matches. A short, unqualified `entry_key` can still match more than one entry of the
/// *same* `expected_type` (a plugin registering its own `content-matcher/xml` alongside a
/// host-registered core `content-matcher/xml`), and `HashMap` iteration order is randomised per
/// process - silently picking one would make the dispatch target non-deterministic across
/// restarts. `expected_type` still guards against the *wrong* capability shape (a
/// content-generator registered under the same name as an unrelated content-matcher), mirroring
/// the explicit `entry_type` check [`find_content_matcher`]/[`find_content_generator`] already do.
pub fn resolve_capability(entry_key: &str, expected_type: CatalogueEntryType) -> anyhow::Result<ResolvedCapability> {
  let entry = resolve_capability_entry(entry_key, expected_type)?;
  match entry.provider_type {
    CatalogueEntryProviderType::CORE => Ok(ResolvedCapability::Core(entry.key.clone())),
    CatalogueEntryProviderType::PLUGIN => entry.plugin.clone()
      .map(|manifest| ResolvedCapability::Plugin(Box::new(manifest)))
      .ok_or_else(|| anyhow::anyhow!("Catalogue entry '{}' has no plugin manifest", entry_key))
  }
}

/// Resolve a catalogue entry key to the entry itself, matching it the same way
/// [`resolve_capability`] documents. Callers that need the entry rather than a dispatch target -
/// [`crate::field::find_field_matcher`] and [`crate::field::find_field_generator`], which wrap it
/// in a matcher/generator - use this directly.
pub fn resolve_capability_entry(entry_key: &str, expected_type: CatalogueEntryType) -> anyhow::Result<CatalogueEntry> {
  let candidates: Vec<(String, CatalogueEntry)> = {
    let inner = CATALOGUE_REGISTER.lock().unwrap();
    inner.iter()
      .filter(|(k, _)| names_catalogue_key(k, entry_key))
      .map(|(k, v)| (k.clone(), v.clone()))
      .collect()
  };

  let mut of_expected_type = candidates.iter().filter(|(_, entry)| entry.entry_type == expected_type);
  let entry = match (of_expected_type.next(), of_expected_type.next()) {
    (None, _) => return match candidates.first() {
      Some((_, entry)) => Err(anyhow::anyhow!(
        "Catalogue entry '{}' is a {:?}, not a {:?}", entry_key, entry.entry_type, expected_type
      )),
      None => Err(anyhow::anyhow!("No catalogue entry found for key '{}'", entry_key))
    },
    (Some(only), None) => &only.1,
    (Some(first), Some(second)) => {
      let mut keys: Vec<&str> = std::iter::once(first).chain(std::iter::once(second))
        .chain(of_expected_type)
        .map(|(k, _)| k.as_str())
        .collect();
      keys.sort_unstable();
      return Err(anyhow::anyhow!(
        "Ambiguous catalogue entry key '{}': matches multiple entries ({}) - register it under a more specific key",
        entry_key, keys.join(", ")
      ));
    }
  };

  Ok(entry.clone())
}

/// Remove all entries for a plugin given the plugin name
pub fn remove_plugin_entries(name: &str) {
  trace!("remove_plugin_entries({})", name);

  let prefix = format!("plugin/{}/", name);
  let keys: Vec<String> = {
    let guard = CATALOGUE_REGISTER.lock().unwrap();
    guard.keys()
      .filter(|key| key.starts_with(&prefix))
      .cloned()
      .collect()
  };

  let mut guard = CATALOGUE_REGISTER.lock().unwrap();
  for key in keys {
    guard.remove(&key);
  }

  debug!("Removed all catalogue entries for plugin {}", name);
}

/// Find a content matcher in the global catalogue for the provided content type
#[instrument(level = "trace", skip(content_type))]
pub fn find_content_matcher<CT: Into<String>>(content_type: CT) -> Option<ContentMatcher> {
  let content_type_str = content_type.into();
  debug!("Looking for a content matcher for {}", content_type_str);
  let content_type = match ContentType::parse(content_type_str.as_str()) {
    Ok(ct) => ct,
    Err(err) => {
      error!("'{}' is not a valid content type", err);
      return None;
    }
  };
  let guard = CATALOGUE_REGISTER.lock().unwrap();
  trace!("Catalogue has {} entries", guard.len());
  guard.values().find(|entry| {
    trace!("Catalogue entry {:?}", entry);
    if entry.entry_type == CatalogueEntryType::CONTENT_MATCHER {
      trace!("Catalogue entry is a content matcher for {:?}", entry.values.get("content-types"));
      if let Some(content_types) = entry.values.get("content-types") {
        content_types.split(";").any(|ct| matches_pattern(ct.trim(), &content_type))
      } else {
        false
      }
    } else {
      false
    }
  }).map(|entry| ContentMatcher { catalogue_entry: entry.clone() })
}

/// Checks if a registered content-type pattern matches a content type. The pattern is
/// matched as a regex against the base type (i.e. with any parameters like `charset`
/// stripped), anchored at both ends (the whole base type must match, not just a substring) -
/// this must stay consistent with the equivalent check in the JVM driver's
/// `CatalogueManager.matches`, so that a plugin's catalogue registration behaves the same way
/// regardless of which driver loaded it. Regex metacharacters in a content type (most
/// commonly `+`, as in a `+json`/`+xml` structured syntax suffix) need to be escaped by the
/// plugin author for a literal match.
fn matches_pattern(pattern: &str, content_type: &ContentType) -> bool {
  // Deliberately not `content_type.base_type()`: that replaces the subtype with the
  // structured syntax suffix (e.g. "application/jwt+json" -> "application/json"), which is
  // useful for deciding how to *parse* a body but wrong here - it would make two unrelated
  // "+json" content types register as the same catalogue entry. Just strip attributes
  // (e.g. `charset`), keeping the type/subtype+suffix as the plugin actually registered it.
  let base_type = match &content_type.suffix {
    Some(suffix) => format!("{}/{}+{}", content_type.main_type, content_type.sub_type, suffix),
    None => format!("{}/{}", content_type.main_type, content_type.sub_type)
  };
  match Regex::new(&format!("^(?:{})$", pattern)) {
    Ok(regex) => regex.is_match(base_type.as_str()),
    Err(err) => {
      error!("Failed to parse '{}' as a regex - {}", pattern, err);
      false
    }
  }
}

/// Find a content generator in the global catalogue for the provided content type
pub fn find_content_generator(content_type: &ContentType) -> Option<ContentGenerator> {
  debug!("Looking for a content generator for {}", content_type);
  let guard = CATALOGUE_REGISTER.lock().unwrap();
  guard.values().find(|entry| {
    if entry.entry_type == CatalogueEntryType::CONTENT_GENERATOR {
      if let Some(content_types) = entry.values.get("content-types") {
        content_types.split(";").any(|ct| matches_pattern(ct.trim(), content_type))
      } else {
        false
      }
    } else {
      false
    }
  }).map(|entry| ContentGenerator { catalogue_entry: entry.clone() })
}

/// Returns a copy of all catalogue entries
pub fn all_entries() -> Vec<CatalogueEntry> {
  let guard = CATALOGUE_REGISTER.lock().unwrap();
  guard.values().cloned().collect()
}

/// Returns catalogue entries provided by the core host framework (excludes plugin entries)
pub fn core_entries() -> Vec<CatalogueEntry> {
  let guard = CATALOGUE_REGISTER.lock().unwrap();
  guard.values()
    .filter(|entry| entry.provider_type == CatalogueEntryProviderType::CORE)
    .cloned()
    .collect()
}

#[cfg(test)]
mod tests {
  use expectest::prelude::*;
  use maplit::hashmap;

  use crate::proto::catalogue_entry;

  use super::*;

  #[test]
  fn sets_plugin_catalogue_entries_correctly() {
    // Given
    let manifest = PactPluginManifest {
      name: "sets_plugin_catalogue_entries_correctly".to_string(),
      .. PactPluginManifest::default()
    };
    let entries = vec![
      ProtoCatalogueEntry {
        r#type: catalogue_entry::EntryType::ContentMatcher as i32,
        key: "protobuf".to_string(),
        values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
      },
      ProtoCatalogueEntry {
        r#type: catalogue_entry::EntryType::ContentGenerator as i32,
        key: "protobuf".to_string(),
        values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
      },
      ProtoCatalogueEntry {
        r#type: catalogue_entry::EntryType::Transport as i32,
        key: "grpc".to_string(),
        values: hashmap!{}
      }
    ];

    // When
    register_plugin_entries(&manifest, &entries);

    // Then
    let matcher_entry = lookup_entry("content-matcher/protobuf");
    let generator_entry = lookup_entry("content-generator/protobuf");
    let transport_entry = lookup_entry("transport/grpc");

    remove_plugin_entries("sets_plugin_catalogue_entries_correctly");

    expect!(matcher_entry).to(be_some().value(CatalogueEntry {
      entry_type: CatalogueEntryType::CONTENT_MATCHER,
      provider_type: CatalogueEntryProviderType::PLUGIN,
      plugin: Some(manifest.clone()),
      key: "protobuf".to_string(),
      values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
    }));
    expect!(generator_entry).to(be_some().value(CatalogueEntry {
      entry_type: CatalogueEntryType::CONTENT_GENERATOR,
      provider_type: CatalogueEntryProviderType::PLUGIN,
      plugin: Some(manifest.clone()),
      key: "protobuf".to_string(),
      values: hashmap!{ "content-types".to_string() => "application/protobuf;application/grpc".to_string() }
    }));
    expect!(transport_entry).to(be_some().value(CatalogueEntry {
      entry_type: CatalogueEntryType::TRANSPORT,
      provider_type: CatalogueEntryProviderType::PLUGIN,
      plugin: Some(manifest.clone()),
      key: "grpc".to_string(),
      values: hashmap!{}
    }));
  }

  #[test]
  fn entry_type_proto_values_and_names_round_trip() {
    for entry_type in [
      CatalogueEntryType::CONTENT_MATCHER,
      CatalogueEntryType::CONTENT_GENERATOR,
      CatalogueEntryType::TRANSPORT,
      CatalogueEntryType::MATCHER,
      CatalogueEntryType::INTERACTION,
      CatalogueEntryType::GENERATOR
    ] {
      expect!(CatalogueEntryType::from_proto_value(entry_type.to_proto_value()))
        .to(be_some().value(entry_type));
      expect!(CatalogueEntryType::from_proto_name(entry_type.as_proto_name()))
        .to(be_some().value(entry_type));
      // The Display form round-trips too - it is what catalogue keys are built from
      expect!(CatalogueEntryType::from(entry_type.to_string().as_str())).to(be_equal_to(entry_type));
    }

    expect!(CatalogueEntryType::from_proto_value(99)).to(be_none());
    expect!(CatalogueEntryType::from_proto_name("NOT_AN_ENTRY_TYPE")).to(be_none());
  }

  #[test]
  fn entry_types_shared_with_v1_keep_their_v1_wire_values() {
    // The wire values come from the V2 enum, but the driver publishes one catalogue to every
    // running plugin, V1 ones included - so the values V1 knows about must not shift.
    expect!(CatalogueEntryType::CONTENT_MATCHER.to_proto_value())
      .to(be_equal_to(EntryType::ContentMatcher as i32));
    expect!(CatalogueEntryType::CONTENT_GENERATOR.to_proto_value())
      .to(be_equal_to(EntryType::ContentGenerator as i32));
    expect!(CatalogueEntryType::TRANSPORT.to_proto_value())
      .to(be_equal_to(EntryType::Transport as i32));
    expect!(CatalogueEntryType::MATCHER.to_proto_value())
      .to(be_equal_to(EntryType::Matcher as i32));
    expect!(CatalogueEntryType::INTERACTION.to_proto_value())
      .to(be_equal_to(EntryType::Interaction as i32));
  }

  #[test]
  fn registers_a_generator_entry_under_its_own_entry_type() {
    let name = "registers_a_generator_entry_under_its_own_entry_type";
    let manifest = PactPluginManifest { name: name.to_string(), .. PactPluginManifest::default() };
    // GENERATOR only exists on the V2 enum. This is exactly the case where prost's generated
    // `entry.r#type()` accessor - built against V1 - would report CONTENT_MATCHER instead.
    let entries = vec![
      ProtoCatalogueEntry {
        r#type: CatalogueEntryType::GENERATOR.to_proto_value(),
        key: name.to_string(),
        values: hashmap!{}
      }
    ];

    register_plugin_entries(&manifest, &entries);

    let entry = lookup_entry(&format!("generator/{}", name));
    let as_a_content_matcher = lookup_entry(&format!("content-matcher/{}", name));
    remove_plugin_entries(name);

    expect!(entry.map(|entry| entry.entry_type)).to(be_some().value(CatalogueEntryType::GENERATOR));
    expect!(as_a_content_matcher).to(be_none());
  }

  #[test]
  fn ignores_a_catalogue_entry_whose_type_this_driver_does_not_understand() {
    let name = "ignores_a_catalogue_entry_whose_type_this_driver_does_not_understand";
    let manifest = PactPluginManifest { name: name.to_string(), .. PactPluginManifest::default() };
    let entries = vec![
      ProtoCatalogueEntry { r#type: 99, key: name.to_string(), values: hashmap!{} }
    ];

    register_plugin_entries(&manifest, &entries);

    let registered = all_entries().into_iter().find(|entry| entry.key == name);
    remove_plugin_entries(name);

    expect!(registered).to(be_none());
  }

  #[test]
  fn find_content_matcher_requires_the_whole_base_type_to_match() {
    let manifest = PactPluginManifest {
      name: "find_content_matcher_requires_the_whole_base_type_to_match".to_string(),
      .. PactPluginManifest::default()
    };
    let entries = vec![
      ProtoCatalogueEntry {
        r#type: catalogue_entry::EntryType::ContentMatcher as i32,
        key: "jwt".to_string(),
        // "+" must be escaped, otherwise it's a regex quantifier, not a literal character
        values: hashmap!{ "content-types".to_string() => "application/jwt;application/jwt\\+json".to_string() }
      }
    ];
    register_plugin_entries(&manifest, &entries);

    let exact_match = find_content_matcher("application/jwt+json");
    let with_params = find_content_matcher("application/jwt+json;charset=utf-8");
    let longer_type = find_content_matcher("application/jwt+jsonextra");
    let unrelated_type = find_content_matcher("application/json");

    remove_plugin_entries("find_content_matcher_requires_the_whole_base_type_to_match");

    expect!(exact_match).to(be_some());
    expect!(with_params).to(be_some());
    expect!(longer_type).to(be_none());
    expect!(unrelated_type).to(be_none());
  }

  #[test]
  fn resolve_capability_resolves_an_unambiguous_core_entry() {
    let key = "resolve_capability_resolves_an_unambiguous_core_entry";
    register_core_entries(&vec![CatalogueEntry {
      entry_type: CatalogueEntryType::CONTENT_MATCHER,
      provider_type: CatalogueEntryProviderType::CORE,
      plugin: None,
      key: key.to_string(),
      values: hashmap!{}
    }]);

    let resolved = resolve_capability(key, CatalogueEntryType::CONTENT_MATCHER).unwrap();

    let core_key = match resolved {
      ResolvedCapability::Core(core_key) => core_key,
      ResolvedCapability::Plugin(_) => panic!("expected a Core resolution, got Plugin")
    };
    expect!(core_key).to(be_equal_to(key.to_string()));
  }

  /// The core matcher entries as the Pact frameworks actually register them - keyed by the name
  /// the rule carries in a request (`MatchingRule::name()`), with the specification version it was
  /// introduced in as a value. Kept in sync with `MATCHER_CATALOGUE_ENTRIES` in pact_matching and
  /// `MatcherExecutor.kt` in Pact-JVM.
  fn register_core_matcher_entries() {
    let entries = [("equality", "V1"), ("regex", "V2"), ("type", "V2"), ("min-type", "V2"),
      ("max-type", "V2"), ("min-max-type", "V2"), ("include", "V3"), ("number", "V3"),
      ("integer", "V3"), ("decimal", "V3"), ("null", "V3"), ("date", "V3"), ("time", "V3"),
      ("datetime", "V3"), ("content-type", "V3"), ("values", "V3"), ("array-contains", "V4"),
      ("boolean", "V4"), ("status-code", "V4"), ("not-empty", "V4"), ("semver", "V4"),
      ("each-key", "V4"), ("each-value", "V4")]
      .iter()
      .map(|(key, version)| CatalogueEntry {
        entry_type: CatalogueEntryType::MATCHER,
        provider_type: CatalogueEntryProviderType::CORE,
        plugin: None,
        key: key.to_string(),
        values: hashmap!{ "spec-version".to_string() => version.to_string() }
      })
      .collect();
    register_core_entries(&entries);
  }

  fn resolved_core_key(entry_key: &str) -> String {
    match resolve_capability(entry_key, CatalogueEntryType::MATCHER) {
      Ok(ResolvedCapability::Core(key)) => key,
      other => panic!("expected '{}' to resolve to a core entry, got {:?}", entry_key, other)
    }
  }

  #[test]
  fn resolve_capability_resolves_a_core_rule_by_the_name_it_is_registered_under() {
    register_core_matcher_entries();

    // The name a Pact file (and a plugin calling back) uses - the same string the driver puts in
    // MatchFieldRequest.rule.type, so a plugin can forward a rule it was handed straight back
    expect!(resolved_core_key("type")).to(be_equal_to("type".to_string()));
    expect!(resolved_core_key("regex")).to(be_equal_to("regex".to_string()));
    expect!(resolved_core_key("date")).to(be_equal_to("date".to_string()));
    expect!(resolved_core_key("equality")).to(be_equal_to("equality".to_string()));
    expect!(resolved_core_key("semver")).to(be_equal_to("semver".to_string()));
    expect!(resolved_core_key("not-empty")).to(be_equal_to("not-empty".to_string()));

    // Rules whose name ends in another rule's name are distinct entries, not ambiguous with it
    expect!(resolved_core_key("content-type")).to(be_equal_to("content-type".to_string()));
    expect!(resolved_core_key("min-type")).to(be_equal_to("min-type".to_string()));
    expect!(resolved_core_key("min-max-type")).to(be_equal_to("min-max-type".to_string()));

    // More of the catalogue key resolves the same entry
    expect!(resolved_core_key("core/matcher/date")).to(be_equal_to("date".to_string()));
    expect!(resolved_core_key("matcher/date")).to(be_equal_to("date".to_string()));
  }

  #[test]
  fn resolve_capability_does_not_match_a_key_component_as_a_substring() {
    // "type" names the `type` rule and nothing else - if a component matched by suffix it would
    // also name `content-type`, `min-type` and `max-type`, and be ambiguous across all of them
    expect!(names_catalogue_key("core/matcher/type", "type")).to(be_true());
    expect!(names_catalogue_key("core/matcher/content-type", "type")).to(be_false());
    expect!(names_catalogue_key("core/matcher/type", "matcher/type")).to(be_true());
    expect!(names_catalogue_key("core/matcher/type", "core/matcher/type")).to(be_true());
    expect!(names_catalogue_key("core/matcher/type", "r/type")).to(be_false());
    expect!(names_catalogue_key("core/content-matcher/xml", "xml")).to(be_true());
    expect!(names_catalogue_key("core/content-matcher/xml", "ml")).to(be_false());
  }

  #[test]
  fn lookup_entry_matches_by_name_not_by_substring() {
    let name = "lookup_entry_matches_by_name_not_by_substring";
    let manifest = PactPluginManifest { name: name.to_string(), .. PactPluginManifest::default() };
    register_plugin_entries(&manifest, &vec![
      ProtoCatalogueEntry {
        r#type: CatalogueEntryType::CONTENT_MATCHER.to_proto_value(),
        key: name.to_string(),
        values: hashmap!{}
      },
      ProtoCatalogueEntry {
        r#type: CatalogueEntryType::MATCHER.to_proto_value(),
        key: format!("other-{}", name),
        values: hashmap!{}
      }
    ]);

    let by_name = lookup_entry(name).map(|entry| entry.entry_type);
    let by_components = lookup_entry(&format!("content-matcher/{}", name)).map(|entry| entry.entry_type);
    let fully_qualified = lookup_entry(&format!("plugin/{}/content-matcher/{}", name, name))
      .map(|entry| entry.entry_type);
    // A trailing substring of a component names nothing
    let by_substring = lookup_entry(&name[3..]);

    remove_plugin_entries(name);

    expect!(by_name).to(be_some().value(CatalogueEntryType::CONTENT_MATCHER));
    expect!(by_components).to(be_some().value(CatalogueEntryType::CONTENT_MATCHER));
    expect!(fully_qualified).to(be_some().value(CatalogueEntryType::CONTENT_MATCHER));
    expect!(by_substring).to(be_none());
  }

  #[test]
  fn lookup_entry_finds_a_core_rule_by_name() {
    register_core_matcher_entries();

    expect!(lookup_entry("type").map(|entry| entry.key)).to(be_some().value("type".to_string()));
    expect!(lookup_entry("date").map(|entry| entry.key)).to(be_some().value("date".to_string()));
    expect!(lookup_entry("matcher/date").map(|entry| entry.key)).to(be_some().value("date".to_string()));
    expect!(lookup_entry("core/matcher/date").map(|entry| entry.key)).to(be_some().value("date".to_string()));
  }

  #[test]
  fn resolve_capability_reports_a_plugin_rule_sharing_a_core_rule_name_as_ambiguous() {
    // Core rules are now keyed by the rule name itself, so a plugin registering the same name is a
    // genuine collision. Neither wins silently - both are reachable by a fully qualified key.
    let name = "resolve_capability_reports_a_plugin_rule_sharing_a_core_rule_name_as_ambiguous";
    register_core_entries(&vec![CatalogueEntry {
      entry_type: CatalogueEntryType::MATCHER,
      provider_type: CatalogueEntryProviderType::CORE,
      plugin: None,
      key: name.to_string(),
      values: hashmap!{}
    }]);
    // Before the plugin registers anything, the bare name finds the core rule
    let core_first = resolved_core_key(name);

    let manifest = PactPluginManifest { name: name.to_string(), .. PactPluginManifest::default() };
    register_plugin_entries(&manifest, &vec![ProtoCatalogueEntry {
      r#type: CatalogueEntryType::MATCHER.to_proto_value(),
      key: name.to_string(),
      values: hashmap!{}
    }]);

    let ambiguous = resolve_capability(name, CatalogueEntryType::MATCHER);
    let still_core = resolved_core_key(&format!("core/matcher/{}", name));
    let plugin_rule = resolve_capability(
      &format!("plugin/{}/matcher/{}", name, name), CatalogueEntryType::MATCHER
    );
    remove_plugin_entries(name);

    expect!(core_first).to(be_equal_to(name.to_string()));
    let error = ambiguous.expect_err("expected the shared name to be ambiguous").to_string();
    expect!(error.contains("Ambiguous catalogue entry key")).to(be_true());
    expect!(error.contains(&format!("core/matcher/{}", name))).to(be_true());
    expect!(error.contains(&format!("plugin/{}/matcher/{}", name, name))).to(be_true());
    expect!(still_core).to(be_equal_to(name.to_string()));
    match plugin_rule.expect("expected the plugin's own entry to resolve when fully qualified") {
      ResolvedCapability::Plugin(resolved_manifest) => expect!(resolved_manifest.name).to(be_equal_to(name.to_string())),
      ResolvedCapability::Core(key) => panic!("expected the plugin entry, got core '{}'", key)
    };
  }

  #[test]
  fn resolve_capability_returns_a_clear_error_for_an_unregistered_key() {
    let result = resolve_capability(
      "resolve_capability_returns_a_clear_error_for_an_unregistered_key",
      CatalogueEntryType::CONTENT_MATCHER
    );

    let err = result.expect_err("expected an error for an unregistered key");
    expect!(err.to_string().contains("No catalogue entry found")).to(be_true());
  }

  #[test]
  fn resolve_capability_returns_a_clear_error_for_the_wrong_capability_shape() {
    let key = "resolve_capability_returns_a_clear_error_for_the_wrong_capability_shape";
    register_core_entries(&vec![CatalogueEntry {
      entry_type: CatalogueEntryType::CONTENT_GENERATOR,
      provider_type: CatalogueEntryProviderType::CORE,
      plugin: None,
      key: key.to_string(),
      values: hashmap!{}
    }]);

    let result = resolve_capability(key, CatalogueEntryType::CONTENT_MATCHER);

    let err = result.expect_err("expected an error when the entry is a generator, not a matcher");
    expect!(err.to_string().contains("is a CONTENT_GENERATOR, not a CONTENT_MATCHER")).to(be_true());
  }

  #[test]
  fn resolve_capability_rejects_an_ambiguous_key_shared_by_a_core_and_a_plugin_entry() {
    let key = "resolve_capability_rejects_an_ambiguous_key_shared_by_a_core_and_a_plugin_entry";
    let manifest = PactPluginManifest {
      name: "resolve_capability_rejects_an_ambiguous_key_shared_by_a_core_and_a_plugin_entry".to_string(),
      .. PactPluginManifest::default()
    };
    register_core_entries(&vec![CatalogueEntry {
      entry_type: CatalogueEntryType::CONTENT_MATCHER,
      provider_type: CatalogueEntryProviderType::CORE,
      plugin: None,
      key: key.to_string(),
      values: hashmap!{}
    }]);
    register_plugin_entries(&manifest, &vec![ProtoCatalogueEntry {
      r#type: catalogue_entry::EntryType::ContentMatcher as i32,
      key: key.to_string(),
      values: hashmap!{}
    }]);

    let result = resolve_capability(key, CatalogueEntryType::CONTENT_MATCHER);

    remove_plugin_entries(&manifest.name);

    let err = result.expect_err("expected an error for a key matching more than one entry");
    expect!(err.to_string().contains("Ambiguous catalogue entry key")).to(be_true());
  }
}