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
use psl::datamodel_connector::NativeTypeConstructor;

pub(crate) fn run(schema: &str) -> String {
    let validated_configuration = match psl::parse_configuration(schema) {
        Ok(validated_configuration) => validated_configuration,
        Err(_) => return "[]".to_owned(),
    };

    if validated_configuration.datasources.len() != 1 {
        return "[]".to_owned();
    }

    let datasource = &validated_configuration.datasources[0];
    let available_native_type_constructors = datasource.active_connector.available_native_type_constructors();
    let available_native_type_constructors: Vec<SerializableNativeTypeConstructor> =
        available_native_type_constructors.iter().map(From::from).collect();

    serde_json::to_string(&available_native_type_constructors).expect("Failed to render JSON")
}

#[derive(serde::Serialize)]
struct SerializableNativeTypeConstructor {
    pub name: &'static str,
    pub _number_of_args: usize,
    pub _number_of_optional_args: usize,
    pub prisma_types: Vec<&'static str>,
}

impl From<&NativeTypeConstructor> for SerializableNativeTypeConstructor {
    fn from(nt: &NativeTypeConstructor) -> Self {
        let NativeTypeConstructor {
            name,
            number_of_args,
            number_of_optional_args,
            prisma_types,
        } = nt;
        SerializableNativeTypeConstructor {
            name,
            _number_of_args: *number_of_args,
            _number_of_optional_args: *number_of_optional_args,
            prisma_types: prisma_types.iter().map(|st| st.as_str()).collect(),
        }
    }
}