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
//! FFI functions to support Pact interaction models.
use pact_models::message::Message;
use pact_models::v4::async_message::AsynchronousMessage;
use pact_models::v4::sync_message::SynchronousMessage;
use pact_models::v4::synch_http::SynchronousHttp;
use crate::{as_ref, ffi_fn};
use crate::models::PactInteraction;
use crate::util::ptr;
ffi_fn! {
/// Casts this interaction to a `SynchronousHttp` interaction. Returns a NULL pointer if the
/// interaction can not be casted to a `SynchronousHttp` interaction (for instance, it is a
/// message interaction). The returned pointer must be freed with `pactffi_sync_http_delete`
/// when no longer required.
///
/// # Safety
/// This function is safe as long as the interaction pointer is a valid pointer.
///
/// # Errors
/// On any error, this function will return a NULL pointer.
fn pactffi_pact_interaction_as_synchronous_http(interaction: *const PactInteraction) -> *const SynchronousHttp {
let interaction = as_ref!(interaction);
let inner = interaction.inner.lock().unwrap();
if let Some(http) = inner.as_v4_http() {
ptr::raw_to(http)
} else {
std::ptr::null()
}
} {
std::ptr::null()
}
}
ffi_fn! {
/// Casts this interaction to a `Message` interaction. Returns a NULL pointer if the
/// interaction can not be casted to a `Message` interaction (for instance, it is a
/// http interaction). The returned pointer must be freed with `pactffi_message_delete`
/// when no longer required.
///
/// Note that if the interaction is a V4 `AsynchronousMessage`, it will be converted to a V3
/// `Message` before being returned.
///
/// # Safety
/// This function is safe as long as the interaction pointer is a valid pointer.
///
/// # Errors
/// On any error, this function will return a NULL pointer.
fn pactffi_pact_interaction_as_message(interaction: *const PactInteraction) -> *const Message {
let interaction = as_ref!(interaction);
let inner = interaction.inner.lock().unwrap();
if let Some(message) = inner.as_message() {
ptr::raw_to(message)
} else {
std::ptr::null()
}
} {
std::ptr::null()
}
}
ffi_fn! {
/// Casts this interaction to a `AsynchronousMessage` interaction. Returns a NULL pointer if the
/// interaction can not be casted to a `AsynchronousMessage` interaction (for instance, it is a
/// http interaction). The returned pointer must be freed with `pactffi_async_message_delete`
/// when no longer required.
///
/// Note that if the interaction is a V3 `Message`, it will be converted to a V4
/// `AsynchronousMessage` before being returned.
///
/// # Safety
/// This function is safe as long as the interaction pointer is a valid pointer.
///
/// # Errors
/// On any error, this function will return a NULL pointer.
fn pactffi_pact_interaction_as_asynchronous_message(interaction: *const PactInteraction) -> *const AsynchronousMessage {
let interaction = as_ref!(interaction);
let inner = interaction.inner.lock().unwrap();
if let Some(message) = inner.as_v4_async_message() {
ptr::raw_to(message)
} else {
std::ptr::null()
}
} {
std::ptr::null()
}
}
ffi_fn! {
/// Casts this interaction to a `SynchronousMessage` interaction. Returns a NULL pointer if the
/// interaction can not be casted to a `SynchronousMessage` interaction (for instance, it is a
/// http interaction). The returned pointer must be freed with `pactffi_sync_message_delete`
/// when no longer required.
///
/// # Safety
/// This function is safe as long as the interaction pointer is a valid pointer.
///
/// # Errors
/// On any error, this function will return a NULL pointer.
fn pactffi_pact_interaction_as_synchronous_message(interaction: *const PactInteraction) -> *const SynchronousMessage {
let interaction = as_ref!(interaction);
let inner = interaction.inner.lock().unwrap();
if let Some(message) = inner.as_v4_sync_message() {
ptr::raw_to(message)
} else {
std::ptr::null()
}
} {
std::ptr::null()
}
}