regorus 0.9.0

A fast, lightweight Rego (OPA policy language) interpreter
Documentation
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Runtime.InteropServices;

#pragma warning disable CS8500
#pragma warning disable CS8981

namespace Regorus.Internal
{
    /// <summary>
    /// Native FFI method declarations for Regorus.
    /// This file contains all P/Invoke declarations for the Regorus native library.
    /// </summary>
    internal static unsafe partial class API
    {
        private const string LibraryName = "regorus_ffi";

        #region Common Methods

        /// <summary>
        /// Drop a RegorusResult.
        /// output and error_message strings are not valid after drop.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_result_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void regorus_result_drop(RegorusResult result);

        /// <summary>
        /// Drop a RegorusBuffer.
        /// data is not valid after drop.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_buffer_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void regorus_buffer_drop(RegorusBuffer* buffer);

        #endregion

        #region Memory Limit Methods

        /// <summary>
        /// Set the global memory limit. Pass hasLimit=false to clear the limit.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_set_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_set_global_memory_limit(ulong limit, [MarshalAs(UnmanagedType.U1)] bool hasLimit);

        /// <summary>
        /// Get the current global memory limit. bool_value indicates whether a limit is set.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_get_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_get_global_memory_limit();

        /// <summary>
        /// Check the global memory limit immediately.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_check_global_memory_limit", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_check_global_memory_limit();

        /// <summary>
        /// Flush the current thread's pending allocation counters into global aggregates.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_flush_thread_memory_counters", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_flush_thread_memory_counters();

        /// <summary>
        /// Set the per-thread flush threshold override. Pass hasThreshold=false to restore defaults.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_set_thread_flush_threshold_override", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_set_thread_flush_threshold_override(ulong threshold, [MarshalAs(UnmanagedType.U1)] bool hasThreshold);

        /// <summary>
        /// Get the per-thread flush threshold. bool_value indicates whether a threshold is configured.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_get_thread_memory_flush_threshold", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_get_thread_memory_flush_threshold();

        #endregion

        #region Engine Methods

        /// <summary>
        /// Construct a new Engine.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusEngine* regorus_engine_new();

        /// <summary>
        /// Clone a RegorusEngine.
        /// To avoid having to parse same policy again, the engine can be cloned
        /// after policies and data have been added.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_clone", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusEngine* regorus_engine_clone(RegorusEngine* engine);

        /// <summary>
        /// Compile an RVM program from the engine state with entry points.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_program_with_entrypoints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_compile_program_with_entrypoints(RegorusEngine* engine, byte** entryPoints, UIntPtr entryPointsLen);

        /// <summary>
        /// Drop a RegorusEngine.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void regorus_engine_drop(RegorusEngine* engine);

        /// <summary>

        /// <summary>
        /// Compile an RVM program from data/modules and entry points.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_program_compile_from_modules", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_program_compile_from_modules(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len, byte** entry_points, UIntPtr entry_points_len);

        /// <summary>
        /// Construct a new empty program.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_program_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusProgram* regorus_program_new();

        /// <summary>
        /// Drop a program handle.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_program_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void regorus_program_drop(RegorusProgram* program);

        /// <summary>
        /// Serialize a program to binary format.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_program_serialize_binary", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_program_serialize_binary(RegorusProgram* program);

        /// <summary>
        /// Deserialize a program from binary format.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_program_deserialize_binary", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_program_deserialize_binary(byte* data, UIntPtr len, byte* is_partial);

        /// <summary>
        /// Generate a readable assembly listing for the program.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_program_generate_listing", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_program_generate_listing(RegorusProgram* program);

        /// <summary>
        /// Create a new RVM instance.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_new", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusRvm* regorus_rvm_new();

        /// <summary>
        /// Create a new RVM instance from a compiled policy.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_new_with_policy", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_new_with_policy(RegorusCompiledPolicy* compiled_policy);

