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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//! A crate exposing the `pact` APIs to other languages
//! via a C Foreign Function Interface.

#![warn(missing_docs)]
#![warn(missing_debug_implementations)]
#![warn(missing_copy_implementations)]

use std::ffi::CStr;
use std::panic::RefUnwindSafe;
use std::str::FromStr;

use lazy_static::lazy_static;
use libc::c_char;
use tracing::{debug, error, info, trace, warn};
use tracing_core::{Level, LevelFilter};
use tracing_log::AsLog;
use tracing_subscriber::FmtSubscriber;

use models::message::Message;
use pact_matching as pm;
pub use pact_matching::Mismatch;
use pact_models::interaction::Interaction;
use pact_models::pact::Pact;
use pact_models::v4::pact::V4Pact;

use crate::util::*;

pub mod error;
pub mod log;
pub mod models;
pub(crate) mod util;
pub mod mock_server;
pub mod verifier;
pub mod plugins;
pub mod matching;

const VERSION: &str = concat!(env!("CARGO_PKG_VERSION"), "\0");

// Create a global runtime of all async tasks
lazy_static! {
  static ref RUNTIME: tokio::runtime::Runtime = tokio::runtime::Builder::new_multi_thread()
          .enable_all()
          .build()
          .expect("Could not start a Tokio runtime for running async tasks");
}

/// Returns the current library version
#[no_mangle]
pub extern "C" fn pactffi_version() -> *const c_char {
    VERSION.as_ptr() as *const c_char
}

/// Initialise the mock server library, can provide an environment variable name to use to
/// set the log levels. This function should only be called once, as it tries to install a global
/// tracing subscriber.
///
/// # Safety
///
/// log_env_var must be a valid NULL terminated UTF-8 string.
#[no_mangle]
pub unsafe extern fn pactffi_init(log_env_var: *const c_char) {
    let log_env_var = if !log_env_var.is_null() {
        let c_str = CStr::from_ptr(log_env_var);
        match c_str.to_str() {
            Ok(str) => str,
            Err(err) => {
                warn!("Failed to parse the environment variable name as a UTF-8 string: {}", err);
                "LOG_LEVEL"
            }
        }
    } else {
        "LOG_LEVEL"
    };

    let subscriber = FmtSubscriber::builder()
      .with_env_filter(log_env_var)
      .with_thread_names(true)
      .with_ansi(false) // Pact .Net can't deal with ANSI escape codes
      .finish();
    if let Err(err) = tracing::subscriber::set_global_default(subscriber) {
      eprintln!("Failed to initialise global tracing subscriber - {err}");
    };
}

/// Initialises logging, and sets the log level explicitly. This function should only be called
/// once, as it tries to install a global tracing subscriber.
///
/// * `log_level` - String. One of TRACE, DEBUG, INFO, WARN, ERROR, NONE/OFF. Case-insensitive. Defaults to INFO.
///
/// # Safety
///
/// Exported functions are inherently unsafe.
#[no_mangle]
pub unsafe extern "C" fn pactffi_init_with_log_level(level: *const c_char) {
  let log_level = log_level_filter_from_c_char(level);
  let subscriber = FmtSubscriber::builder()
    .with_max_level(log_level)
    .with_thread_names(true)
    .with_ansi(false) // Pact .Net can't deal with ANSI escape codes
    .finish();
  if let Err(err) = tracing::subscriber::set_global_default(subscriber) {
    eprintln!("Failed to initialise global tracing subscriber - {err}");
  };
}

/// Enable ANSI coloured output on Windows. On non-Windows platforms, this function is a no-op.
///
/// # Safety
///
/// This function is safe.
#[no_mangle]
#[cfg(windows)]
pub extern "C" fn pactffi_enable_ansi_support() {
  if let Err(err) = ansi_term::enable_ansi_support() {
    warn!("Could not enable ANSI console support - {err}");
  }
}

/// Enable ANSI coloured output on Windows. On non-Windows platforms, this function is a no-op.
///
/// # Safety
///
/// This function is safe.
#[no_mangle]
#[cfg(not(windows))]
pub extern "C" fn pactffi_enable_ansi_support() { }

