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
109
110
111
112
113
114
115
116
117
//! Tools to build providers from configuration.

use crate::{
    ClientVariantFilterProvider, DebugProvider, FixedProvider, KeywordFilterProvider, Multi,
    NullProvider, StealthProvider, TimeoutProvider, WikiFruit,
};
use anyhow::Result;
use async_recursion::async_recursion;
use cadence::StatsdClient;
use merino_adm::remote_settings::RemoteSettingsSuggester;
use merino_cache::{MemoryCacheSuggester, RedisCacheSuggester};
use merino_settings::{providers::SuggestionProviderConfig, Settings};
use merino_suggest_traits::{SetupError, SuggestionProvider};

/// Recursive helper to build a tree of providers.
#[async_recursion]
pub async fn make_provider_tree(
    settings: Settings,
    config: SuggestionProviderConfig,
    metrics_client: StatsdClient,
) -> Result<Box<dyn SuggestionProvider>, SetupError> {
    let provider: Box<dyn SuggestionProvider> = match config {
        SuggestionProviderConfig::RemoteSettings(rs_config) => {
            RemoteSettingsSuggester::new_boxed(settings, rs_config, metrics_client.clone()).await?
        }

        SuggestionProviderConfig::MemoryCache(memory_config) => {
            let inner = make_provider_tree(
                settings,
                memory_config.inner.as_ref().clone(),
                metrics_client.clone(),
            )
            .await?;
            MemoryCacheSuggester::new_boxed(memory_config, inner, metrics_client.clone())
        }

        SuggestionProviderConfig::RedisCache(redis_config) => {
            let inner = make_provider_tree(
                settings.clone(),
                redis_config.inner.as_ref().clone(),
                metrics_client.clone(),
            )
            .await?;
            RedisCacheSuggester::new_boxed(settings, redis_config, metrics_client.clone(), inner)
                .await?
        }

        SuggestionProviderConfig::Multiplexer(multi_config) => {
            let mut providers = Vec::new();
            for config in multi_config.providers {
                providers.push(
                    make_provider_tree(settings.clone(), config, metrics_client.clone()).await?,
                );
            }
            Multi::new_boxed(providers)
        }

        SuggestionProviderConfig::Timeout(timeout_config) => {
            let inner = make_provider_tree(
                settings,
                timeout_config.inner.as_ref().clone(),
                metrics_client,
            )
            .await?;
            TimeoutProvider::new_boxed(timeout_config, inner)
        }

        SuggestionProviderConfig::Fixed(fixed_config) => {
            FixedProvider::new_boxed(settings, fixed_config)?
        }

        SuggestionProviderConfig::KeywordFilter(filter_config) => {
            let inner = make_provider_tree(
                settings,
                filter_config.inner.as_ref().clone(),
                metrics_client.clone(),
            )
            .await?;
            KeywordFilterProvider::new_boxed(filter_config, inner, metrics_client.clone())?
        }

        SuggestionProviderConfig::Stealth(filter_config) => {
            let inner = make_provider_tree(
                settings,
                filter_config.inner.as_ref().clone(),
                metrics_client,
            )
            .await?;
            StealthProvider::new_boxed(inner)
        }

        SuggestionProviderConfig::ClientVariantSwitch(filter_config) => {
            let matching_provider = make_provider_tree(
                settings.clone(),
                filter_config.matching_provider.as_ref().clone(),
                metrics_client.clone(),
            )
            .await?;
            let default_provider = make_provider_tree(
                settings.clone(),
                filter_config.default_provider.as_ref().clone(),
                metrics_client.clone(),
            )
            .await?;
            ClientVariantFilterProvider::new_boxed(
                matching_provider,
                default_provider,
                filter_config.client_variant.clone(),
            )
        }

        SuggestionProviderConfig::Debug => DebugProvider::new_boxed(settings)?,
        SuggestionProviderConfig::WikiFruit => WikiFruit::new_boxed(settings)?,
        SuggestionProviderConfig::Null => Box::new(NullProvider),
    };
    Ok(provider)
}