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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
use serde::{Serialize, Serializer};
use std::fmt;

/// A set of preview features.
pub type PreviewFeatures = enumflags2::BitFlags<PreviewFeature>;

macro_rules! features {
    ($( $variant:ident $(,)? ),*) => {
        #[enumflags2::bitflags]
        #[repr(u64)]
        #[derive(Debug, Copy, Clone, PartialEq, Eq)]
        pub enum PreviewFeature {
            $( $variant,)*
        }

        impl PreviewFeature {
            pub fn parse_opt(s: &str) -> Option<Self> {
                $(
                    if s.eq_ignore_ascii_case(stringify!($variant)) { return Some(Self::$variant) }
                )*

                None
            }
        }

        impl fmt::Display for PreviewFeature {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let variant = match self { $( Self::$variant => stringify!($variant),)* };
                let mut first_char = variant.chars().next().unwrap();
                first_char.make_ascii_lowercase();
                f.write_fmt(format_args!("{first_char}{rest}", rest = &variant[1..]))
            }
        }
    };
}

// (Usually) Append-only list of features. (alphabetically sorted)
features!(
    AggregateApi,
    AtomicNumberOperations,
    ClientExtensions,
    Cockroachdb,
    ConnectOrCreate,
    CreateMany,
    DataProxy,
    Deno,
    Distinct,
    DriverAdapters,
    ExtendedIndexes,
    ExtendedWhereUnique,
    FieldReference,
    FilteredRelationCount,
    FilterJson,
    FullTextIndex,
    FullTextSearch,
    GroupBy,
    ImprovedQueryRaw,
    InteractiveTransactions,
    JsonProtocol,
    Metrics,
    MicrosoftSqlServer,
    Middlewares,
    MongoDb,
    MultiSchema,
    NamedConstraints,
    NApi,
    NativeDistinct,
    NativeTypes,
    OrderByAggregateGroup,
    OrderByNulls,
    OrderByRelation,
    PostgresqlExtensions,
    ReferentialActions,
    ReferentialIntegrity,
    SelectRelationCount,
    Tracing,
    TransactionApi,
    UncheckedScalarInputs,
    Views,
    RelationJoins
);

/// Generator preview features (alphabetically sorted)
pub const ALL_PREVIEW_FEATURES: FeatureMap = FeatureMap {
    active: enumflags2::make_bitflags!(PreviewFeature::{
        Deno
         | DriverAdapters
         | FullTextIndex
         | FullTextSearch
         | Metrics
         | MultiSchema
         | NativeDistinct
         | PostgresqlExtensions
         | Tracing
         | Views
         | RelationJoins
    }),
    deprecated: enumflags2::make_bitflags!(PreviewFeature::{
        AtomicNumberOperations
        | AggregateApi
        | ClientExtensions
        | Cockroachdb
        | ConnectOrCreate
        | CreateMany
        | DataProxy
        | Distinct
        | ExtendedIndexes
        | ExtendedWhereUnique
        | FieldReference
        | FilteredRelationCount
        | FilterJson
        | GroupBy
        | ImprovedQueryRaw
        | InteractiveTransactions
        | JsonProtocol
        | MicrosoftSqlServer
        | Middlewares
        | MongoDb
        | NamedConstraints
        | NApi
        | NativeTypes
        | OrderByAggregateGroup
        | OrderByNulls
        | OrderByRelation
        | ReferentialActions
        | ReferentialIntegrity
        | SelectRelationCount
        | TransactionApi
        | UncheckedScalarInputs
    }),
    hidden: enumflags2::BitFlags::EMPTY,
};

#[derive(Debug)]
pub struct FeatureMap {
    /// Valid, visible features.
    active: PreviewFeatures,

    /// Deprecated features.
    deprecated: PreviewFeatures,

    /// Hidden preview features are valid features, but are not propagated into the tooling
    /// (as autocomplete or similar) or into error messages (eg. showing a list of valid features).
    hidden: PreviewFeatures,
}

impl FeatureMap {
    pub const fn active_features(&self) -> PreviewFeatures {
        self.active
    }

    pub const fn hidden_features(&self) -> PreviewFeatures {
        self.hidden
    }

    pub(crate) fn is_valid(&self, flag: PreviewFeature) -> bool {
        (self.active | self.hidden).contains(flag)
    }

    pub(crate) fn is_deprecated(&self, flag: PreviewFeature) -> bool {
        self.deprecated.contains(flag)
    }
}

impl Serialize for PreviewFeature {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.to_string())
    }
}