6#ifndef OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_RUST_H_
7#define OPENTITAN_SW_DEVICE_LIB_UJSON_UJSON_RUST_H_
19#ifndef RUST_PREPROCESSOR_EMIT
20#error "Do not include this file directly. Include ujson_derive.h instead."
22#include "sw/device/lib/base/adv_macros.h"
35#define RUST_DEFAULT_DERIVE \
36 Debug, Clone, serde::Serialize, serde::Deserialize, PartialEq, Eq
39rust_attr[allow(unused_imports)]
40use opentitanlib::test_utils::status::status_t;
41rust_attr[allow(unused_imports)]
42use opentitanlib::test_utils::hex_bytes;
46#define ujson_struct_field_array_indirect() ujson_struct_field_array
47#define ujson_struct_field_array(t_, sz_, ...) \
48 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
50 arrayvec::ArrayVec<OT_EXPAND(t_, sz_)> \
52 arrayvec::ArrayVec<OT_OBSTRUCT(ujson_struct_field_array_indirect)()(t_, __VA_ARGS__), sz_> \
55#define ujson_struct_field(name_, type_, ...) \
56 pub OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
60 name_: OT_EVAL(ujson_struct_field_array(type_, __VA_ARGS__)) \
63#define ujson_struct_string(name_, size_, ...) \
64 ujson_struct_field(name_, String, ##__VA_ARGS__)
66#define ujson_struct_bytes(name_, size_, len_) \
67 rust_attr[serde(with = "hex_bytes")] \
68 ujson_struct_field(name_, uint8_t, size_)
70#define UJSON_DECLARE_STRUCT(formal_name_, name_, decl_, ...) \
71 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
73 rust_attr[derive(RUST_DEFAULT_DERIVE)] \
75 rust_attr[derive(__VA_ARGS__)] \
77 rust_attr[allow(non_camel_case_types)] \
79 decl_(ujson_struct_field, ujson_struct_string, ujson_struct_bytes) \
81 rust_attr[allow(dead_code)] \
82 pub type formal_name_ = name_
84#define ujson_enum_value(formal_name_, name_, ...) \
85 pub OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
92#define UJSON_DECLARE_ENUM(formal_name_, name_, decl_, ...) \
93 OT_IIF(OT_NOT(OT_VA_ARGS_COUNT(dummy, ##__VA_ARGS__))) \
95 rust_attr[derive(RUST_DEFAULT_DERIVE)] \
97 rust_attr[derive(__VA_ARGS__)] \
99 rust_attr[repr(u32)] \
100 rust_attr[allow(non_camel_case_types)] \
102 decl_(formal_name_, ujson_enum_value) \
103 RUST_ENUM_INTVALUE (u32), \
105 rust_attr[allow(dead_code)] \
106 pub type formal_name_ = name_
112#define UJSON_SERDE_STRUCT(formal_name_, name_, decl_, ...) \
113 UJSON_DECLARE_STRUCT(formal_name_, name_, decl_, ##__VA_ARGS__)
115#define UJSON_SERDE_ENUM(formal_name_, name_, decl_, ...) \
116 UJSON_DECLARE_ENUM(formal_name_, name_, decl_, ##__VA_ARGS__)
118#define C_ONLY(x) const _ : () = {}
119#define RUST_ONLY(x) x