/// Log using the shared core logging facility.
///
/// This is useful for callers to have a single set of logs.
///
/// * `source` - String. The source of the log, such as the class or caller framework to
///                      disambiguate log lines from the rust logging (e.g. pact_go)
/// * `log_level` - String. One of TRACE, DEBUG, INFO, WARN, ERROR. Case-insensitive. Defaults to INFO.
/// * `message` - Message to log
///
/// # Safety
/// This function will fail if any of the pointers passed to it are invalid.
#[no_mangle]
pub unsafe extern "C" fn pactffi_log_message(source: *const c_char, log_level: *const c_char, message: *const c_char) {
  let target = convert_cstr("target", source).unwrap_or("client");

  if !message.is_null() {
    if let Some(message) = convert_cstr("message", message) {
      match log_level_from_c_char(log_level).as_log() {
        ::log::Level::Error => error!(source = target, "{}", message),
        ::log::Level::Warn => warn!(source = target, "{}", message),
        ::log::Level::Info => info!(source = target, "{}", message),
        ::log::Level::Debug => debug!(source = target, "{}", message),
        ::log::Level::Trace => trace!(source = target, "{}", message)
      }
    }
  }
}

unsafe fn log_level_from_c_char(log_level: *const c_char) -> Level {
  if !log_level.is_null() {
    let level = convert_cstr("log_level", log_level).unwrap_or("INFO");
    Level::from_str(level).unwrap_or(tracing::Level::INFO)
  } else {
    Level::INFO
  }
}

unsafe fn log_level_filter_from_c_char(log_level: *const c_char) -> LevelFilter {
  if !log_level.is_null() {
    let level = convert_cstr("log_level", log_level).unwrap_or("INFO");
    match level.to_lowercase().as_str() {
      "none" => LevelFilter::OFF,
      _ => LevelFilter::from_str(level).unwrap_or(LevelFilter::INFO)
    }
  } else {
    LevelFilter::INFO
  }
}

fn convert_cstr(name: &str, value: *const c_char) -> Option<&str> {
    unsafe {
        if value.is_null() {
            warn!("{} is NULL!", name);
            None
        } else {
            let c_str = CStr::from_ptr(value);
            match c_str.to_str() {
                Ok(str) => Some(str),
                Err(err) => {
                    warn!("Failed to parse {} name as a UTF-8 string: {}", name, err);
                    None
                }
            }
        }
    }
}

ffi_fn! {
    /// Match a pair of messages, producing a collection of mismatches,
    /// which is empty if the two messages matched.
    fn pactffi_match_message(msg_1: *const Message, msg_2: *const Message) -> *const Mismatches {
        let msg_1: Box<dyn Interaction + Send + Sync + RefUnwindSafe> = unsafe { Box::from_raw(msg_1 as *mut Message) };
        let msg_2: Box<dyn Interaction + Send + Sync + RefUnwindSafe> = unsafe { Box::from_raw(msg_2 as *mut Message) };

        let mismatches = RUNTIME.block_on(async move {
            // TODO: match_message also requires the Pact that the messages belong to
            Mismatches(pm::match_message(&msg_1, &msg_2, &V4Pact::default().boxed()).await)
        });

        ptr::raw_to(mismatches) as *const Mismatches
    } {
        std::ptr::null() as *const Mismatches
    }
}

ffi_fn! {
    /// Get an iterator over mismatches.
    fn pactffi_mismatches_get_iter(mismatches: *const Mismatches) -> *mut MismatchesIterator {
        let mismatches = as_ref!(mismatches);
        let iter = MismatchesIterator { current: 0, mismatches };
        ptr::raw_to(iter)
    } {
        std::ptr::null_mut()
    }
}

ffi_fn! {
    /// Delete mismatches
    fn pactffi_mismatches_delete(mismatches: *const Mismatches) {
        ptr::drop_raw(mismatches as *mut Mismatches);
    }
}

ffi_fn! {
    /// Get the next mismatch from a mismatches iterator.
    ///
    /// Returns a null pointer if no mismatches remain.
    fn pactffi_mismatches_iter_next(iter: *mut MismatchesIterator) -> *const Mismatch {
        let iter = as_mut!(iter);
        let mismatches = as_ref!(iter.mismatches);
        let index = iter.next();
        match mismatches.0.get(index) {
          Some(mismatch) => mismatch as *const Mismatch,
          None => {
            trace!("iter past the end of mismatches");
            std::ptr::null()
          }
        }
    } {
        std::ptr::null()
    }
}

ffi_fn! {
    /// Delete a mismatches iterator when you're done with it.
    fn pactffi_mismatches_iter_delete(iter: *mut MismatchesIterator) {
        ptr::drop_raw(iter);
    }
}

