1use crate::collection_engine::CollectionEngine;
2use crate::config::ScoutConfig;
3use crate::engine::Engine;
4
5pub struct EngineManager {
6 config: ScoutConfig,
7}
8
9impl EngineManager {
10 pub fn new(config: ScoutConfig) -> Self {
11 Self { config }
12 }
13
14 pub fn engine(&self) -> crate::Result<Box<dyn Engine>> {
15 match self.config.driver.as_str() {
16 "elasticsearch" | "opensearch" => {
17 #[cfg(feature = "elasticsearch")]
18 {
19 let host = self
20 .config
21 .get("elasticsearch.host")
22 .or_else(|| self.config.get("opensearch.host"))
23 .and_then(|value| value.as_str())
24 .unwrap_or("http://127.0.0.1:9200")
25 .to_string();
26 let api_key = self
27 .config
28 .get("elasticsearch.api_key")
29 .or_else(|| self.config.get("opensearch.api_key"))
30 .and_then(|value| value.as_str())
31 .map(str::to_string);
32 Ok(Box::new(
33 crate::elasticsearch_engine::ElasticsearchEngine::new(host, api_key),
34 ))
35 }
36 #[cfg(not(feature = "elasticsearch"))]
37 {
38 let _ = &self.config;
39 Err(crate::ScoutError::Unsupported(
40 "elasticsearch feature is not enabled".to_string(),
41 ))
42 }
43 }
44 _ => Ok(Box::new(CollectionEngine::new())),
45 }
46 }
47}
48
49impl Default for EngineManager {
50 fn default() -> Self {
51 Self::new(ScoutConfig::collection())
52 }
53}