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::introspection::{introspection_pair::EnumPair, sanitize_datamodel_names};
use schema_connector::{warnings as generators, Warnings};

/// Analyze and generate warnigs from an enum.
pub(super) fn generate_warnings(r#enum: EnumPair<'_>, warnings: &mut Warnings) {
    if r#enum.name_from_psl() {
        warnings.remapped_enums.push(generators::Enum {
            r#enum: r#enum.name().to_string(),
        });
    }

    if r#enum.uses_duplicate_name() {
        warnings.duplicate_names.push(generators::TopLevelItem {
            r#type: generators::TopLevelType::Enum,
            name: r#enum.name().to_string(),
        });
    }

    if r#enum.description().is_some() {
        warnings.objects_with_comments.push(generators::Object {
            r#type: "enum",
            name: r#enum.name().to_string(),
        });
    }

    for variant in r#enum.variants() {
        if variant.name().is_empty() {
            let value = variant
                .mapped_name()
                .map(String::from)
                .unwrap_or_else(|| variant.name().to_string());

            warnings.enum_values_with_empty_names.push(generators::EnumAndValue {
                r#enum: r#enum.name().to_string(),
                value,
            });
        }

        if variant.name().is_empty() || sanitize_datamodel_names::needs_sanitation(&variant.name()) {
            let warning = generators::EnumAndValue {
                r#enum: r#enum.name().to_string(),
                value: variant.name().to_string(),
            };

            warnings.enum_values_with_empty_names.push(warning);
        } else if variant.name_from_psl() {
            warnings.remapped_values.push(generators::EnumAndValue {
                value: variant.name().to_string(),
                r#enum: r#enum.name().to_string(),
            });
        }
    }
}