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
use crate::configuration::{self, StringFromEnvVar};

#[derive(Debug, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceConfig {
    pub name: String,
    pub provider: String,
    pub active_provider: String,
    pub url: StringFromEnvVar,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub direct_url: Option<StringFromEnvVar>,
    pub schemas: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub documentation: Option<String>,
}

pub fn render_sources_to_json_value(sources: &[configuration::Datasource]) -> serde_json::Value {
    let res = sources_to_json_structs(sources);
    serde_json::to_value(res).expect("Failed to render JSON.")
}

pub fn render_sources_to_json(sources: &[configuration::Datasource]) -> String {
    let res = sources_to_json_structs(sources);
    serde_json::to_string_pretty(&res).expect("Failed to render JSON.")
}

fn sources_to_json_structs(sources: &[configuration::Datasource]) -> Vec<SourceConfig> {
    let mut res: Vec<SourceConfig> = Vec::new();

    for source in sources {
        res.push(source_to_json_struct(source));
    }

    res
}

fn source_to_json_struct(source: &configuration::Datasource) -> SourceConfig {
    let schemas: Vec<String> = source
        .namespaces
        .iter()
        .map(|(namespace, _)| namespace.clone())
        .collect();

    SourceConfig {
        name: source.name.clone(),
        provider: source.provider.clone(),
        active_provider: source.active_provider.to_string(),
        url: source.url.clone(),
        direct_url: source.direct_url.clone(),
        documentation: source.documentation.clone(),
        schemas,
    }
}