summaryrefslogtreecommitdiffstats
path: root/toolkit/components/uniffi-example-custom-types
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 17:32:43 +0000
commit6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch)
treea68f146d7fa01f0134297619fbe7e33db084e0aa /toolkit/components/uniffi-example-custom-types
parentInitial commit. (diff)
downloadthunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.tar.xz
thunderbird-6bf0a5cb5034a7e684dcc3500e841785237ce2dd.zip
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'toolkit/components/uniffi-example-custom-types')
-rw-r--r--toolkit/components/uniffi-example-custom-types/Cargo.toml21
-rw-r--r--toolkit/components/uniffi-example-custom-types/build.rs7
-rw-r--r--toolkit/components/uniffi-example-custom-types/src/custom-types.udl14
-rw-r--r--toolkit/components/uniffi-example-custom-types/src/lib.rs52
-rw-r--r--toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.kts21
-rw-r--r--toolkit/components/uniffi-example-custom-types/tests/bindings/test_custom_types.swift20
-rw-r--r--toolkit/components/uniffi-example-custom-types/tests/test_generated_bindings.rs7
-rw-r--r--toolkit/components/uniffi-example-custom-types/uniffi.toml40
8 files changed, 182 insertions, 0 deletions
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 <sync-team@mozilla.com>"]
+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<Self> {
+ 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<Self> {
+ 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>) -> 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"