| 1 |
|
| 2 |
# Helper for structured reading of values from a RustBuffer. |
| 3 |
class RustBufferStream |
| 4 |
|
| 5 |
def initialize(rbuf) |
| 6 |
@rbuf = rbuf |
| 7 |
@offset = 0 |
| 8 |
end |
| 9 |
|
| 10 |
def remaining |
| 11 |
@rbuf.len - @offset |
| 12 |
end |
| 13 |
|
| 14 |
def read(size) |
| 15 |
raise InternalError, 'read past end of rust buffer' if @offset + size > @rbuf.len |
| 16 |
|
| 17 |
data = @rbuf.data.get_bytes @offset, size |
| 18 |
|
| 19 |
@offset += size |
| 20 |
|
| 21 |
data |
| 22 |
end |
| 23 |
|
| 24 |
{% for typ in ci.iter_local_types() -%} |
| 25 |
{%- let canonical_type_name = self::canonical_name(typ).borrow()|class_name_rb -%} |
| 26 |
{%- match typ -%} |
| 27 |
|
| 28 |
{% when Type::Int8 -%} |
| 29 |
|
| 30 |
def readI8 |
| 31 |
unpack_from 1, 'c' |
| 32 |
end |
| 33 |
|
| 34 |
{% when Type::UInt8 -%} |
| 35 |
|
| 36 |
def readU8 |
| 37 |
unpack_from 1, 'c' |
| 38 |
end |
| 39 |
|
| 40 |
{% when Type::Int16 -%} |
| 41 |
|
| 42 |
def readI16 |
| 43 |
unpack_from 2, 's>' |
| 44 |
end |
| 45 |
|
| 46 |
{% when Type::UInt16 -%} |
| 47 |
|
| 48 |
def readU16 |
| 49 |
unpack_from 2, 'S>' |
| 50 |
end |
| 51 |
|
| 52 |
{% when Type::Int32 -%} |
| 53 |
|
| 54 |
def readI32 |
| 55 |
unpack_from 4, 'l>' |
| 56 |
end |
| 57 |
|
| 58 |
{% when Type::UInt32 -%} |
| 59 |
|
| 60 |
def readU32 |
| 61 |
unpack_from 4, 'L>' |
| 62 |
end |
| 63 |
|
| 64 |
{% when Type::Int64 -%} |
| 65 |
|
| 66 |
def readI64 |
| 67 |
unpack_from 8, 'q>' |
| 68 |
end |
| 69 |
|
| 70 |
{% when Type::UInt64 -%} |
| 71 |
|
| 72 |
def readU64 |
| 73 |
unpack_from 8, 'Q>' |
| 74 |
end |
| 75 |
|
| 76 |
{% when Type::Float32 -%} |
| 77 |
|
| 78 |
def readF32 |
| 79 |
unpack_from 4, 'g' |
| 80 |
end |
| 81 |
|
| 82 |
{% when Type::Float64 -%} |
| 83 |
|
| 84 |
def readF64 |
| 85 |
unpack_from 8, 'G' |
| 86 |
end |
| 87 |
|
| 88 |
{% when Type::Boolean -%} |
| 89 |
|
| 90 |
def readBool |
| 91 |
v = unpack_from 1, 'c' |
| 92 |
|
| 93 |
return false if v == 0 |
| 94 |
return true if v == 1 |
| 95 |
|
| 96 |
raise InternalError, 'Unexpected byte for Boolean type' |
| 97 |
end |
| 98 |
|
| 99 |
{% when Type::String -%} |
| 100 |
|
| 101 |
def readString |
| 102 |
size = unpack_from 4, 'l>' |
| 103 |
|
| 104 |
raise InternalError, 'Unexpected negative string length' if size.negative? |
| 105 |
|
| 106 |
read(size).force_encoding(Encoding::UTF_8) |
| 107 |
end |
| 108 |
|
| 109 |
{% when Type::Bytes -%} |
| 110 |
|
| 111 |
def readBytes |
| 112 |
size = unpack_from 4, 'l>' |
| 113 |
|
| 114 |
raise InternalError, 'Unexpected negative byte string length' if size.negative? |
| 115 |
|
| 116 |
read(size).force_encoding(Encoding::BINARY) |
| 117 |
end |
| 118 |
|
| 119 |
{% when Type::Timestamp -%} |
| 120 |
# The Timestamp type. |
| 121 |
ONE_SECOND_IN_NANOSECONDS = 10**9 |
| 122 |
|
| 123 |
def read{{ canonical_type_name }} |
| 124 |
seconds = unpack_from 8, 'q>' |
| 125 |
nanoseconds = unpack_from 4, 'L>' |
| 126 |
|
| 127 |
# UniFFi conventions assume that nanoseconds part has to represent nanoseconds portion of |
| 128 |
# duration between epoch and the timestamp moment. Ruby `Time#tv_nsec` returns the number of |
| 129 |
# nanoseconds for the subsecond part, which is sort of opposite to "duration" meaning. |
| 130 |
# Hence we need to convert value returned by `Time#tv_nsec` back and forth with the following |
| 131 |
# logic: |
| 132 |
if seconds < 0 && nanoseconds != 0 |
| 133 |
# In order to get duration nsec we shift by 1 second: |
| 134 |
nanoseconds = ONE_SECOND_IN_NANOSECONDS - nanoseconds |
| 135 |
|
| 136 |
# Then we compensate 1 second shift: |
| 137 |
seconds -= 1 |
| 138 |
end |
| 139 |
|
| 140 |
Time.at(seconds, nanoseconds, :nanosecond, in: '+00:00').utc |
| 141 |
end |
| 142 |
|
| 143 |
{% when Type::Duration -%} |
| 144 |
# The Duration type. |
| 145 |
|
| 146 |
def read{{ canonical_type_name }} |
| 147 |
seconds = unpack_from 8, 'q>' |
| 148 |
nanoseconds = unpack_from 4, 'L>' |
| 149 |
|
| 150 |
Time.at(seconds, nanoseconds, :nanosecond, in: '+00:00').utc |
| 151 |
end |
| 152 |
|
| 153 |
{% when Type::Object with { name: object_name, .. } -%} |
| 154 |
# The Object type {{ object_name }}. |
| 155 |
|
| 156 |
def read{{ canonical_type_name }} |
| 157 |
pointer = FFI::Pointer.new unpack_from 8, 'Q>' |
| 158 |
return {{ object_name|class_name_rb }}.uniffi_allocate(pointer) |
| 159 |
end |
| 160 |
|
| 161 |
{% when Type::Enum { name, .. } -%} |
| 162 |
{%- let e = ci.get_enum_definition(name).unwrap() -%} |
| 163 |
{% if !ci.is_name_used_as_error(name) %} |
| 164 |
{% let enum_name = name %} |
| 165 |
# The Enum type {{ enum_name }}. |
| 166 |
|
| 167 |
def read{{ canonical_type_name }} |
| 168 |
variant = unpack_from 4, 'l>' |
| 169 |
{% if e.is_flat() -%} |
| 170 |
{%- for variant in e.variants() %} |
| 171 |
if variant == {{ loop.index }} |
| 172 |
return {{ enum_name|class_name_rb }}::{{ variant.name()|enum_name_rb }} |
| 173 |
end |
| 174 |
{%- endfor %} |
| 175 |
|
| 176 |
raise InternalError, 'Unexpected variant tag for {{ canonical_type_name }}' |
| 177 |
{%- else -%} |
| 178 |
{%- for variant in e.variants() %} |
| 179 |
if variant == {{ loop.index }} |
| 180 |
{%- if variant.has_fields() %} |
| 181 |
return {{ enum_name|class_name_rb }}::{{ variant.name()|enum_name_rb }}.new( |
| 182 |
{%- for field in variant.fields() %} |
| 183 |
self.read{{ self::canonical_name(field.as_type().borrow()).borrow()|class_name_rb }}(){% if loop.last %}{% else %},{% endif %} |
| 184 |
{%- endfor %} |
| 185 |
) |
| 186 |
{%- else %} |
| 187 |
return {{ enum_name|class_name_rb }}::{{ variant.name()|enum_name_rb }}.new |
| 188 |
{% endif %} |
| 189 |
end |
| 190 |
{%- endfor %} |
| 191 |
raise InternalError, 'Unexpected variant tag for {{ canonical_type_name }}' |
| 192 |
{%- endif %} |
| 193 |
end |
| 194 |
|
| 195 |
{% else %} |
| 196 |
|
| 197 |
{% let error_name = name %} |
| 198 |
|
| 199 |
# The Error type {{ error_name }} |
| 200 |
|
| 201 |
def read{{ canonical_type_name }} |
| 202 |
variant = unpack_from 4, 'l>' |
| 203 |
{% if e.is_flat() -%} |
| 204 |
{%- for variant in e.variants() %} |
| 205 |
if variant == {{ loop.index }} |
| 206 |
return {{ error_name|class_name_rb }}::{{ variant.name()|class_name_rb }}.new( |
| 207 |
readString() |
| 208 |
) |
| 209 |
end |
| 210 |
{%- endfor %} |
| 211 |
|
| 212 |
raise InternalError, 'Unexpected variant tag for {{ canonical_type_name }}' |
| 213 |
{%- else -%} |
| 214 |
{%- for variant in e.variants() %} |
| 215 |
if variant == {{ loop.index }} |
| 216 |
{%- if variant.has_fields() %} |
| 217 |
return {{ error_name|class_name_rb }}::{{ variant.name()|class_name_rb }}.new( |
| 218 |
{%- for field in variant.fields() %} |
| 219 |
read{{ self::canonical_name(field.as_type().borrow()).borrow()|class_name_rb }}(){% if loop.last %}{% else %},{% endif %} |
| 220 |
{%- endfor %} |
| 221 |
) |
| 222 |
{%- else %} |
| 223 |
return {{ error_name|class_name_rb }}::{{ variant.name()|class_name_rb }}.new |
| 224 |
{%- endif %} |
| 225 |
end |
| 226 |
{%- endfor %} |
| 227 |
|
| 228 |
raise InternalError, 'Unexpected variant tag for {{ canonical_type_name }}' |
| 229 |
{%- endif %} |
| 230 |
end |
| 231 |
{% endif %} |
| 232 |
|
| 233 |
{% when Type::Record { name: record_name, .. } -%} |
| 234 |
{%- let rec = ci.get_record_definition(record_name).unwrap() -%} |
| 235 |
# The Record type {{ record_name }}. |
| 236 |
|
| 237 |
def read{{ canonical_type_name }} |
| 238 |
{{ rec.name()|class_name_rb }}.new( |
| 239 |
{%- for field in rec.fields() %} |
| 240 |
{{ field.name()|var_name_rb }}: read{{ self::canonical_name(field.as_type().borrow()).borrow()|class_name_rb }}{% if loop.last %}{% else %},{% endif %} |
| 241 |
{%- endfor %} |
| 242 |
) |
| 243 |
end |
| 244 |
|
| 245 |
{% when Type::Optional { inner_type } -%} |
| 246 |
# The Optional<T> type for {{ self::canonical_name(inner_type) }}. |
| 247 |
|
| 248 |
def read{{ canonical_type_name }} |
| 249 |
flag = unpack_from 1, 'c' |
| 250 |
|
| 251 |
if flag == 0 |
| 252 |
return nil |
| 253 |
elsif flag == 1 |
| 254 |
return read{{ self::canonical_name(inner_type).borrow()|class_name_rb }} |
| 255 |
else |
| 256 |
raise InternalError, 'Unexpected flag byte for {{ canonical_type_name }}' |
| 257 |
end |
| 258 |
end |
| 259 |
|
| 260 |
{% when Type::Sequence { inner_type } -%} |
| 261 |
# The Sequence<T> type for {{ self::canonical_name(inner_type) }}. |
| 262 |
|
| 263 |
def read{{ canonical_type_name }} |
| 264 |
count = unpack_from 4, 'l>' |
| 265 |
|
| 266 |
raise InternalError, 'Unexpected negative sequence length' if count.negative? |
| 267 |
|
| 268 |
items = [] |
| 269 |
|
| 270 |
count.times do |
| 271 |
items.append read{{ self::canonical_name(inner_type).borrow()|class_name_rb }} |
| 272 |
end |
| 273 |
|
| 274 |
items |
| 275 |
end |
| 276 |
|
| 277 |
{% when Type::Map { key_type: k, value_type: inner_type } -%} |
| 278 |
# The Map<T> type for {{ self::canonical_name(inner_type) }}. |
| 279 |
|
| 280 |
def read{{ canonical_type_name }} |
| 281 |
count = unpack_from 4, 'l>' |
| 282 |
raise InternalError, 'Unexpected negative map size' if count.negative? |
| 283 |
|
| 284 |
items = {} |
| 285 |
count.times do |
| 286 |
key = readString |
| 287 |
items[key] = read{{ self::canonical_name(inner_type).borrow()|class_name_rb }} |
| 288 |
end |
| 289 |
|
| 290 |
items |
| 291 |
end |
| 292 |
{%- else -%} |
| 293 |
# This type is not yet supported in the Ruby backend. |
| 294 |
def read{{ canonical_type_name }} |
| 295 |
raise InternalError, 'RustBufferStream.read not implemented yet for {{ canonical_type_name }}' |
| 296 |
end |
| 297 |
|
| 298 |
{%- endmatch -%} |
| 299 |
{%- endfor %} |
| 300 |
|
| 301 |
def unpack_from(size, format) |
| 302 |
raise InternalError, 'read past end of rust buffer' if @offset + size > @rbuf.len |
| 303 |
|
| 304 |
value = @rbuf.data.get_bytes(@offset, size).unpack format |
| 305 |
|
| 306 |
@offset += size |
| 307 |
|
| 308 |
# TODO: verify this |
| 309 |
raise 'more than one element!!!' if value.size > 1 |
| 310 |
|
| 311 |
value[0] |
| 312 |
end |
| 313 |
end |
| 314 |
|
| 315 |
private_constant :RustBufferStream |