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
use std::fmt::{Display, Formatter};
use std::fmt;
use anyhow::anyhow;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
pub mod content_types;
pub mod bodies;
pub mod v4;
pub mod provider_states;
#[cfg_attr(feature = "ffi", repr(C))]
#[derive(Debug, Clone, PartialEq, PartialOrd, Deserialize, Serialize)]
#[allow(non_camel_case_types)]
pub enum PactSpecification {
Unknown,
V1,
V1_1,
V2,
V3,
V4
}
impl Default for PactSpecification {
fn default() -> Self {
PactSpecification::Unknown
}
}
impl PactSpecification {
pub fn version_str(&self) -> String {
match *self {
PactSpecification::V1 => "1.0.0",
PactSpecification::V1_1 => "1.1.0",
PactSpecification::V2 => "2.0.0",
PactSpecification::V3 => "3.0.0",
PactSpecification::V4 => "4.0",
_ => "unknown"
}.into()
}
pub fn to_string(&self) -> String {
match *self {
PactSpecification::V1 => "V1",
PactSpecification::V1_1 => "V1.1",
PactSpecification::V2 => "V2",
PactSpecification::V3 => "V3",
PactSpecification::V4 => "V4",
_ => "unknown"
}.into()
}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq)]
pub struct Consumer {
pub name: String
}
impl Consumer {
pub fn from_json(pact_json: &Value) -> Consumer {
let val = match pact_json.get("name") {
Some(v) => match v.clone() {
Value::String(s) => s,
_ => v.to_string()
},
None => "consumer".to_string()
};
Consumer { name: val.clone() }
}
pub fn to_json(&self) -> Value {
json!({ "name" : self.name })
}
}
#[derive(Debug, Clone, Default, Deserialize, Serialize, PartialEq)]
pub struct Provider {
pub name: String
}
impl Provider {
pub fn from_json(pact_json: &Value) -> Provider {
let val = match pact_json.get("name") {
Some(v) => match v.clone() {
Value::String(s) => s,
_ => v.to_string()
},
None => "provider".to_string()
};
Provider { name: val.clone() }
}
pub fn to_json(&self) -> Value {
json!({ "name" : self.name })
}
}
#[derive(PartialEq, Debug, Clone, Eq)]
pub enum DifferenceType {
Method,
Path,
Headers,
QueryParameters,
Body,
MatchingRules,
Status
}
#[derive(Debug, Clone, Deserialize, Serialize, Ord, PartialOrd, Eq, PartialEq)]
pub enum HttpStatus {
Information,
Success,
Redirect,
ClientError,
ServerError,
StatusCodes(Vec<u16>),
NonError,
Error
}
impl HttpStatus {
pub fn from_json(value: &Value) -> anyhow::Result<Self> {
match value {
Value::String(s) => match s.as_str() {
"info" => Ok(HttpStatus::Information),
"success" => Ok(HttpStatus::Success),
"redirect" => Ok(HttpStatus::Redirect),
"clientError" => Ok(HttpStatus::ClientError),
"serverError" => Ok(HttpStatus::ServerError),
"nonError" => Ok(HttpStatus::NonError),
"error" => Ok(HttpStatus::Error),
_ => Err(anyhow!("'{}' is not a valid value for an HTTP Status", s))
},
Value::Array(a) => {
let status_codes = a.iter().map(|status| match status {
Value::Number(n) => if n.is_u64() {
Ok(n.as_u64().unwrap() as u16)
} else if n.is_i64() {
Ok(n.as_i64().unwrap() as u16)
} else {
Ok(n.as_f64().unwrap() as u16)
},
Value::String(s) => s.parse::<u16>().map_err(|err| anyhow!(err)),
_ => Err(anyhow!("'{}' is not a valid JSON value for an HTTP Status", status))
}).collect::<Vec<anyhow::Result<u16>>>();
if status_codes.iter().any(|it| it.is_err()) {
Err(anyhow!("'{}' is not a valid JSON value for an HTTP Status", value))
} else {
Ok(HttpStatus::StatusCodes(status_codes.iter().map(|code| *code.as_ref().unwrap()).collect()))
}
}
_ => Err(anyhow!("'{}' is not a valid JSON value for an HTTP Status", value))
}
}
pub fn to_json(&self) -> Value {
match self {
HttpStatus::StatusCodes(codes) => json!(codes),
_ => json!(self.to_string())
}
}
}
impl Display for HttpStatus {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
HttpStatus::Information => write!(f, "Informational response (100–199)"),
HttpStatus::Success => write!(f, "Successful response (200–299)"),
HttpStatus::Redirect => write!(f, "Redirect (300–399)"),
HttpStatus::ClientError => write!(f, "Client error (400–499)"),
HttpStatus::ServerError => write!(f, "Server error (500–599)"),
HttpStatus::StatusCodes(status) =>
write!(f, "{}", status.iter().map(|s| s.to_string()).join(", ")),
HttpStatus::NonError => write!(f, "Non-error response (< 400)"),
HttpStatus::Error => write!(f, "Error response (>= 400)")
}
}
}
#[cfg(test)]
mod tests;