Debian Codegraph @

third_party/rust/uniffi_bindgen/src/bindings/python/templates/StringHelper.py

raw
1 class _UniffiConverterString:
2 @staticmethod
3 def check_lower(value):
4 if not isinstance(value, str):
5 raise TypeError("argument must be str, not {}".format(type(value).__name__))
6 return value
7
8 @staticmethod
9 def read(buf):
10 size = buf.read_i32()
11 if size < 0:
12 raise InternalError("Unexpected negative string length")
13 utf8_bytes = buf.read(size)
14 return utf8_bytes.decode("utf-8")
15
16 @staticmethod
17 def write(value, buf):
18 utf8_bytes = value.encode("utf-8")
19 buf.write_i32(len(utf8_bytes))
20 buf.write(utf8_bytes)
21
22 @staticmethod
23 def lift(buf):
24 with buf.consume_with_stream() as stream:
25 return stream.read(stream.remaining()).decode("utf-8")
26
27 @staticmethod
28 def lower(value):
29 with _UniffiRustBuffer.alloc_with_builder() as builder:
30 builder.write(value.encode("utf-8"))
31 return builder.finalize()