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
use pact_matching::models::{RequestResponsePact, Consumer, Provider, RequestResponseInteraction};
use lazy_static::*;
use maplit::*;
use std::sync::Mutex;
use std::cell::RefCell;
use std::collections::HashMap;
lazy_static! {
static ref PACT_HANDLES: Mutex<HashMap<usize, RefCell<RequestResponsePact>>> = Mutex::new(hashmap![]);
}
#[repr(C)]
#[derive(Debug, Clone)]
pub struct PactHandle {
pub pact: usize
}
#[repr(C)]
#[derive(Debug, Clone)]
pub struct InteractionHandle {
pub pact: usize,
pub interaction: usize
}
#[repr(C)]
#[derive(Debug, Clone)]
pub enum InteractionPart {
Request,
Response
}
impl PactHandle {
pub fn new(consumer: &str, provider: &str) -> Self {
let mut handles = PACT_HANDLES.lock().unwrap();
let id = handles.len() + 1;
handles.insert(id, RefCell::new(RequestResponsePact {
consumer: Consumer { name: consumer.to_string() },
provider: Provider { name: provider.to_string() },
.. RequestResponsePact::default()
}));
PactHandle {
pact: id
}
}
pub fn with_pact<R>(&self, f: &dyn Fn(usize, &mut RequestResponsePact) -> R) -> Option<R> {
let mut handles = PACT_HANDLES.lock().unwrap();
handles.get_mut(&self.pact).map(|inner| f(self.pact - 1, &mut inner.borrow_mut()))
}
}
impl InteractionHandle {
pub fn new(pact: PactHandle, interaction: usize) -> InteractionHandle {
InteractionHandle {
pact: pact.pact,
interaction
}
}
pub fn with_pact<R>(&self, f: &dyn Fn(usize, &mut RequestResponsePact) -> R) -> Option<R> {
let mut handles = PACT_HANDLES.lock().unwrap();
handles.get_mut(&self.pact).map(|inner| f(self.pact - 1, &mut inner.borrow_mut()))
}
pub fn with_interaction<R>(&self, f: &dyn Fn(usize, &mut RequestResponseInteraction) -> R) -> Option<R> {
let mut handles = PACT_HANDLES.lock().unwrap();
handles.get_mut(&self.pact).map(|inner| {
match inner.borrow_mut().interactions.get_mut(self.interaction - 1) {
Some(inner_i) => Some(f(self.interaction - 1, inner_i)),
None => None
}
}).flatten()
}
}