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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
//! FFI wrapper code for iterating over Pact interactions
use std::panic::RefUnwindSafe;
use tracing::trace;
use pact_models::message::Message;
use pact_models::message_pact::MessagePact;
use pact_models::v4::pact::V4Pact;
use pact_models::v4::async_message::AsynchronousMessage;
use pact_models::v4::sync_message::SynchronousMessage;
use pact_models::v4::synch_http::SynchronousHttp;
use pact_models::v4::V4InteractionType;
use crate::{as_mut, ffi_fn};
use crate::models::PactInteraction;
use crate::ptr;
ffi_fn! {
/// Free the iterator when you're done using it.
fn pactffi_pact_message_iter_delete(iter: *mut PactMessageIterator) {
ptr::drop_raw(iter);
}
}
/// An iterator over messages in a pact.
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct PactMessageIterator {
current: usize,
message_pact: MessagePact,
}
impl PactMessageIterator {
/// Create a new iterator given a message pact
pub fn new(message_pact: MessagePact) -> Self {
PactMessageIterator {
current: 0,
message_pact
}
}
/// Get the next message in the message pact.
fn next(&mut self) -> Option<&mut Message> {
let idx = self.current;
self.current += 1;
self.message_pact.messages.get_mut(idx)
}
}
ffi_fn! {
/// Get the next message from the message pact. As the messages returned are owned by the
/// iterator, they do not need to be deleted but will be cleaned up when the iterator is
/// deleted.
///
/// Will return a NULL pointer when the iterator has advanced past the end of the list.
///
/// # Safety
///
/// This function is safe.
///
/// Deleting a message returned by the iterator can lead to undefined behaviour.
///
/// # Error Handling
///
/// This function will return a NULL pointer if passed a NULL pointer or if an error occurs.
fn pactffi_pact_message_iter_next(iter: *mut PactMessageIterator) -> *mut Message {
let iter = as_mut!(iter);
match iter.next() {
Some(message) => message as *mut Message,
None => {
trace!("iter past the end of messages");
std::ptr::null_mut()
}
}
} {
std::ptr::null_mut()
}
}
/// An iterator over asynchronous messages in a V4 pact.
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct PactAsyncMessageIterator {
current: usize,
messages: Vec<AsynchronousMessage>
}
impl PactAsyncMessageIterator {
/// Create a new iterator over all asynchronous messages in the pact
pub fn new(pact: V4Pact) -> Self {
PactAsyncMessageIterator {
current: 0,
messages: pact.filter_interactions(V4InteractionType::Asynchronous_Messages)
.iter()
.map(|i| i.as_v4_async_message().unwrap())
.collect()
}
}
/// Get the next message in the pact.
fn next(&mut self) -> Option<&mut AsynchronousMessage> {
let idx = self.current;
self.current += 1;
self.messages.get_mut(idx)
}
}
ffi_fn! {
/// Get the next asynchronous from the V4 pact. As the messages returned are
/// owned by the iterator, they do not need to be deleted but will be
/// cleaned up when the iterator is deleted.
///
/// Will return a NULL pointer when the iterator has advanced past the end
/// of the list.
///
/// # Safety
///
/// This function is safe.
///
/// Deleting a message returned by the iterator can lead to undefined
/// behaviour.
///
/// # Error Handling
///
/// This function will return a NULL pointer if passed a NULL pointer or if
/// an error occurs.
fn pactffi_pact_async_message_iter_next(iter: *mut PactAsyncMessageIterator) -> *mut AsynchronousMessage {
let iter = as_mut!(iter);
match iter.next() {
Some(message) => message as *mut AsynchronousMessage,
None => {
trace!("iter past the end of messages");
std::ptr::null_mut()
}
}
} {
std::ptr::null_mut()
}
}
ffi_fn! {
/// Free the iterator when you're done using it.
fn pactffi_pact_async_message_iter_delete(iter: *mut PactAsyncMessageIterator) {
ptr::drop_raw(iter);
}
}
/// An iterator over synchronous request/response messages in a V4 pact.
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct PactSyncMessageIterator {
current: usize,
messages: Vec<SynchronousMessage>
}
impl PactSyncMessageIterator {
/// Create a new iterator over all synchronous request/response messages in the pact
pub fn new(pact: V4Pact) -> Self {
PactSyncMessageIterator {
current: 0,
messages: pact.filter_interactions(V4InteractionType::Synchronous_Messages)
.iter()
.map(|i| i.as_v4_sync_message().unwrap())
.collect()
}
}
/// Get the next message in the pact.
fn next(&mut self) -> Option<&mut SynchronousMessage> {
let idx = self.current;
self.current += 1;
self.messages.get_mut(idx)
}
}
ffi_fn! {
/// Get the next synchronous request/response message from the V4 pact. As the messages returned are owned by the
/// iterator, they do not need to be deleted but will be cleaned up when the iterator is
/// deleted.
///
/// Will return a NULL pointer when the iterator has advanced past the end of the list.
///
/// # Safety
///
/// This function is safe.
///
/// Deleting a message returned by the iterator can lead to undefined behaviour.
///
/// # Error Handling
///
/// This function will return a NULL pointer if passed a NULL pointer or if an error occurs.
fn pactffi_pact_sync_message_iter_next(iter: *mut PactSyncMessageIterator) -> *mut SynchronousMessage {
let iter = as_mut!(iter);
match iter.next() {
Some(message) => message as *mut SynchronousMessage,
None => {
trace!("iter past the end of messages");
std::ptr::null_mut()
}
}
} {
std::ptr::null_mut()
}
}
ffi_fn! {
/// Free the iterator when you're done using it.
fn pactffi_pact_sync_message_iter_delete(iter: *mut PactSyncMessageIterator) {
ptr::drop_raw(iter);
}
}
/// An iterator over synchronous HTTP request/response interactions in a V4 pact.
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct PactSyncHttpIterator {
current: usize,
interactions: Vec<SynchronousHttp>
}
impl PactSyncHttpIterator {
/// Create a new iterator over all synchronous HTTP request/response interactions in the V4 pact
pub fn new(pact: V4Pact) -> Self {
PactSyncHttpIterator {
current: 0,
interactions: pact.filter_interactions(V4InteractionType::Synchronous_HTTP)
.iter()
.map(|i| i.as_v4_http().unwrap())
.collect()
}
}
/// Get the next interaction in the pact.
fn next(&mut self) -> Option<&mut SynchronousHttp> {
let idx = self.current;
self.current += 1;
self.interactions.get_mut(idx)
}
}
ffi_fn! {
/// Get the next synchronous HTTP request/response interaction from the V4 pact. As the
/// interactions returned are owned by the iterator, they do not need to be deleted but
/// will be cleaned up when the iterator is deleted.
///
/// Will return a NULL pointer when the iterator has advanced past the end of the list.
///
/// # Safety
///
/// This function is safe.
///
/// Deleting an interaction returned by the iterator can lead to undefined behaviour.
///
/// # Error Handling
///
/// This function will return a NULL pointer if passed a NULL pointer or if an error occurs.
fn pactffi_pact_sync_http_iter_next(iter: *mut PactSyncHttpIterator) -> *mut SynchronousHttp {
let iter = as_mut!(iter);
match iter.next() {
Some(interaction) => interaction as *mut SynchronousHttp,
None => {
trace!("iter past the end of the list");
std::ptr::null_mut()
}
}
} {
std::ptr::null_mut()
}
}
ffi_fn! {
/// Free the iterator when you're done using it.
fn pactffi_pact_sync_http_iter_delete(iter: *mut PactSyncHttpIterator) {
ptr::drop_raw(iter);
}
}
/// An iterator over the interactions in a pact.
#[derive(Debug)]
#[allow(missing_copy_implementations)]
pub struct PactInteractionIterator {
current: usize,
interactions: Vec<PactInteraction>
}
impl PactInteractionIterator {
/// Create a new iterator over all the interactions in the pact
pub fn new(pact: Box<dyn pact_models::pact::Pact + Send + Sync + RefUnwindSafe>) -> Self {
PactInteractionIterator {
current: 0,
interactions: pact.interactions().iter()
.map(|i| PactInteraction::new(i))
.collect()
}
}
/// Get the next interaction in the pact.
fn next(&mut self) -> Option<&PactInteraction> {
let idx = self.current;
self.current += 1;
self.interactions.get(idx)
}
}
ffi_fn! {
/// Get the next interaction from the pact. As the interactions returned are owned by the
/// iterator, they do not need to be deleted but will be cleaned up when the iterator is
/// deleted.
///
/// Will return a NULL pointer when the iterator has advanced past the end of the list.
///
/// # Safety
///
/// This function is safe.
///
/// Deleting an interaction returned by the iterator can lead to undefined behaviour.
///
/// # Error Handling
///
/// This function will return a NULL pointer if passed a NULL pointer or if an error occurs.
fn pactffi_pact_interaction_iter_next(iter: *mut PactInteractionIterator) -> *const PactInteraction {
let iter = as_mut!(iter);
match iter.next() {
Some(interaction) => interaction as *const PactInteraction,
None => {
trace!("iter past the end of messages");
std::ptr::null()
}
}
} {
std::ptr::null()
}
}
ffi_fn! {
/// Free the iterator when you're done using it.
fn pactffi_pact_interaction_iter_delete(iter: *mut PactInteractionIterator) {
ptr::drop_raw(iter);
}
}