1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
//! A suggestion provider that provides toy responses.
//!
//! It is useful in that it is fully self contained and very simple. It is meant
//! to be used in development and testing.

use std::marker::PhantomData;

use anyhow::anyhow;
use async_trait::async_trait;
use http::Uri;
use merino_settings::Settings;

use merino_settings::SuggestionProviderConfig;
use merino_suggest_traits::{
    convert_config, CacheInputs, MakeFreshType, Proportion, SetupError, SuggestError, Suggestion,
    SuggestionProvider, SuggestionRequest, SuggestionResponse,
};

/// A toy suggester to test the system.
pub struct WikiFruit {
    /// A zero-sized private field to ensure that the type cannot be directly created.
    _phantom: PhantomData<()>,
}

impl WikiFruit {
    /// Create a WikiFruit provider from settings.
    pub fn new_boxed(settings: Settings) -> Result<Box<Self>, SetupError> {
        if !settings.debug {
            Err(SetupError::InvalidConfiguration(anyhow!(
                "WikiFruit suggestion provider can only be used in debug mode",
            )))
        } else {
            Ok(Box::new(Self {
                _phantom: PhantomData,
            }))
        }
    }
}

#[async_trait]
impl SuggestionProvider for WikiFruit {
    fn name(&self) -> String {
        "WikiFruit".to_string()
    }

    fn cache_inputs(&self, req: &SuggestionRequest, cache_inputs: &mut dyn CacheInputs) {
        cache_inputs.add(req.query.as_bytes());
    }

    async fn suggest(
        &self,
        request: SuggestionRequest,
    ) -> Result<SuggestionResponse, SuggestError> {
        let suggestion = match request.query.as_ref() {
            "apple" => Some(Suggestion {
                id: 1,
                full_keyword: "apple".to_string(),
                title: "Wikipedia - Apple".to_string(),
                url: Uri::from_static("https://en.wikipedia.org/wiki/Apple"),
                impression_url: Some(Uri::from_static("https://127.0.0.1/")),
                click_url: Some(Uri::from_static("https://127.0.0.1/")),
                provider: "Merino::WikiFruit".to_string(),
                advertiser: "test_advertiser".to_string(),
                is_sponsored: false,
                icon: Uri::from_static("https://en.wikipedia.org/favicon.ico"),
                score: Proportion::zero(),
            }),
            "banana" => Some(Suggestion {
                id: 1,
                full_keyword: "banana".to_string(),
                title: "Wikipedia - Banana".to_string(),
                url: Uri::from_static("https://en.wikipedia.org/wiki/Banana"),
                impression_url: Some(Uri::from_static("https://127.0.0.1/")),
                click_url: Some(Uri::from_static("https://127.0.0.1/")),
                provider: "Merino::WikiFruit".to_string(),
                advertiser: "test_advertiser".to_string(),
                is_sponsored: false,
                icon: Uri::from_static("https://en.wikipedia.org/favicon.ico"),
                score: Proportion::zero(),
            }),
            "cherry" => Some(Suggestion {
                id: 1,
                full_keyword: "cherry".to_string(),
                title: "Wikipedia - Cherry".to_string(),
                url: Uri::from_static("https://en.wikipedia.org/wiki/Cherry"),
                impression_url: Some(Uri::from_static("https://127.0.0.1/")),
                click_url: Some(Uri::from_static("https://127.0.0.1/")),
                provider: "Merino::WikiFruit".to_string(),
                advertiser: "test_advertiser".to_string(),
                is_sponsored: false,
                icon: Uri::from_static("https://en.wikipedia.org/favicon.ico"),
                score: Proportion::zero(),
            }),
            _ => None,
        };

        Ok(SuggestionResponse::new(suggestion.into_iter().collect()))
    }

    async fn reconfigure(
        &mut self,
        new_config: serde_json::Value,
        _make_fresh: &MakeFreshType,
    ) -> Result<(), SetupError> {
        // make sure this is a wiki fruit config
        convert_config::<SuggestionProviderConfig>(new_config).map(|_| ())
    }
}