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
use std::fmt::{self, Debug, Display, Formatter};
use std::sync::{Arc, Mutex};
use serde_json::Value;
use crate::bodies::OptionalBody;
use crate::content_types::ContentType;
use crate::matchingrules::MatchingRules;
use crate::message::Message;
use crate::PactSpecification;
use crate::provider_states::ProviderState;
use crate::sync_interaction::RequestResponseInteraction;
use crate::v4::async_message::AsynchronousMessage;
use crate::v4::interaction::{interaction_from_json, V4Interaction};
use crate::v4::sync_message::SynchronousMessage;
use crate::v4::synch_http::SynchronousHttp;
#[derive(Debug, Clone)]
pub struct PactConflict {
pub interaction: String,
pub description: String
}
pub trait Interaction: Debug {
fn type_of(&self) -> String;
fn is_request_response(&self) -> bool;
fn as_request_response(&self) -> Option<RequestResponseInteraction>;
fn is_message(&self) -> bool;
fn as_message(&self) -> Option<Message>;
fn id(&self) -> Option<String>;
fn description(&self) -> String;
fn set_id(&mut self, id: Option<String>);
fn set_description(&mut self, description: &str);
fn provider_states(&self) -> Vec<ProviderState>;
fn provider_states_mut(&mut self) -> &mut Vec<ProviderState>;
#[deprecated(
since = "0.1.0",
note = "Some interactions have multiple contents (like request/response), so it is impossible \
to know which to return for this method"
)]
fn contents(&self) -> OptionalBody;
fn contents_for_verification(&self) -> OptionalBody;
#[deprecated(
since = "0.1.0",
note = "Some interactions have multiple contents (like request/response), so it is impossible \
to know which to return for this method"
)]
fn content_type(&self) -> Option<ContentType>;
fn is_v4(&self) -> bool;
fn as_v4(&self) -> Option<Box<dyn V4Interaction>>;
fn as_v4_mut(&mut self) -> Option<&mut dyn V4Interaction>;
fn is_v4_http(&self) -> bool { false }
fn as_v4_http(&self) -> Option<SynchronousHttp>;
fn as_v4_async_message(&self) -> Option<AsynchronousMessage>;
fn is_v4_async_message(&self) -> bool { false }
fn as_v4_sync_message(&self) -> Option<SynchronousMessage>;
fn as_v4_http_mut(&mut self) -> Option<&mut SynchronousHttp>;
fn is_v4_sync_message(&self) -> bool { false }
fn as_v4_async_message_mut(&mut self) -> Option<&mut AsynchronousMessage>;
fn as_v4_sync_message_mut(&mut self) -> Option<&mut SynchronousMessage>;
fn boxed(&self) -> Box<dyn Interaction + Send + Sync>;
fn arced(&self) -> Arc<dyn Interaction + Send + Sync>;
fn thread_safe(&self) -> Arc<Mutex<dyn Interaction + Send + Sync>>;
#[deprecated(
since = "0.2.1",
note = "Some interactions have multiple contents (like request/response), so it is impossible \
to know which to return for this method"
)]
fn matching_rules(&self) -> Option<MatchingRules>;
fn pending(&self) -> bool { false }
}
impl Display for dyn Interaction {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
if let Some(req_res) = self.as_request_response() {
std::fmt::Display::fmt(&req_res, f)
} else if let Some(mp) = self.as_message() {
std::fmt::Display::fmt(&mp, f)
} else if let Some(mp) = self.as_v4_http() {
std::fmt::Display::fmt(&mp, f)
} else if let Some(mp) = self.as_v4_async_message() {
std::fmt::Display::fmt(&mp, f)
} else {
Err(fmt::Error)
}
}
}
impl Clone for Box<dyn Interaction> {
fn clone(&self) -> Self {
if self.is_v4() {
if let Some(http) = self.as_v4_http() {
Box::new(http)
} else if let Some(message) = self.as_v4_async_message() {
Box::new(message)
} else {
panic!("Internal Error - Tried to clone an interaction that was not valid")
}
} else if let Some(req_res) = self.as_request_response() {
Box::new(req_res)
} else if let Some(mp) = self.as_message() {
Box::new(mp)
} else {
panic!("Internal Error - Tried to clone an interaction that was not valid")
}
}
}
pub fn http_interaction_from_json(source: &str, json: &Value, spec: &PactSpecification) -> anyhow::Result<Box<dyn Interaction + Send + Sync>> {
match spec {
PactSpecification::V4 => interaction_from_json(source, 0, json)
.map(|i| i.boxed()),
_ => Ok(Box::new(RequestResponseInteraction::from_json(0, json, spec)?))
}
}
pub fn message_interaction_from_json(source: &str, json: &Value, spec: &PactSpecification) -> anyhow::Result<Box<dyn Interaction + Send + Sync>> {
match spec {
PactSpecification::V4 => interaction_from_json(source, 0, json)
.map(|i| i.boxed()),
_ => Message::from_json(0, json, spec).map(|i| i.boxed())
}
}
pub(crate) fn parse_interactions(pact_json: &Value, spec_version: PactSpecification
) -> anyhow::Result<Vec<RequestResponseInteraction>> {
if let Some(&Value::Array(ref array)) = pact_json.get("interactions") {
array.iter().enumerate().map(|(index, ijson)| {
RequestResponseInteraction::from_json(index, ijson, &spec_version)
}).collect()
}
else {
Ok(vec![])
}
}