cel-cxx-ffi 0.2.5

FFI bindings for the cel-cxx crate
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
#ifndef CEL_CXX_FFI_INCLUDE_COMMON_VALUES_H_
#define CEL_CXX_FFI_INCLUDE_COMMON_VALUES_H_

#include <limits>
#include <rust/cxx.h>
#include <common/value.h>
#include <common/values/value_builder.h>
#include <common/values/parsed_message_value.h>
#include <absl/strings/str_cat.h>

namespace rust::cel_cxx {

using absl::Status;
using absl::Time;
using absl::Duration;
using Arena = google::protobuf::Arena;
using DescriptorPool = const google::protobuf::DescriptorPool;
using MessageFactory = google::protobuf::MessageFactory;

using Type = cel::Type;
using OpaqueType = cel::OpaqueType;

using Value = cel::Value;
using BoolValue = cel::BoolValue;
using BytesValue = cel::BytesValue;
using DoubleValue = cel::DoubleValue;
using DurationValue = cel::DurationValue;
using ErrorValue = cel::ErrorValue;
using IntValue = cel::IntValue;
using ListValue = cel::ListValue;
using ListValueBuilder = cel::ListValueBuilder;
using MapValue = cel::MapValue;
using MapValueBuilder = cel::MapValueBuilder;
using MessageValue = cel::MessageValue;
using NullValue = cel::NullValue;
using OpaqueValue = cel::OpaqueValue;
using OptionalValue = cel::OptionalValue;
using StringValue = cel::StringValue;
using StructValue = cel::StructValue;
using TimestampValue = cel::TimestampValue;
using TypeValue = cel::TypeValue;
using UintValue = cel::UintValue;
using UnknownValue = cel::UnknownValue;

using ValueIterator = cel::ValueIterator;
using ValueBuilder = cel::ValueBuilder;

struct AnyFfiOpaqueValue;

// Value
inline size_t Value_size_of() {
    return sizeof(Value);
}

inline void Value_swap(Value& lhs, Value& rhs) {
    swap(lhs, rhs);
}

inline std::unique_ptr<Value> Value_new_bool(const BoolValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_bytes(const BytesValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_double(const DoubleValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_duration(const DurationValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_error(const ErrorValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_int(const IntValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_list(const ListValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_map(const MapValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_message(const MessageValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_null() {
    return std::make_unique<Value>();
}

inline std::unique_ptr<Value> Value_new_opaque(const OpaqueValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_optional(const OptionalValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_string(const StringValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_timestamp(const TimestampValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_type(const TypeValue& value) {
    return std::make_unique<Value>(value);
}

inline std::unique_ptr<Value> Value_new_uint(const UintValue& value) {
    return std::make_unique<Value>(value);
}


inline std::unique_ptr<BoolValue> Value_get_bool(const Value& value) {
    return std::make_unique<BoolValue>(value.GetBool());
}

inline std::unique_ptr<BytesValue> Value_get_bytes(const Value& value) {
    return std::make_unique<BytesValue>(value.GetBytes());
}

inline std::unique_ptr<DoubleValue> Value_get_double(const Value& value) {
    return std::make_unique<DoubleValue>(value.GetDouble());
}

inline std::unique_ptr<DurationValue> Value_get_duration(const Value& value) {
    return std::make_unique<DurationValue>(value.GetDuration());
}

inline std::unique_ptr<ErrorValue> Value_get_error(const Value& value) {
    return std::make_unique<ErrorValue>(value.GetError());
}

inline std::unique_ptr<IntValue> Value_get_int(const Value& value) {
    return std::make_unique<IntValue>(value.GetInt());
}

inline std::unique_ptr<ListValue> Value_get_list(const Value& value) {
    return std::make_unique<ListValue>(value.GetList());
}

inline std::unique_ptr<MapValue> Value_get_map(const Value& value) {
    return std::make_unique<MapValue>(value.GetMap());
}

inline std::unique_ptr<MessageValue> Value_get_message(const Value& value) {
    return std::make_unique<MessageValue>(value.GetMessage());
}

inline std::unique_ptr<NullValue> Value_get_null(const Value& value) {
    return std::make_unique<NullValue>(value.GetNull());
}

inline std::unique_ptr<OpaqueValue> Value_get_opaque(const Value& value) {
    return std::make_unique<OpaqueValue>(value.GetOpaque());
}

inline std::unique_ptr<OptionalValue> Value_get_optional(const Value& value) {
    return std::make_unique<OptionalValue>(value.GetOptional());
}

inline std::unique_ptr<StringValue> Value_get_string(const Value& value) {
    return std::make_unique<StringValue>(value.GetString());
}

inline std::unique_ptr<TimestampValue> Value_get_timestamp(const Value& value) {
    return std::make_unique<TimestampValue>(value.GetTimestamp());
}

inline std::unique_ptr<TypeValue> Value_get_type(const Value& value) {
    return std::make_unique<TypeValue>(value.GetType());
}

inline std::unique_ptr<UintValue> Value_get_uint(const Value& value) {
    return std::make_unique<UintValue>(value.GetUint());
}

// BoolValue
inline std::unique_ptr<BoolValue> BoolValue_new(bool value) {
    String a;
    return std::make_unique<BoolValue>(value);
}

// BytesValue
inline std::unique_ptr<BytesValue> BytesValue_new(const Arena& arena, Slice<const uint8_t> bytes) {
    auto bytes_value = cel::BytesValue::From(
        std::string_view(
            reinterpret_cast<const char*>(bytes.data()),
            bytes.size()
        ),
        &const_cast<Arena&>(arena)
    );
    return std::make_unique<BytesValue>(std::move(bytes_value));
}

inline Vec<uint8_t> BytesValue_native_value(const BytesValue& bytes_value) {
    auto s = bytes_value.ToString();
    Vec<uint8_t> vec;
    vec.reserve(s.size());
    std::move(s.begin(), s.end(), std::back_inserter(vec));
    return vec;
}

// DoubleValue
inline std::unique_ptr<DoubleValue> DoubleValue_new(double value) {
    return std::make_unique<DoubleValue>(value);
}

// DurationValue
inline std::unique_ptr<DurationValue> DurationValue_new(Duration value) {
    return std::make_unique<DurationValue>(value);
}

// ErrorValue
inline std::unique_ptr<ErrorValue> ErrorValue_new(Status status) {
    return std::make_unique<ErrorValue>(status);
}

inline Status ErrorValue_native_value(const ErrorValue& error_value) {
    return error_value.ToStatus();
}

// IntValue
inline std::unique_ptr<IntValue> IntValue_new(int64_t value) {
    return std::make_unique<IntValue>(value);
}

// ListValue
inline bool ListValue_is_empty(const ListValue& list_value) {
    return list_value.IsEmpty().value();
}

inline size_t ListValue_size(const ListValue& list_value) {
    return list_value.Size().value();
}

inline Status ListValue_new_iterator(
    const ListValue& list_value,
    std::unique_ptr<ValueIterator>& iterator
) {
    auto status_or = list_value.NewIterator();
    if (!status_or.ok()) {
        return status_or.status();
    }
    iterator.reset(status_or->release());
    return Status();
}

// ListValueBuilder
inline std::unique_ptr<ListValueBuilder> ListValueBuilder_new(const Arena& arena) {
    return cel::NewListValueBuilder(&const_cast<Arena&>(arena));
}

inline Status ListValueBuilder_add(
    ListValueBuilder& builder,
    const Value& value
) {
    return builder.Add(value);
}

inline std::unique_ptr<ListValue> ListValueBuilder_build(
    ListValueBuilder& builder
) {
    return std::make_unique<ListValue>(std::move(builder).Build());
}

// MapValue
inline bool MapValue_is_empty(const MapValue& map_value) {
    return map_value.IsEmpty().value();
}

inline size_t MapValue_size(const MapValue& map_value) {
    return map_value.Size().value();
}

inline Status MapValue_new_iterator(
    const MapValue& map_value,
    std::unique_ptr<ValueIterator>& iterator
) {
    auto status_or = map_value.NewIterator();
    if (!status_or.ok()) {
        return status_or.status();
    }
    iterator.reset(status_or->release());
    return Status();
}

// MapValueBuilder
inline std::unique_ptr<MapValueBuilder> MapValueBuilder_new(const Arena& arena) {
    return cel::NewMapValueBuilder(&const_cast<Arena&>(arena));
}

inline Status MapValueBuilder_put(
    MapValueBuilder& builder,
    const Value& key,
    const Value& value
) {
    return builder.Put(key, value);
}

inline std::unique_ptr<MapValue> MapValueBuilder_build(
    MapValueBuilder& builder
) {
    return std::make_unique<MapValue>(std::move(builder).Build());
}


// MessageValue
inline Status MessageValue_from_bytes(
    const Arena& arena,
    const DescriptorPool& descriptor_pool,
    const MessageFactory& message_factory,
    Str type_name,
    Slice<const uint8_t> bytes,
    std::unique_ptr<MessageValue>& result
) {
    std::string_view name(type_name.data(), type_name.size());
    auto descriptor = descriptor_pool.FindMessageTypeByName(name);
    if (descriptor == nullptr) {
        return absl::NotFoundError(
            absl::StrCat("Message type not found: ", name));
    }
    // GetPrototype is semantically const but not declared as such in protobuf C++.
    auto prototype = const_cast<MessageFactory&>(message_factory).GetPrototype(descriptor);
    if (prototype == nullptr) {
        return absl::InternalError(
            absl::StrCat("Failed to get prototype for: ", name));
    }
    auto* arena_ptr = &const_cast<Arena&>(arena);
    auto* message = prototype->New(arena_ptr);
    if (bytes.size() > static_cast<size_t>(std::numeric_limits<int>::max())) {
        return absl::InvalidArgumentError(
            absl::StrCat("Message bytes too large for: ", name));
    }
    if (!message->ParseFromArray(bytes.data(), static_cast<int>(bytes.size()))) {
        return absl::InvalidArgumentError(
            absl::StrCat("Failed to parse bytes as: ", name));
    }
    result = std::make_unique<MessageValue>(
        cel::ParsedMessageValue(message, arena_ptr));
    return Status();
}

inline Status MessageValue_to_bytes(
    const MessageValue& message_value,
    Vec<uint8_t>& result_vec
) {
    const auto& parsed = message_value.GetParsed();
    std::string serialized;
    if (!(*parsed).SerializeToString(&serialized)) {
        return absl::InternalError("Failed to serialize MessageValue to bytes");
    }
    result_vec.reserve(serialized.size());
    std::move(serialized.begin(), serialized.end(), std::back_inserter(result_vec));
    return Status();
}

inline String MessageValue_get_type_name(const MessageValue& message_value) {
    auto name = message_value.GetTypeName();
    return String(std::string(name));
}

// MessageValue field access
inline Status MessageValue_get_field_by_name(
    const MessageValue& message_value,
    const DescriptorPool& descriptor_pool,
    const MessageFactory& message_factory,
    const Arena& arena,
    Str field_name,
    std::unique_ptr<Value>& result
) {
    auto value = std::make_unique<Value>();
    std::string_view name(field_name.data(), field_name.size());
    auto status = message_value.GetFieldByName(
        name, &descriptor_pool,
        &const_cast<MessageFactory&>(message_factory),
        &const_cast<Arena&>(arena),
        value.get());
    if (!status.ok()) return status;
    result.swap(value);
    return Status();
}

inline bool MessageValue_has_field_by_name(
    const MessageValue& message_value,
    Str field_name
) {
    std::string_view name(field_name.data(), field_name.size());
    auto result = message_value.HasFieldByName(name);
    if (!result.ok()) return false;
    return result.value();
}

// NullValue
inline std::unique_ptr<NullValue> NullValue_new() {
    return std::make_unique<NullValue>();
}

// OpaqueValue
class AnyFfiOpaqueValueWrapper: public cel::OpaqueValueInterface {
public:
    AnyFfiOpaqueValueWrapper(Box<AnyFfiOpaqueValue> ffi): ffi_(std::move(ffi)) {}
    Box<AnyFfiOpaqueValue> ffi() const;
    Box<AnyFfiOpaqueValue> release() && { return std::move(ffi_); }

    virtual std::string DebugString() const override;
    virtual std::string_view GetTypeName() const override;
    virtual OpaqueType GetRuntimeType() const override;
    virtual Status Equal(
        const OpaqueValue& other,
        const google::protobuf::DescriptorPool* absl_nonnull descriptor_pool,
        google::protobuf::MessageFactory* absl_nonnull message_factory,
        google::protobuf::Arena* absl_nonnull arena,
        Value* absl_nonnull result) const override;
    virtual OpaqueValue Clone(google::protobuf::Arena* absl_nonnull arena) const override;
    virtual cel::NativeTypeId GetNativeTypeId() const override;

    struct Content {
        const OpaqueValueInterface* absl_nonnull interface;
        google::protobuf::Arena* absl_nonnull arena;
    };
private:
    Box<AnyFfiOpaqueValue> ffi_;
};

std::unique_ptr<OpaqueValue> OpaqueValue_new(
    const Arena& arena,
    const google::protobuf::DescriptorPool& descriptor_pool,
    Box<AnyFfiOpaqueValue> ffi);

inline std::unique_ptr<OptionalValue> OpaqueValue_get_optional(const OpaqueValue& opaque_value) {
    return std::make_unique<OptionalValue>(opaque_value.GetOptional());
}

Box<AnyFfiOpaqueValue> OpaqueValue_get_ffi(const OpaqueValue& opaque_value);

// OptionalValue
inline std::unique_ptr<OptionalValue> OptionalValue_new(const Arena& arena, const Value& value) {
    auto optional_value = OptionalValue::Of(value, &const_cast<Arena&>(arena));
    return std::make_unique<OptionalValue>(std::move(optional_value));
}

inline std::unique_ptr<OptionalValue> OptionalValue_none() {
    return std::make_unique<OptionalValue>();
}

inline std::unique_ptr<Value> OptionalValue_get_value(const OptionalValue& optional_value) {
    return std::make_unique<Value>(optional_value.Value());
}

// StringValue
inline std::unique_ptr<StringValue> StringValue_new(const Arena& arena, Str value) {
    auto string_value = StringValue::From(
        std::string_view(value.data(), value.size()),
        &const_cast<Arena&>(arena));
    return std::make_unique<StringValue>(std::move(string_value));
}

inline String StringValue_native_value(const StringValue& string_value) {
    return String::lossy(string_value.ToString());
}

// TimestampValue
inline std::unique_ptr<TimestampValue> TimestampValue_new(Time value) {
    return std::make_unique<TimestampValue>(value);
}

// TypeValue
inline std::unique_ptr<TypeValue> TypeValue_new(const Type& value) {
    return std::make_unique<TypeValue>(value);
}

// UintValue
inline std::unique_ptr<UintValue> UintValue_new(uint64_t value) {
    return std::make_unique<UintValue>(value);
}

// ValueIterator
inline Status ValueIterator_next1(
    ValueIterator& iterator,
    const google::protobuf::DescriptorPool& descriptor_pool,
    const google::protobuf::MessageFactory& message_factory,
    const google::protobuf::Arena& arena,
    std::unique_ptr<Value>& result
) {
    auto value = std::make_unique<Value>();
    auto status_or = iterator.Next1(
        &descriptor_pool,
        &const_cast<MessageFactory&>(message_factory),
        &const_cast<Arena&>(arena),
        value.get()
    );
    if (!status_or.ok()) {
        return status_or.status();
    }
    if (!status_or.value()) {
        return absl::OutOfRangeError("No more values");
    }
    result.swap(value);
    return Status();
}

inline Status ValueIterator_next2(
    ValueIterator& iterator,
    const google::protobuf::DescriptorPool& descriptor_pool,
    const google::protobuf::MessageFactory& message_factory,
    const google::protobuf::Arena& arena,
    std::unique_ptr<Value>& key,
    std::unique_ptr<Value>& value
) {
    auto key_value = std::make_unique<Value>();
    auto value_value = std::make_unique<Value>();
    auto status_or = iterator.Next2(
        &descriptor_pool,
        &const_cast<MessageFactory&>(message_factory),
        &const_cast<Arena&>(arena),
        key_value.get(),
        value_value.get()
    );
    if (!status_or.ok()) {
        return status_or.status();
    }
    if (!status_or.value()) {
        return absl::OutOfRangeError("No more values");
    }
    key.swap(key_value);
    value.swap(value_value);
    return Status();
}

// ValueBuilder
struct AnyFfiValueBuilder;
std::unique_ptr<ValueBuilder> ValueBuilder_new(Box<AnyFfiValueBuilder> ffi);

inline std::unique_ptr<ValueBuilder> ValueBuilder_new_message(
    const Arena& arena,
    const DescriptorPool& descriptor_pool,
    const MessageFactory& message_factory,
    absl::string_view name
) {
    return cel::common_internal::NewValueBuilder(
        &const_cast<Arena&>(arena),
        &descriptor_pool,
        &const_cast<MessageFactory&>(message_factory),
        name
    );
}

} // namespace rust::cel_cxx
#endif // CEL_CXX_FFI_INCLUDE_COMMON_VALUES_H_