Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a Rust component for a Merino client (#5345) #5346

Closed
wants to merge 12 commits into from
Closed
7 changes: 7 additions & 0 deletions .buildconfig-android.yml
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ projects:
- name: nimbus
type: aar
description: The Nimbus SDK, compiled to work with the Application Services full-megazord.
merinoclient:
linabutler marked this conversation as resolved.
Show resolved Hide resolved
path: components/merino_client/android
artifactId: merinoclient
publications:
- name: merinoclient
type: aar
description: Firefox Suggest Service client.
full-megazord:
uploadSymbols: true
path: megazords/full/android
Expand Down
21 changes: 21 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"components/crashtest",
"components/fxa-client",
"components/logins",
"components/merino_client",
"components/nimbus",
"components/places",
"components/push",
Expand Down
26 changes: 26 additions & 0 deletions components/merino_client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[package]
name = "merino_client"
edition = "2021"
version = "0.1.0"
authors = ["contextual-services-team@mozilla.com"]
license = "MPL-2.0"
exclude = ["/android", "/ios"]

[dependencies]
anyhow = "1.0"
error-support = { path = "../support/error" }
log = "0.4"
serde = "1"
serde_derive = "1"
serde_json = "1"
thiserror = "1.0"
uniffi = "^0.21"
uniffi_macros = "^0.21"
url = "2.1" # mozilla-central can't yet take 2.2 (see bug 1734538)
linabutler marked this conversation as resolved.
Show resolved Hide resolved
viaduct = { path = "../viaduct" }
linabutler marked this conversation as resolved.
Show resolved Hide resolved

[build-dependencies]
uniffi_build = { version = "^0.21", features = [ "builtin-bindgen" ]}

[dev-dependencies]
viaduct-reqwest = { path = "../support/viaduct-reqwest" }
7 changes: 7 additions & 0 deletions components/merino_client/android/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

apply from: "$rootDir/build-scripts/component-common.gradle"
apply from: "$rootDir/publish.gradle"

ext.configureUniFFIBindgen("../src/merino_client.udl")
ext.dependsOnTheMegazord()
ext.configurePublish()
21 changes: 21 additions & 0 deletions components/merino_client/android/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
2 changes: 2 additions & 0 deletions components/merino_client/android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="org.mozilla.appservices.merinoclient" />
7 changes: 7 additions & 0 deletions components/merino_client/build.rs
Original file line number Diff line number Diff line change
@@ -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_build::generate_scaffolding("./src/merino_client.udl").unwrap();
}
49 changes: 49 additions & 0 deletions components/merino_client/src/client.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* 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 serde_derive::*;

use crate::error::{MerinoClientError, Result};

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MerinoClientOptions {
linabutler marked this conversation as resolved.
Show resolved Hide resolved
pub endpoint_url: String,
pub client_variants: Vec<String>,
pub timeout_ms: i64,
pub providers: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct MerinoClientFetchOptions {
pub providers: Vec<String>,
pub timeout_ms: i64,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct MerinoSuggestion {
pub title: String,
pub url: String,
pub provider: String,
pub is_sponsored: bool,
pub score: f64,
pub icon: Option<String>,
pub request_id: String,
pub client_variants: Vec<String>,
}

pub struct MerinoClient {
// ...
}

impl MerinoClient {
pub fn new(options: MerinoClientOptions) -> Self {
Self {
// ...
}
}

pub fn fetch(&self, query: &str, options: Option<MerinoClientFetchOptions>) -> Result<Vec<MerinoSuggestion>> {
Err(MerinoClientError::Other { reason: "Not implemented".into() })
}
}
11 changes: 11 additions & 0 deletions components/merino_client/src/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/* 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/. */

pub type Result<T> = std::result::Result<T, MerinoClientError>;

#[derive(Debug, thiserror::Error)]
pub enum MerinoClientError {
#[error("Other: {reason}")]
Other { reason: String },
}
18 changes: 18 additions & 0 deletions components/merino_client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* 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/. */

mod client;
mod error;

pub use crate::{
client::{
MerinoClient,
MerinoClientOptions,
MerinoClientFetchOptions,
MerinoSuggestion,
},
error::MerinoClientError,
};

uniffi_macros::include_scaffolding!("merino_client");
38 changes: 38 additions & 0 deletions components/merino_client/src/merino_client.udl
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
namespace merino_client {
linabutler marked this conversation as resolved.
Show resolved Hide resolved

};

[Error]
interface MerinoClientError {
Other(string reason);
};

dictionary MerinoClientOptions {
string endpoint_url;
sequence<string> client_variants;
i64 timeout_ms;
sequence<string> providers;
};

dictionary MerinoClientFetchOptions {
sequence<string> providers;
i64 timeout_ms;
};

dictionary MerinoSuggestion {
string title;
string url;
string provider;
boolean is_sponsored;
f64 score;
string? icon;
string request_id;
sequence<string> client_variants;
};

interface MerinoClient {
constructor(MerinoClientOptions options);

[Throws=MerinoClientError]
sequence<MerinoSuggestion> fetch([ByRef] string query, MerinoClientFetchOptions? options);
};
8 changes: 8 additions & 0 deletions components/merino_client/uniffi.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[bindings.kotlin]
package_name = "mozilla.appservices.merinoclient"
cdylib_name = "megazord"

[bindings.swift]
ffi_module_name = "MozillaRustComponents"
ffi_module_filename = "merino_clientFFI"
generate_module_map = false
1 change: 1 addition & 0 deletions megazords/full/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ nimbus-sdk = { path = "../../components/nimbus" }
autofill = { path = "../../components/autofill" }
crashtest = { path = "../../components/crashtest" }
error-support = { path = "../../components/support/error" }
merino_client = { path = "../../components/merino_client" }
linabutler marked this conversation as resolved.
Show resolved Hide resolved

lazy_static = "1.4"
1 change: 1 addition & 0 deletions megazords/full/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub use crashtest;
pub use error_support;
pub use fxa_client;
pub use logins;
pub use merino_client;
pub use nimbus;
pub use places;
pub use push;
Expand Down
1 change: 1 addition & 0 deletions megazords/ios-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ tabs = { path = "../../components/tabs", features = ["full-sync"] }
places = {path = "../../components/places" }
sync15 = {path = "../../components/sync15"}
error-support = { path = "../../components/support/error" }
merino_client = { path = "../../components/merino_client" }
1 change: 1 addition & 0 deletions megazords/ios-rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use crashtest;
pub use error_support;
pub use fxa_client;
pub use logins;
pub use merino_client;
pub use nimbus;
pub use places;
pub use push;
Expand Down