ffi_fn! {
    /// Get a JSON representation of the mismatch.
    fn pactffi_mismatch_to_json(mismatch: *const Mismatch) -> *const c_char {
        let mismatch = as_ref!(mismatch);
        let json = mismatch.to_json().to_string();
        string::to_c(&json)? as *const c_char
    } {
        std::ptr::null()
    }
}

ffi_fn! {
    /// Get the type of a mismatch.
    fn pactffi_mismatch_type(mismatch: *const Mismatch) -> *const c_char {
        let mismatch = as_ref!(mismatch);
        let t = mismatch.mismatch_type();
        string::to_c(t)? as *const c_char
    } {
        std::ptr::null()
    }
}

ffi_fn! {
    /// Get a summary of a mismatch.
    fn pactffi_mismatch_summary(mismatch: *const Mismatch) -> *const c_char {
        let mismatch = as_ref!(mismatch);
        let summary = mismatch.summary();
        string::to_c(&summary)? as *const c_char
    } {
        std::ptr::null()
    }
}

ffi_fn! {
    /// Get a description of a mismatch.
    fn pactffi_mismatch_description(mismatch: *const Mismatch) -> *const c_char {
        let mismatch = as_ref!(mismatch);
        let description = mismatch.description();
        string::to_c(&description)? as *const c_char
    } {
        std::ptr::null()
    }
}

ffi_fn! {
    /// Get an ANSI-compatible description of a mismatch.
    fn pactffi_mismatch_ansi_description(mismatch: *const Mismatch) -> *const c_char {
        let mismatch = as_ref!(mismatch);
        let ansi_description = mismatch.ansi_description();
        string::to_c(&ansi_description)? as *const c_char
    } {
        std::ptr::null()
    }
}

/// A collection of mismatches from a matching comparison.
#[allow(missing_copy_implementations)]
#[allow(missing_debug_implementations)]
pub struct Mismatches(Vec<Mismatch>);

/// An iterator over mismatches.
#[allow(missing_copy_implementations)]
#[allow(missing_debug_implementations)]
pub struct MismatchesIterator {
    current: usize,
    mismatches: *const Mismatches,
}

impl MismatchesIterator {
    fn next(&mut self) -> usize {
        let idx = self.current;
        self.current += 1;
        idx
    }
}

#[cfg(test)]
mod tests {
  use std::ffi::CString;

  use expectest::prelude::*;
  use rstest::rstest;

  use super::*;
  use tracing_core::LevelFilter;

  #[rstest]
  #[case("trace", LevelFilter::TRACE)]
  #[case("TRACE", LevelFilter::TRACE)]
  #[case("debug", LevelFilter::DEBUG)]
  #[case("DEBUG", LevelFilter::DEBUG)]
  #[case("info", LevelFilter::INFO)]
  #[case("INFO", LevelFilter::INFO)]
  #[case("warn", LevelFilter::WARN)]
  #[case("WARN", LevelFilter::WARN)]
  #[case("error", LevelFilter::ERROR)]
  #[case("ERROR", LevelFilter::ERROR)]
  #[case("off", LevelFilter::OFF)]
  #[case("OFF", LevelFilter::OFF)]
  #[case("none", LevelFilter::OFF)]
  #[case("NONE", LevelFilter::OFF)]
  #[case("invalid", LevelFilter::INFO)]
  fn log_level_filter_from_c_char_test(#[case] text: String, #[case] level: LevelFilter) {
    let value = CString::new(text).unwrap();
    let result = unsafe { log_level_filter_from_c_char(value.as_ptr()) };
    expect!(result).to(be_equal_to(level));
  }

  #[rstest]
  #[case("trace", Level::TRACE)]
  #[case("TRACE", Level::TRACE)]
  #[case("debug", Level::DEBUG)]
  #[case("DEBUG", Level::DEBUG)]
  #[case("info", Level::INFO)]
  #[case("INFO", Level::INFO)]
  #[case("warn", Level::WARN)]
  #[case("WARN", Level::WARN)]
  #[case("error", Level::ERROR)]
  #[case("ERROR", Level::ERROR)]
  #[case("off", Level::INFO)]
  #[case("OFF", Level::INFO)]
  #[case("none", Level::INFO)]
  #[case("NONE", Level::INFO)]
  #[case("invalid", Level::INFO)]
  fn log_level_from_c_char_test(#[case] text: String, #[case] level: Level) {
    let value = CString::new(text).unwrap();
    let result = unsafe { log_level_from_c_char(value.as_ptr()) };
    expect!(result).to(be_equal_to(level));
  }
}