Skip to content

Commit 5f0f404

Browse files
authored
merino/relevancy/suggest: Refactorings for uniffi_parse_rs (#7419)
I'm working on a new UniFFI parser (mozilla/uniffi-rs#2841) and it currently has a couple extra requirements for types used in exported functions: * The types are publicly available from other crates. * The name matches the one used in the `custom_type!` macro. * Macro expansion is not supported, the types need to be written out by hand. I could maybe rework the parser to avoid these requirements, but the first two the changes feel like improvements to me. Not being able to use macros for UniFFIed types is worse but I don't think it will affect us all that much in practice. Please push back if you don't think so.
1 parent 76c16f2 commit 5f0f404

6 files changed

Lines changed: 89 additions & 58 deletions

File tree

components/merino/src/curated_recommendations/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ pub mod models;
1616
#[cfg(test)]
1717
mod tests;
1818

19-
use crate::curated_recommendations::models::locale::CuratedRecommendationLocale;
19+
pub use crate::curated_recommendations::models::locale::CuratedRecommendationLocale;
2020
use crate::curated_recommendations::models::request::CuratedRecommendationsConfig;
2121
use crate::curated_recommendations::models::request::CuratedRecommendationsRequest;
2222
use crate::curated_recommendations::models::response::CuratedRecommendationsResponse;
23-
pub use error::{ApiResult, Error, Result};
23+
pub use error::{ApiResult, CuratedRecommendationsApiError, Error, Result};
2424
use error_support::handle_error;
2525
use url::Url;
2626

components/merino/src/curated_recommendations/models/locale.rs

Lines changed: 79 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -5,59 +5,87 @@
55

66
use serde::{Deserialize, Serialize};
77

8-
/// Defines the `CuratedRecommendationLocale` enum along with `all_locales()` and
9-
/// `from_locale_string()` methods, ensuring the variant list is specified exactly once.
10-
macro_rules! define_locales {
11-
( $( $variant:ident => $str:literal ),+ $(,)? ) => {
12-
/// Locales supported by Merino curated recommendations.
13-
///
14-
/// Each variant maps to a BCP 47 locale string (e.g. `"en-US"`, `"fr"`) used when
15-
/// requesting recommendations from the Merino API.
16-
#[derive(Debug, Serialize, PartialEq, Deserialize, uniffi::Enum)]
17-
pub enum CuratedRecommendationLocale {
18-
$(
19-
#[serde(rename = $str)]
20-
$variant,
21-
)+
22-
}
23-
24-
impl CuratedRecommendationLocale {
25-
/// Returns all supported locale strings (e.g. `"en-US"`, `"fr-FR"`).
26-
///
27-
/// These strings are the canonical serialized values of the enum variants.
28-
pub fn all_locales() -> Vec<String> {
29-
vec![ $( $str.to_string(), )+ ]
30-
}
31-
32-
/// Parses a locale string (e.g. `"en-US"`) into a `CuratedRecommendationLocale`
33-
/// enum variant.
34-
///
35-
/// Returns `None` if the string does not match a known variant.
36-
pub fn from_locale_string(locale: String) -> Option<CuratedRecommendationLocale> {
37-
match locale.as_str() {
38-
$( $str => Some(CuratedRecommendationLocale::$variant), )+
39-
_ => None,
40-
}
41-
}
42-
}
43-
};
8+
/// Locales supported by Merino curated recommendations.
9+
///
10+
/// Each variant maps to a BCP 47 locale string (e.g. `"en-US"`, `"fr"`) used when
11+
/// requesting recommendations from the Merino API.
12+
#[derive(Debug, Serialize, PartialEq, Deserialize, uniffi::Enum)]
13+
pub enum CuratedRecommendationLocale {
14+
#[serde(rename = "fr")]
15+
Fr,
16+
#[serde(rename = "fr-FR")]
17+
FrFr,
18+
#[serde(rename = "es")]
19+
Es,
20+
#[serde(rename = "es-ES")]
21+
EsEs,
22+
#[serde(rename = "it")]
23+
It,
24+
#[serde(rename = "it-IT")]
25+
ItIt,
26+
#[serde(rename = "en")]
27+
En,
28+
#[serde(rename = "en-CA")]
29+
EnCa,
30+
#[serde(rename = "en-GB")]
31+
EnGb,
32+
#[serde(rename = "en-US")]
33+
EnUs,
34+
#[serde(rename = "de")]
35+
De,
36+
#[serde(rename = "de-DE")]
37+
DeDe,
38+
#[serde(rename = "de-AT")]
39+
DeAt,
40+
#[serde(rename = "de-CH")]
41+
DeCh,
4442
}
43+
impl CuratedRecommendationLocale {
44+
/// Returns all supported locale strings (e.g. `"en-US"`, `"fr-FR"`).
45+
///
46+
/// These strings are the canonical serialized values of the enum variants.
47+
pub fn all_locales() -> Vec<String> {
48+
vec![
49+
"fr".to_string(),
50+
"fr-FR".to_string(),
51+
"es".to_string(),
52+
"es-ES".to_string(),
53+
"it".to_string(),
54+
"it-IT".to_string(),
55+
"en".to_string(),
56+
"en-CA".to_string(),
57+
"en-GB".to_string(),
58+
"en-US".to_string(),
59+
"de".to_string(),
60+
"de-DE".to_string(),
61+
"de-AT".to_string(),
62+
"de-CH".to_string(),
63+
]
64+
}
4565

46-
define_locales! {
47-
Fr => "fr",
48-
FrFr => "fr-FR",
49-
Es => "es",
50-
EsEs => "es-ES",
51-
It => "it",
52-
ItIt => "it-IT",
53-
En => "en",
54-
EnCa => "en-CA",
55-
EnGb => "en-GB",
56-
EnUs => "en-US",
57-
De => "de",
58-
DeDe => "de-DE",
59-
DeAt => "de-AT",
60-
DeCh => "de-CH",
66+
/// Parses a locale string (e.g. `"en-US"`) into a `CuratedRecommendationLocale`
67+
/// enum variant.
68+
///
69+
/// Returns `None` if the string does not match a known variant.
70+
pub fn from_locale_string(locale: String) -> Option<CuratedRecommendationLocale> {
71+
match locale.as_str() {
72+
"fr" => Some(CuratedRecommendationLocale::Fr),
73+
"fr-FR" => Some(CuratedRecommendationLocale::FrFr),
74+
"es" => Some(CuratedRecommendationLocale::Es),
75+
"es-ES" => Some(CuratedRecommendationLocale::EsEs),
76+
"it" => Some(CuratedRecommendationLocale::It),
77+
"it-IT" => Some(CuratedRecommendationLocale::ItIt),
78+
"en" => Some(CuratedRecommendationLocale::En),
79+
"en-CA" => Some(CuratedRecommendationLocale::EnCa),
80+
"en-GB" => Some(CuratedRecommendationLocale::EnGb),
81+
"en-US" => Some(CuratedRecommendationLocale::EnUs),
82+
"de" => Some(CuratedRecommendationLocale::De),
83+
"de-DE" => Some(CuratedRecommendationLocale::DeDe),
84+
"de-AT" => Some(CuratedRecommendationLocale::DeAt),
85+
"de-CH" => Some(CuratedRecommendationLocale::DeCh),
86+
_ => None,
87+
}
88+
}
6189
}
6290

6391
#[cfg(test)]

components/merino/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
pub mod curated_recommendations;
2222
pub mod suggest;
2323
pub mod worldcup;
24+
pub use curated_recommendations::{CuratedRecommendationLocale, CuratedRecommendationsApiError};
2425
uniffi::setup_scaffolding!("merino");

components/relevancy/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ pub use ranker::score;
3535

3636
use error_support::handle_error;
3737

38-
use db::BanditData;
38+
pub use db::BanditData;
3939

4040
uniffi::setup_scaffolding!();
4141

components/suggest/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@ mod yelp;
2424

2525
pub use config::{SuggestGlobalConfig, SuggestProviderConfig};
2626
pub use error::{Error, SuggestApiError};
27-
pub use geoname::{Geoname, GeonameMatch};
27+
pub use geoname::{
28+
AlternateNames, Geoname, GeonameAlternates, GeonameMatch, GeonameMatchType, GeonameType,
29+
};
2830
pub use metrics::{LabeledTimingSample, SuggestIngestionMetrics};
2931
pub use provider::{AmpMatchingStrategy, SuggestionProvider, SuggestionProviderConstraints};
3032
pub use query::{QueryWithMetricsResult, SuggestionQuery};
3133
pub use store::{InterruptKind, SuggestIngestionConstraints, SuggestStore, SuggestStoreBuilder};
32-
pub use suggestion::{raw_suggestion_url_matches, Suggestion};
34+
pub use suggestion::{raw_suggestion_url_matches, FtsMatchInfo, Suggestion, YelpSubjectType};
3335

3436
pub(crate) type Result<T> = std::result::Result<T, Error>;
3537
pub type SuggestApiResult<T> = std::result::Result<T, SuggestApiError>;

components/suggest/src/suggestion.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use chrono::Local;
77

8-
use crate::{db::DEFAULT_SUGGESTION_SCORE, geoname::Geoname};
8+
use crate::{db::DEFAULT_SUGGESTION_SCORE, geoname::Geoname, JsonValue};
99

1010
/// The template parameter for a timestamp in a "raw" sponsored suggestion URL.
1111
const TIMESTAMP_TEMPLATE: &str = "%YYYYMMDDHH%";
@@ -86,7 +86,7 @@ pub enum Suggestion {
8686
},
8787
Dynamic {
8888
suggestion_type: String,
89-
data: Option<serde_json::Value>,
89+
data: Option<JsonValue>,
9090
/// This value is optionally defined in the suggestion's remote settings
9191
/// data and is an opaque token used for dismissing the suggestion in
9292
/// lieu of a URL. If `Some`, the suggestion can be dismissed by passing

0 commit comments

Comments
 (0)