From 0ebf5bdf043a27fd3dfb7f92e0cb63d88954c44d Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 03:47:29 +0200 Subject: Adding upstream version 115.8.0esr. Signed-off-by: Daniel Baumann --- .../uniffi-example-custom-types/Cargo.toml | 21 +++++++++ .../uniffi-example-custom-types/build.rs | 7 +++ .../src/custom-types.udl | 14 ++++++ .../uniffi-example-custom-types/src/lib.rs | 52 ++++++++++++++++++++++ .../tests/bindings/test_custom_types.kts | 21 +++++++++ .../tests/bindings/test_custom_types.swift | 20 +++++++++ .../tests/test_generated_bindings.rs | 7 +++ .../uniffi-example-custom-types/uniffi.toml | 40 +++++++++++++++++ 8 files changed, 182 insertions(+) create mode 100644 toolkit/components/uniffi-example-custom-types/Cargo.toml create mode 100644 toolkit/components/uniffi-example-custom-types/build.rs create mode 100644 toolkit/components/uniffi-example-custom-types/src/custom-types.udl create mode 100644 toolkit/components/uniffi-example-custom-types/src/lib.rs create mode 100644 toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.kts create mode 100644 toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.swift create mode 100644 toolkit/components/uniffi-example-custom-types/tests/test_generated_bindings.rs create mode 100644 toolkit/components/uniffi-example-custom-types/uniffi.toml (limited to 'toolkit/components/uniffi-example-custom-types') diff --git a/toolkit/components/uniffi-example-custom-types/Cargo.toml b/toolkit/components/uniffi-example-custom-types/Cargo.toml new file mode 100644 index 0000000000..5be9bc16d2 --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "uniffi-example-custom-types" +edition = "2021" +version = "0.19.6" +authors = ["Firefox Sync Team "] +license = "MPL-2.0" +publish = false + +[lib] +crate-type = ["lib", "cdylib"] +name = "uniffi_custom_types" + +[dependencies] +anyhow = "1" +bytes = "1.0" +serde_json = "1" +uniffi = { workspace = true } +url = "2.1" + +[build-dependencies] +uniffi = { workspace = true, features = ["build"] } diff --git a/toolkit/components/uniffi-example-custom-types/build.rs b/toolkit/components/uniffi-example-custom-types/build.rs new file mode 100644 index 0000000000..10b6d220da --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/build.rs @@ -0,0 +1,7 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +fn main() { + uniffi::generate_scaffolding("./src/custom-types.udl").unwrap(); +} diff --git a/toolkit/components/uniffi-example-custom-types/src/custom-types.udl b/toolkit/components/uniffi-example-custom-types/src/custom-types.udl new file mode 100644 index 0000000000..8fef8ff41a --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/src/custom-types.udl @@ -0,0 +1,14 @@ +[Custom] +typedef string Url; + +[Custom] +typedef i64 Handle; + +dictionary CustomTypesDemo { + Url url; + Handle handle; +}; + +namespace custom_types { + CustomTypesDemo get_custom_types_demo(CustomTypesDemo? demo); +}; diff --git a/toolkit/components/uniffi-example-custom-types/src/lib.rs b/toolkit/components/uniffi-example-custom-types/src/lib.rs new file mode 100644 index 0000000000..61aea7a11a --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/src/lib.rs @@ -0,0 +1,52 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +use url::Url; + +// Custom Handle type which trivially wraps an i64. +pub struct Handle(pub i64); + +// We must implement the UniffiCustomTypeConverter trait for each custom type on the scaffolding side +impl UniffiCustomTypeConverter for Handle { + // The `Builtin` type will be used to marshall values across the FFI + type Builtin = i64; + + // Convert Builtin to our custom type + fn into_custom(val: Self::Builtin) -> uniffi::Result { + Ok(Handle(val)) + } + + // Convert our custom type to Builtin + fn from_custom(obj: Self) -> Self::Builtin { + obj.0 + } +} + +// Use `url::Url` as a custom type, with `String` as the Builtin +impl UniffiCustomTypeConverter for Url { + type Builtin = String; + + fn into_custom(val: Self::Builtin) -> uniffi::Result { + Ok(Url::parse(&val)?) + } + + fn from_custom(obj: Self) -> Self::Builtin { + obj.to_string() + } +} + +// And a little struct and function that ties them together. +pub struct CustomTypesDemo { + url: Url, + handle: Handle, +} + +pub fn get_custom_types_demo(v: Option) -> CustomTypesDemo { + v.unwrap_or_else(|| CustomTypesDemo { + url: Url::parse("http://example.com/").unwrap(), + handle: Handle(123), + }) +} + +include!(concat!(env!("OUT_DIR"), "/custom-types.uniffi.rs")); diff --git a/toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.kts b/toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.kts new file mode 100644 index 0000000000..d75bd99eee --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.kts @@ -0,0 +1,21 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import java.net.URL + +import customtypes.* + +// TODO: use an actual test runner. + +// Get the custom types and check their data +val demo = getCustomTypesDemo(null) +// URL is customized on the bindings side +assert(demo.url == URL("http://example.com/")) +// Handle isn't so it appears as a plain Long +assert(demo.handle == 123L) + +// Change some data and ensure that the round-trip works +demo.url = URL("http://new.example.com/") +demo.handle = 456; +assert(demo == getCustomTypesDemo(demo)) diff --git a/toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.swift b/toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.swift new file mode 100644 index 0000000000..5aaf6ff3b0 --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.swift @@ -0,0 +1,20 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import custom_types +import Foundation + +// TODO: use an actual test runner. + +do { + // Test simple values. + var demo = getCustomTypesDemo(demo: nil) + assert(demo.url == URL(string: "http://example.com/")) + assert(demo.handle == 123) + + // Change some data and ensure that the round-trip works + demo.url = URL(string: "http://new.example.com/")! + demo.handle = 456 + assert(demo == getCustomTypesDemo(demo: demo)) +} diff --git a/toolkit/components/uniffi-example-custom-types/tests/test_generated_bindings.rs b/toolkit/components/uniffi-example-custom-types/tests/test_generated_bindings.rs new file mode 100644 index 0000000000..38db89790e --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/tests/test_generated_bindings.rs @@ -0,0 +1,7 @@ +uniffi::build_foreign_language_testcases!( + ["src/custom-types.udl"], + [ + "tests/bindings/test_custom_types.kts", + "tests/bindings/test_custom_types.swift", + ] +); diff --git a/toolkit/components/uniffi-example-custom-types/uniffi.toml b/toolkit/components/uniffi-example-custom-types/uniffi.toml new file mode 100644 index 0000000000..873fa090fb --- /dev/null +++ b/toolkit/components/uniffi-example-custom-types/uniffi.toml @@ -0,0 +1,40 @@ +[bindings.swift] +cdylib_name = "custom_types" + +[bindings.swift.custom_types.Url] +# Name of the type in the Swift code +type_name = "URL" +# Modules that need to be imported +imports = ["Foundation"] +# Functions to convert between strings and URLs +into_custom = "URL(string: {})!" +from_custom = "String(describing: {})" + +[bindings.kotlin] +cdylib_name = "custom_types" +package_name = "customtypes" + +[bindings.kotlin.custom_types.Url] +# Name of the type in the Kotlin code +type_name = "URL" +# Classes that need to be imported +imports = [ "java.net.URL" ] +# Functions to convert between strings and URLs +into_custom = "URL({})" +from_custom = "{}.toString()" + +[bindings.python] +cdylib_name = "custom_types" + +[bindings.python.custom_types.Url] +# We're going to be the urllib.parse.ParseResult class, which is the closest +# thing Python has to a Url class. No need to specify `type_name` though, +# since Python is loosely typed. +# modules to import +imports = ["urllib.parse"] +# Functions to convert between strings and the ParsedUrl class +into_custom = "urllib.parse.urlparse({})" +from_custom = "urllib.parse.urlunparse({})" + +[bindings.ruby] +cdylib_name = "custom_types" -- cgit v1.2.3