forked from pact-foundation/pact-reference
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FFI wrappers for constructing and deleting MessagePact (#43)
- Loading branch information
1 parent
6c709fe
commit 41347af
Showing
2 changed files
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//! FFI wrapper for `MessagePact` from pact_matching. | ||
use anyhow::{anyhow, Context}; | ||
use libc::{c_char, c_int, EXIT_FAILURE, EXIT_SUCCESS}; | ||
|
||
// Necessary to make 'cbindgen' generate an opaque struct on the C side. | ||
pub use pact_matching::models::message_pact::MessagePact; | ||
|
||
use crate::util::*; | ||
use crate::{cstr, ffi, safe_str}; | ||
|
||
/// Construct a new `MessagePact` from the JSON string. | ||
/// The provided file name is used when generating error messages. | ||
#[no_mangle] | ||
#[allow(clippy::missing_safety_doc)] | ||
pub unsafe extern "C" fn message_pact_new_from_json( | ||
file_name: *const c_char, | ||
json_str: *const c_char, | ||
) -> *mut MessagePact { | ||
ffi! { | ||
name: "message_pact_new_from_json", | ||
params: [file_name, json_str], | ||
op: { | ||
let file_name = safe_str!(file_name); | ||
let json_str = safe_str!(json_str); | ||
|
||
let json_value: serde_json::Value = | ||
serde_json::from_str(json_str) | ||
.context("error parsing json_str as JSON")?; | ||
|
||
let message_pact = MessagePact::from_json( | ||
&file_name.to_string(), | ||
&json_value, | ||
).map_err(|e| anyhow!("{}", e))?; | ||
|
||
Ok(ptr::raw_to(message_pact)) | ||
}, | ||
fail: { | ||
ptr::null_mut_to::<MessagePact>() | ||
} | ||
} | ||
} | ||
|
||
/// Delete the `MessagePact` being pointed to. | ||
#[no_mangle] | ||
#[allow(clippy::missing_safety_doc)] | ||
pub unsafe extern "C" fn message_pact_delete( | ||
message_pact: *mut MessagePact, | ||
) -> c_int { | ||
ffi! { | ||
name: "message_pact_delete", | ||
params: [message_pact], | ||
op: { | ||
ptr::drop_raw(message_pact); | ||
Ok(EXIT_SUCCESS) | ||
}, | ||
fail: { | ||
EXIT_FAILURE | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
//! Represents messages in `pact_matching`. | ||
pub mod message; | ||
pub mod message_pact; | ||
pub mod pact_specification; | ||
pub mod provider_state; |