From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- .../src/bindings/python/templates/StringHelper.py | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 third_party/rust/uniffi_bindgen/src/bindings/python/templates/StringHelper.py (limited to 'third_party/rust/uniffi_bindgen/src/bindings/python/templates/StringHelper.py') diff --git a/third_party/rust/uniffi_bindgen/src/bindings/python/templates/StringHelper.py b/third_party/rust/uniffi_bindgen/src/bindings/python/templates/StringHelper.py new file mode 100644 index 0000000000..40890b6abc --- /dev/null +++ b/third_party/rust/uniffi_bindgen/src/bindings/python/templates/StringHelper.py @@ -0,0 +1,33 @@ +class _UniffiConverterString: + @staticmethod + def check(value): + if not isinstance(value, str): + raise TypeError("argument must be str, not {}".format(type(value).__name__)) + return value + + @staticmethod + def read(buf): + size = buf.read_i32() + if size < 0: + raise InternalError("Unexpected negative string length") + utf8_bytes = buf.read(size) + return utf8_bytes.decode("utf-8") + + @staticmethod + def write(value, buf): + value = _UniffiConverterString.check(value) + utf8_bytes = value.encode("utf-8") + buf.write_i32(len(utf8_bytes)) + buf.write(utf8_bytes) + + @staticmethod + def lift(buf): + with buf.consume_with_stream() as stream: + return stream.read(stream.remaining()).decode("utf-8") + + @staticmethod + def lower(value): + value = _UniffiConverterString.check(value) + with _UniffiRustBuffer.alloc_with_builder() as builder: + builder.write(value.encode("utf-8")) + return builder.finalize() -- cgit v1.2.3