|
5 | 5 |
|
6 | 6 | use serde::{Deserialize, Serialize}; |
7 | 7 |
|
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, |
44 | 42 | } |
| 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 | + } |
45 | 65 |
|
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 | + } |
61 | 89 | } |
62 | 90 |
|
63 | 91 | #[cfg(test)] |
|
0 commit comments