        /// <summary>
        /// Drop an RVM instance.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void regorus_rvm_drop(RegorusRvm* vm);

        /// <summary>
        /// Load a program into the RVM.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_load_program", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_load_program(RegorusRvm* vm, RegorusProgram* program);

        /// <summary>
        /// Set the data document for the RVM.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_set_data(RegorusRvm* vm, byte* data_json);

        /// <summary>
        /// Set the input document for the RVM.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_input", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_set_input(RegorusRvm* vm, byte* input_json);

        /// <summary>
        /// Execute the program.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_execute", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_execute(RegorusRvm* vm);

        /// <summary>
        /// Execute an entry point by name.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_execute_entry_point_by_name", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_execute_entry_point_by_name(RegorusRvm* vm, byte* entry_point);

        /// <summary>
        /// Execute an entry point by index.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_execute_entry_point_by_index", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_execute_entry_point_by_index(RegorusRvm* vm, UIntPtr index);

        /// <summary>
        /// Resume execution.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_resume", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_resume(RegorusRvm* vm, byte* resume_value_json, [MarshalAs(UnmanagedType.I1)] bool has_value);

        /// <summary>
        /// Get the current execution state.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_get_execution_state", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_get_execution_state(RegorusRvm* vm);

        /// <summary>
        /// Set the maximum instruction limit.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_max_instructions", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_set_max_instructions(RegorusRvm* vm, UIntPtr max_instructions);

        /// <summary>
        /// Set strict builtin error handling.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_strict_builtin_errors", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_set_strict_builtin_errors(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool strict);

        /// <summary>
        /// Set execution mode (0 run-to-completion, 1 suspendable).
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_execution_mode", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_set_execution_mode(RegorusRvm* vm, byte mode);

        /// <summary>
        /// Set step mode for suspendable execution.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_step_mode", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_set_step_mode(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool enabled);

        /// <summary>
        /// Set execution timer configuration.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_rvm_set_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_rvm_set_execution_timer_config(RegorusRvm* vm, [MarshalAs(UnmanagedType.I1)] bool has_config, RegorusExecutionTimerConfig config);
        /// Add a policy.
        /// The policy is parsed into AST.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.add_policy
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_add_policy", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_add_policy(RegorusEngine* engine, byte* path, byte* rego);

        /// <summary>
        /// Add a policy from file.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_add_policy_from_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_add_policy_from_file(RegorusEngine* engine, byte* path);

        /// <summary>
        /// Add policy data.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.add_data
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_add_data_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_add_data_json(RegorusEngine* engine, byte* data);

        /// <summary>
        /// Get list of loaded Rego packages as JSON.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_packages
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_get_packages", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_get_packages(RegorusEngine* engine);

        /// <summary>
        /// Get list of policies as JSON.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_policies
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policies", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_get_policies(RegorusEngine* engine);

        /// <summary>
        /// Add data from JSON file.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_add_data_from_json_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_add_data_from_json_file(RegorusEngine* engine, byte* path);

        /// <summary>
        /// Clear policy data.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.clear_data
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_clear_data(RegorusEngine* engine);

        /// <summary>
        /// Set input.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_input
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_set_input_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_set_input_json(RegorusEngine* engine, byte* input);

        /// <summary>
        /// Set input from JSON file.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_set_input_from_json_file", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_set_input_from_json_file(RegorusEngine* engine, byte* path);

        /// <summary>
        /// Evaluate query.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.eval_query
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_eval_query", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_eval_query(RegorusEngine* engine, byte* query);

        /// <summary>
        /// Evaluate specified rule.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.eval_rule
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_eval_rule", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_eval_rule(RegorusEngine* engine, byte* rule);

        /// <summary>
        /// Enable/disable coverage.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_enable_coverage
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_set_enable_coverage", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_set_enable_coverage(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable);

        /// <summary>
        /// Get coverage report.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.get_coverage_report
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_get_coverage_report", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_get_coverage_report(RegorusEngine* engine);

