Skip to content

Commit a9c0c0d

Browse files
committed
Example Component
Created an example component. I'm hoping this eases the work required to create a new component and serves as a starting point for engineers that are new to cross-platform component development.
1 parent 5fb3395 commit a9c0c0d

File tree

16 files changed

+1541
-1
lines changed

16 files changed

+1541
-1
lines changed

.cargo/config.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@ suggest-debug-ingestion-sizes = ["run", "-p", "suggest", "--bin", "debug_ingesti
1010
relevancy = ["run", "-p", "examples-relevancy-cli", "--"]
1111
suggest = ["run", "-p", "examples-suggest-cli", "--"]
1212
remote-settings = ["run", "-p", "examples-remote-settings-cli", "--"]
13+
example = ["run", "-p", "examples-example-cli", "--"]
1314
start-bindings = ["run", "-p", "start-bindings", "--"]

Cargo.lock

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ members = [
66
"components/as-ohttp-client",
77
"components/autofill",
88
"components/crashtest",
9+
"components/example",
10+
#"components/example/cli",
911
"components/fxa-client",
1012
"components/logins",
1113
"components/merino",

components/example/Cargo.toml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Cargo toml example
2+
[package]
3+
name = "example-component"
4+
version = "0.1.0"
5+
edition = "2021"
6+
license = "MPL-2.0"
7+
8+
# Dependency tips:
9+
# * Try to match the version number from an existing component
10+
# * Be careful when adding new dependencies, since they will require an audit when vendoring the
11+
# code into moz-central.
12+
[dependencies]
13+
# UniFFI is a dependency for any shared Rust component, make the version match what the other
14+
# components are using.
15+
uniffi = { version = "0.28.2" }
16+
# app-services support crates that you probably want to use.
17+
error-support = { path = "../support/error" }
18+
interrupt-support = { path = "../support/interrupt" }
19+
sql-support = { path = "../support/sql" }
20+
# parking_lot is for Mutex and other synchronization primitives.
21+
parking_lot = "0.12"
22+
# Rust bindings for SQLite
23+
rusqlite = { version = "0.31.0", features = ["bundled"] }
24+
# viaduct is our HTTP client, you'll probably also want serde + serde_json to handle
25+
# serialization/deserialization and url to generate request URLs.
26+
viaduct = { path = "../viaduct" }
27+
serde = { version = "1", features=["derive"] }
28+
serde_json = "1"
29+
url = "2"
30+
# Useful Rust crates
31+
log = "0.4"
32+
thiserror = "1.0"
33+
34+
[dev-dependencies]

components/example/src/README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
This crate is an example Rust component. It aims to show how components are generally structured
2+
and the best practices to use when creating one.
3+
4+
For this example, we're going to build the classic TODO app example. Our app will feature:
5+
6+
* UniFFI-generated Kotlin/Swift bindings
7+
* SQLite persistence
8+
* Error reporting, that would report to Sentry if we used this in firefox-android
9+
* An HTTP REST client.
10+
11+
Each file focuses on a particular part of the component:
12+
13+
* `lib.rs`: Defining a public API and using UniFFI to expose it
14+
* `error.rs`: Error hierarchies and error reporting
15+
* `schema.rs`: SQLite schema migrations
16+
* `db.rs`: SQLite DB operations
17+
* `http.rs`: HTTP requests
18+
* `../Cargo.toml`: Dependency management
19+
* `../../../examples/example-cli`: Creating a CLI for your component
20+
21+
Feel free to ignore files that don't relate to your component and to copy+paste the ones that do.

components/example/src/cli/Cargo.toml

Whitespace-only changes.

0 commit comments

Comments
 (0)