cfn_guard_ffi/lib.rs
1use cfn_guard::run_checks;
2use ffi_support::ExternError;
3use std::os::raw::c_char;
4
5mod errors;
6mod types;
7
8use errors::FfiError;
9use types::FfiValidateInput;
10
11/**
12 * C prototype for this function:
13 * typedef struct {
14 * int32_t code;
15 * char *message;
16 * } extern_err_t;
17 *
18 * typedef struct {
19 * char *content;
20 * char *file_name;
21 * } validate_input_t;
22 *
23 * char* cfn_guard_run_checks(validate_input_t template, validate_input_t rules, _Bool verbose, extern_err_t * err);
24 * void cfn_guard_free_string(char *);
25 *
26 * if an error is returned, it will be populated in `err`. `cfn_guard_free_string` must be called
27 * for the `message` field in `err`.
28 *
29 * if `err.code` == 0, then the result will be a json string. This `*char` must be passed to
30 * `cfn_guard_free_string` to return the memory allocated by rust.
31 */
32#[no_mangle]
33pub extern "C" fn cfn_guard_run_checks<'a>(
34 data: FfiValidateInput<'a>,
35 rules: FfiValidateInput<'a>,
36 verbose: c_char,
37 err: &mut ExternError,
38) -> *mut c_char {
39 ffi_support::call_with_result(err, || {
40 match run_checks(data.into(), rules.into(), verbose == 1) {
41 Err(e) => Err(FfiError(e)),
42 Ok(r) => Ok(r),
43 }
44 })
45}
46
47ffi_support::define_string_destructor!(cfn_guard_free_string);