pact_consumer/patterns/mod.rs
1//! JSON "patterns", which can be used to either generate JSON documents or
2//! match them.
3
4use pact_models::matchingrules::MatchingRuleCategory;
5use pact_models::path_exp::DocPath;
6use std::fmt::Debug;
7
8#[macro_use] mod json_macros;
9mod json_pattern;
10#[macro_use] mod special_rules;
11mod string_pattern;
12#[cfg(feature = "datetime")] #[macro_use] mod date_time;
13
14pub use self::json_pattern::*;
15pub use self::special_rules::*;
16pub use self::string_pattern::*;
17#[cfg(feature = "datetime")] pub use self::date_time::*;
18
19/// Abstract interface to types which can:
20///
21/// 1. Generate example data.
22/// 2. Match data returned by tests in various flexible ways, for example,
23/// accepting all strings which match a regular expression.
24///
25/// For an overview of how the matching rules work, and what kinds of special
26/// matching rules exist, see the [`pact_matching` documentation][spec].
27///
28/// The current version of this API will only work for `JsonPattern` and
29/// `serde_json::Value`. Extending this scheme to work for XML would require
30/// parameterizing the input and output types, and possibly other changes.
31///
32/// [spec]: https://docs.rs/pact_matching/0.2.2/pact_matching/
33pub trait Pattern: Debug {
34 /// What type of data can this pattern be matched against? What kind of
35 /// example data does it generate?
36 type Matches;
37
38 /// Convert this `Matchable` into an example data value, stripping out
39 /// any special match rules.
40 fn to_example(&self) -> Self::Matches;
41
42 /// Convert this `Matchable` into an example data value as byte slice.
43 fn to_example_bytes(&self) -> Vec<u8>;
44
45 /// Extract the matching rules from this `Matchable`, and insert them into
46 /// `rules_out`, using `path` as the base path.
47 ///
48 /// This API corresponds to the [`Extract` code in Ruby][ruby].
49 ///
50 /// (The `path` parameter is represented as a `&str` here, which forces each
51 /// recursive call to allocate strings. We could optimize this by using a
52 /// custom `path` representation which worked like a stack-based linked list
53 /// stored in reverse order, but that would add significant complexity.)
54 ///
55 /// [ruby]:
56 /// https://github.com/pact-foundation/pact-support/blob/master/lib/pact/matching_rules/extract.rb
57 fn extract_matching_rules(&self, path: DocPath, rules_out: &mut MatchingRuleCategory);
58}