        /// <summary>
        /// Enable/disable strict builtin errors.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_strict_builtin_errors
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_set_strict_builtin_errors", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_set_strict_builtin_errors(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool strict);

        /// <summary>
        /// Get pretty printed coverage report.
        /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Report.html#method.to_string_pretty
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_get_coverage_report_pretty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_get_coverage_report_pretty(RegorusEngine* engine);

        /// <summary>
        /// Clear coverage data.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.clear_coverage_data
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_coverage_data", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_clear_coverage_data(RegorusEngine* engine);

        /// <summary>
        /// Whether to gather output of print statements.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_gather_prints
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_set_gather_prints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_set_gather_prints(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable);

        /// <summary>
        /// Take all the gathered print statements.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.take_prints
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_take_prints", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_take_prints(RegorusEngine* engine);

        /// <summary>
        /// Get AST of policies.
        /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_ast_as_json
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_get_ast_as_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_get_ast_as_json(RegorusEngine* engine);

        /// <summary>
        /// Gets the package names defined in each policy added to the engine.
        /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_policy_package_names
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policy_package_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_get_policy_package_names(RegorusEngine* engine);

        /// <summary>
        /// Gets the parameters defined in each policy added to the engine.
        /// See https://docs.rs/regorus/latest/regorus/coverage/struct.Engine.html#method.get_policy_parameters
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_get_policy_parameters", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_get_policy_parameters(RegorusEngine* engine);

        /// <summary>
        /// Enable/disable rego v1.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.set_rego_v0
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_set_rego_v0", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_set_rego_v0(RegorusEngine* engine, [MarshalAs(UnmanagedType.U1)] bool enable);

        /// <summary>
        /// Compile a target-aware policy from the current engine state.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.compile_for_target
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_for_target", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_compile_for_target(RegorusEngine* engine);

        /// <summary>
        /// Compile a policy with a specific entry point rule.
        /// See https://docs.rs/regorus/latest/regorus/struct.Engine.html#method.compile_with_entrypoint
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_compile_with_entrypoint", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_compile_with_entrypoint(RegorusEngine* engine, byte* rule);

#if REGORUS_FFI_TEST_HOOKS
        /// <summary>
        /// Trigger a panic inside the engine for testing purposes.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_test_trigger_panic", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_test_trigger_panic();

        /// <summary>
        /// Reset the engine poison flag for testing.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_test_reset_poison", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void regorus_engine_test_reset_poison();
#endif

        /// <summary>
        /// Configure the execution timer for a specific engine instance.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_set_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_set_execution_timer_config(RegorusEngine* engine, RegorusExecutionTimerConfig* config);

        /// <summary>
        /// Clear the execution timer configuration for a specific engine instance.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_engine_clear_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_engine_clear_execution_timer_config(RegorusEngine* engine);

        #endregion

        #region Execution Timer Global Methods

        /// <summary>
        /// Set the process-wide fallback execution timer configuration.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_set_fallback_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_set_fallback_execution_timer_config(RegorusExecutionTimerConfig config);

        /// <summary>
        /// Clear the process-wide fallback execution timer configuration.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_clear_fallback_execution_timer_config", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_clear_fallback_execution_timer_config();

        #endregion

        #region Compilation Methods

        /// <summary>
        /// Compiles a policy from data and modules with a specific entry point rule.
        /// This is a convenience function that wraps regorus::compile_policy_with_entrypoint.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_compile_policy_with_entrypoint", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_compile_policy_with_entrypoint(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len, byte* entry_point_rule);

        /// <summary>
        /// Compiles a target-aware policy from data and modules.
        /// This is a convenience function that wraps regorus::compile_policy_for_target.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_compile_policy_for_target", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_compile_policy_for_target(byte* data_json, RegorusPolicyModule* modules, UIntPtr modules_len);

        #endregion

        #region Compiled Policy Methods

        /// <summary>
        /// Drop a RegorusCompiledPolicy.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_drop", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern void regorus_compiled_policy_drop(RegorusCompiledPolicy* compiled_policy);

        /// <summary>
        /// Evaluate the compiled policy with the given input.
        /// For target policies, evaluates the target's effect rule.
        /// For regular policies, evaluates the originally compiled rule.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_eval_with_input", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_compiled_policy_eval_with_input(RegorusCompiledPolicy* compiled_policy, byte* input);

        /// <summary>
        /// Get information about the compiled policy including metadata about modules,
        /// target configuration, and resource types.
        /// Returns a JSON-encoded PolicyInfo struct containing comprehensive
        /// information about the compiled policy such as module IDs, target name,
        /// applicable resource types, entry point rule, and parameters.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_compiled_policy_get_policy_info", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_compiled_policy_get_policy_info(RegorusCompiledPolicy* compiled_policy);

        #endregion

        #region Target Registry Methods

        /// <summary>
        /// Register a target from JSON definition.
        /// The target JSON should follow the target schema format.
        /// Once registered, the target can be referenced in Rego policies using __target__ rules.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_register_target_from_json", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_register_target_from_json(byte* target_json);

        /// <summary>
        /// Check if a target is registered.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_target_registry_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_target_registry_contains(byte* name);

        /// <summary>
        /// Get a list of all registered target names as JSON array.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_target_registry_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_target_registry_list_names();

        /// <summary>
        /// Remove a target from the registry by name.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_target_registry_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_target_registry_remove(byte* name);

        /// <summary>
        /// Clear all targets from the registry.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_target_registry_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_target_registry_clear();

        /// <summary>
        /// Get the number of registered targets.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_target_registry_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_target_registry_len();

        /// <summary>
        /// Check if the target registry is empty.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_target_registry_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_target_registry_is_empty();

        #endregion

        #region Resource Schema Registry Methods

        /// <summary>
        /// Register a resource schema from JSON with a given name.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_register", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_resource_schema_register(byte* name, byte* schema_json);

        /// <summary>
        /// Check if a resource schema with the given name exists.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_resource_schema_contains(byte* name);

        /// <summary>
        /// Get the number of registered resource schemas.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_resource_schema_len();

        /// <summary>
        /// Check if the resource schema registry is empty.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_resource_schema_is_empty();

        /// <summary>
        /// List all registered resource schema names as a JSON array.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_resource_schema_list_names();

        /// <summary>
        /// Remove a resource schema by name.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_resource_schema_remove(byte* name);

        /// <summary>
        /// Clear all resource schemas from the registry.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_resource_schema_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_resource_schema_clear();

        #endregion

        #region Effect Schema Registry Methods

        /// <summary>
        /// Register an effect schema from JSON with a given name.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_register", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_effect_schema_register(byte* name, byte* schema_json);

        /// <summary>
        /// Check if an effect schema with the given name exists.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_contains", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_effect_schema_contains(byte* name);

        /// <summary>
        /// Get the number of registered effect schemas.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_len", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_effect_schema_len();

        /// <summary>
        /// Check if the effect schema registry is empty.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_is_empty", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_effect_schema_is_empty();

        /// <summary>
        /// List all registered effect schema names as a JSON array.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_list_names", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_effect_schema_list_names();

        /// <summary>
        /// Remove an effect schema by name.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_remove", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_effect_schema_remove(byte* name);

        /// <summary>
        /// Clear all effect schemas from the registry.
        /// </summary>
        [DllImport(LibraryName, EntryPoint = "regorus_effect_schema_clear", CallingConvention = CallingConvention.Cdecl, ExactSpelling = true)]
        internal static extern RegorusResult regorus_effect_schema_clear();

        #endregion
    }

    #region Native Structures

    /// <summary>
    /// Type of data contained in RegorusResult.
    /// </summary>
    internal enum RegorusDataType : uint
    {
        /// <summary>
        /// No data / void.
        /// </summary>
        None,
        /// <summary>
        /// String data (output field is valid).
        /// </summary>
        String,
        /// <summary>
        /// Boolean data (bool_value field is valid).
        /// </summary>
        Boolean,
        /// <summary>
        /// Integer data (int_value field is valid).
        /// </summary>
        Integer,
        /// <summary>
        /// Pointer data (pointer_value field is valid).
        /// </summary>
        Pointer,
    }

    /// <summary>
    /// Status of a call on RegorusEngine.
    /// </summary>
    internal enum RegorusStatus : uint
    {
        /// <summary>
        /// The operation was successful.
        /// </summary>
        Ok,
        /// <summary>
        /// The operation was unsuccessful.
        /// </summary>
        Error,
        /// <summary>
        /// Invalid data format provided.
        /// </summary>
        InvalidDataFormat,
        /// <summary>
        /// Invalid entrypoint rule specified.
        /// </summary>
        InvalidEntrypoint,
        /// <summary>
        /// Compilation failed.
        /// </summary>
        CompilationFailed,
        /// <summary>
        /// Invalid argument provided.
        /// </summary>
        InvalidArgument,
        /// <summary>
        /// Invalid module ID.
        /// </summary>
        InvalidModuleId,
        /// <summary>
        /// Invalid policy content.
        /// </summary>
        InvalidPolicy,
        /// <summary>
        /// The engine panicked and cannot be reused until reset.
        /// </summary>
        Panic,
        /// <summary>
        /// The engine remains poisoned because a previous panic was detected.
        /// </summary>
        Poisoned,
    }

    /// <summary>
    /// Result of a call on RegorusEngine.
    /// Must be freed using regorus_result_drop.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe partial struct RegorusResult
    {
        /// <summary>
        /// Status.
        /// </summary>
        public RegorusStatus status;
        /// <summary>
        /// Type of data contained in this result.
        /// </summary>
        public RegorusDataType data_type;
        /// <summary>
        /// String output produced by the call.
        /// Valid when data_type is String. Owned by Rust.
        /// </summary>
        public byte* output;
        /// <summary>
        /// Boolean value.
        /// Valid when data_type is Boolean.
        /// </summary>
        [MarshalAs(UnmanagedType.I1)]
        public bool bool_value;
        /// <summary>
        /// Integer value.
        /// Valid when data_type is Integer.
        /// </summary>
        public long int_value;
        /// <summary>
        /// Pointer value.
        /// Valid when data_type is Pointer.
        /// </summary>
        public void* pointer_value;
        /// <summary>
        /// Errors produced by the call.
        /// Owned by Rust.
        /// </summary>
        public byte* error_message;
    }

    /// <summary>
    /// FFI representation of the execution timer configuration.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal struct RegorusExecutionTimerConfig
    {
        public ulong limit_ns;
        public uint check_interval;
    }

    /// <summary>
    /// Byte buffer returned from FFI.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe struct RegorusBuffer
    {
        public byte* data;
        public UIntPtr len;
        public UIntPtr capacity;
    }

    /// <summary>
    /// Wrapper for regorus::Engine.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe partial struct RegorusEngine
    {
    }

    /// <summary>
    /// Wrapper for regorus::CompiledPolicy.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe partial struct RegorusCompiledPolicy
    {
    }

    /// <summary>
    /// Wrapper for regorus::rvm::Program.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe partial struct RegorusProgram
    {
    }

    /// <summary>
    /// Wrapper for regorus::rvm::RegoVM.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe partial struct RegorusRvm
    {
    }

    /// <summary>
    /// FFI wrapper for PolicyModule struct.
    /// </summary>
    [StructLayout(LayoutKind.Sequential)]
    internal unsafe partial struct RegorusPolicyModule
    {
        public byte* id;
        public byte* content;
    }

    #endregion
}