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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
use syn::Error;

fn path_to_single_string(path: &syn::Path) -> Option<String> {
    path.segments
        .iter()
        .next()
        .map(|segment| segment.ident.to_string())
}

#[derive(Debug)]
struct Group {
    variant: GroupVariant,
    n_init_impls: usize,
}

impl Group {
    fn new(variant: GroupVariant) -> Self {
        let n_init_impls = variant.n_init_impls();

        Self {
            variant,
            n_init_impls,
        }
    }
}

#[derive(Debug)]
enum GroupVariant {
    Any(Vec<Group>),
    All(Vec<Group>),
    Not(Box<Group>),
    Field(String),
}

impl GroupVariant {
    fn n_init_impls(&self) -> usize {
        match self {
            // Sum!
            GroupVariant::Any(args) => args
                .iter()
                .fold(0, |acc, arg| acc + arg.variant.n_init_impls()),
            // Product!
            GroupVariant::All(args) => args
                .iter()
                .fold(1, |acc, arg| acc * arg.variant.n_init_impls()),
            GroupVariant::Not(arg) => arg.n_init_impls,
            GroupVariant::Field(_) => 1,
        }
    }
}

impl TryFrom<&syn::Expr> for Group {
    type Error = Error;

    fn try_from(expr: &syn::Expr) -> Result<Self, Self::Error> {
        match expr {
            syn::Expr::Call(syn::ExprCall { func, args, .. }) => match func.as_ref() {
                syn::Expr::Path(path) => {
                    let Some(path) = path_to_single_string(&path.path)
                    else {
                        return Err(Error::new_spanned(func, "Expected `any`, `all` or `not`"))
                    };

                    match path.as_str() {
                        "any" => Ok(Self::new(GroupVariant::Any(
                            args.into_iter()
                                .map(Self::try_from)
                                .collect::<Result<Vec<_>, Error>>()?,
                        ))),
                        "all" => Ok(Self::new(GroupVariant::All(
                            args.into_iter()
                                .map(Self::try_from)
                                .collect::<Result<Vec<_>, Error>>()?,
                        ))),
                        "not" => {
                            let mut args_iter = args.iter();

                            let Some(arg) = args_iter.next()
                            else {
                                return Err(Error::new_spanned(args, "Expected exactly one argument for `not`"));
                            };

                            if args_iter.next().is_some() {
                                return Err(Error::new_spanned(
                                    args,
                                    "Expected only one argument for `not`",
                                ));
                            }

                            Ok(Self::new(GroupVariant::Not(Box::new(Self::try_from(arg)?))))
                        }
                        _ => Err(Error::new_spanned(func, "Expected `any`, `all` or `not`")),
                    }
                }
                _ => Err(Error::new_spanned(func, "Expected `any`, `all` or `not`")),
            },
            syn::Expr::Path(path) => {
                let Some(field_name) = path_to_single_string(&path.path)
                else {
                    return Err(Error::new_spanned(path, "Expected a field name"));
                };

                Ok(Self::new(GroupVariant::Field(field_name)))
            }
            _ => Err(Error::new_spanned(
                expr,
                "Expected `any(…)`, `all(…)`, `not(…)` or a field",
            )),
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Dependency {
    Generic,
    Set,
    Unset,
}

impl std::fmt::Display for Dependency {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Dependency::Generic => write!(f, "_"),
            Dependency::Set => write!(f, "S"),
            Dependency::Unset => write!(f, "U"),
        }
    }
}

impl Group {
    // Returns a bool about whether the group contains a `not`.
    fn set_init_impls<T>(
        &self,
        impls: &mut [Vec<Dependency>],
        field_names: &[T],
        invert: bool,
    ) -> bool
    where
        T: PartialEq<String>,
    {
        let mut contains_not = false;

        match &self.variant {
            GroupVariant::Any(args) => {
                let mut ind = 0;

                let mult = impls.len() / args.iter().fold(0, |acc, arg| acc + arg.n_init_impls);

                for arg in args {
                    let new_ind = ind + mult * arg.n_init_impls;
                    let new_contains_not =
                        arg.set_init_impls(&mut impls[ind..new_ind], field_names, invert);
                    ind = new_ind;

                    contains_not = contains_not || new_contains_not;
                }
            }
            GroupVariant::All(args) => {
                let mut remaining_n_init_impls = impls.len();

                for arg in args {
                    for split_ind in 0..impls.len() / remaining_n_init_impls {
                        let init = split_ind * remaining_n_init_impls;
                        let new_contains_not = arg.set_init_impls(
                            &mut impls[init..init + remaining_n_init_impls],
                            field_names,
                            invert,
                        );

                        contains_not = contains_not || new_contains_not;
                    }

                    remaining_n_init_impls /= arg.n_init_impls;
                }
            }
            GroupVariant::Not(arg) => {
                arg.set_init_impls(impls, field_names, !invert);

                contains_not = true;
            }
            GroupVariant::Field(field_name) => {
                let Some(ind) = field_names.iter().position(|x| x == field_name)
                else {
                    panic!("{field_name} not found as struct field!");
                };

                for implementation in impls {
                    implementation[ind] = if invert {
                        Dependency::Unset
                    } else {
                        Dependency::Set
                    };
                }
            }
        };

        contains_not
    }

    fn no_conflict(first_impl: &[Dependency], second_impl: &[Dependency]) -> bool {
        let mut generic_non_generic_found = false;
        let mut non_generic_generic_found = false;
        let mut non_generic_diff = false;

        for (first_dep, second_dep) in second_impl.iter().zip(first_impl.iter()) {
            match (first_dep, second_dep) {
                (Dependency::Generic, Dependency::Set)
                | (Dependency::Generic, Dependency::Unset) => {
                    generic_non_generic_found = true;

                    if non_generic_generic_found {
                        break;
                    }
                }
                (Dependency::Set, Dependency::Generic)
                | (Dependency::Unset, Dependency::Generic) => {
                    non_generic_generic_found = true;

                    if generic_non_generic_found {
                        break;
                    }
                }
                (Dependency::Set, Dependency::Unset) | (Dependency::Unset, Dependency::Set) => {
                    non_generic_diff = true;
                    break;
                }
                _ => (),
            }
        }

        non_generic_diff || (generic_non_generic_found && non_generic_generic_found)
    }

    fn check_input_conflicts(impls: &[Vec<Dependency>]) -> Result<(), &'static str> {
        for (upper_impl_ind, upper_impl) in impls.iter().enumerate().take(impls.len() - 1) {
            let no_conflict = impls[upper_impl_ind + 1..]
                .iter()
                .all(|lower_impl| Self::no_conflict(lower_impl, upper_impl));

            if !no_conflict {
                return Err("A logical conflict was detected in the input. Something like `any(a, all(a, b))` which must be only `a`");
            }
        }

        Ok(())
    }

    fn fix_conflicts(
        init_impls: &[Vec<Dependency>],
        impls: &mut [Vec<Dependency>],
        additional_impls: &mut Vec<Vec<Dependency>>,
        new_additional_impls: &mut Vec<Vec<Dependency>>,
        focus_dep: Dependency,
        inverse_focus_dep: Dependency,
    ) {
        for (impl_ind, init_impl) in init_impls.iter().enumerate() {
            let focus_deps = init_impl
                .iter()
                .copied()
                .enumerate()
                .filter_map(|(ind, dep)| if dep == focus_dep { Some(ind) } else { None })
                .collect::<Vec<_>>();

            // Could happen if there is a `not`.
            if focus_deps.is_empty() {
                continue;
            }

            for lower_impl in impls
                .iter_mut()
                .skip(impl_ind + 1)
                .chain(additional_impls.iter_mut())
            {
                let Some(shadowed_generics) = focus_deps
                    .iter()
                    .copied()
                    .filter_map(|ind| {
                        let dep = lower_impl[ind];

                        if dep == focus_dep {
                            // Ignore only this dep.
                            None
                        } else if dep == inverse_focus_dep {
                            // Ignore the whole lower implementation and go to the next one.
                            Some(None)
                        } else {
                            Some(Some(ind))
                        }
                    })
                    .collect::<Option<Vec<_>>>()
                else {
                    // dep == inverse_focus_dep.
                    continue;
                };

                if let Some(first_shadowed_generic) = shadowed_generics.first() {
                    // Set the first inverse by mutating the initial implementation.
                    // U _ _
                    lower_impl[*first_shadowed_generic] = inverse_focus_dep;

                    // Set the possible rest of the diagonal shadow with additional implementations.
                    // S U _
                    // S S U
                    // etc.
                    for (shadowed_generic_ind, shadowed_generic) in
                        shadowed_generics.iter().skip(1).copied().enumerate()
                    {
                        let mut additional_impl = lower_impl.clone();

                        // Set the focus deps.
                        for left_shadowed_generic in shadowed_generics
                            .iter()
                            .take(shadowed_generic_ind + 1)
                            .copied()
                        {
                            additional_impl[left_shadowed_generic] = focus_dep;
                        }
                        // Set the inverse.
                        additional_impl[shadowed_generic] = inverse_focus_dep;

                        new_additional_impls.push(additional_impl);
                    }
                }
            }

            additional_impls.extend_from_slice(new_additional_impls);
            new_additional_impls.clear();
        }
    }

    fn impls<T>(self, field_names: &[T]) -> Result<Vec<Vec<Dependency>>, &'static str>
    where
        T: PartialEq<String>,
    {
        let n_fields = field_names.len();

        let mut impls = vec![vec![Dependency::Generic; n_fields]; self.n_init_impls];

        let contains_not = self.set_init_impls(&mut impls, field_names, false);

        Self::check_input_conflicts(&impls)?;

        // Sort implementations on the number of non-generic dependencies.
        impls.sort_by_cached_key(|implementation| {
            implementation.iter().fold(0, |acc, dep| match dep {
                Dependency::Set | Dependency::Unset => acc + 1,
                Dependency::Generic => acc,
            })
        });

        let mut init_impls = impls.clone();
        let mut additional_impls = Vec::new();
        let mut new_additional_impls = Vec::new();

        // Fix conflicts related to fields with `S` in the initial implementations.
        Self::fix_conflicts(
            &init_impls,
            &mut impls,
            &mut additional_impls,
            &mut new_additional_impls,
            Dependency::Set,
            Dependency::Unset,
        );

        if contains_not {
            // Fix conflicts related to `not` fields with `U` in the initial implementations.
            Self::fix_conflicts(
                &init_impls,
                &mut impls,
                &mut additional_impls,
                &mut new_additional_impls,
                Dependency::Unset,
                Dependency::Set,
            );

            // Remove possible less generic conflicting implementations from the mutated initial implementations.

            // Recycling allocated vector.
            init_impls.clear();
            let mut cleaned_up_init_impls = init_impls;

            // Sort implementations to have the one with more generics higher.
            impls.sort_by_cached_key(|implementation| {
                implementation
                    .iter()
                    .filter(|dep| matches!(dep, Dependency::Set | Dependency::Unset))
                    .count()
            });

            // Iterate starting from the bottom where the less generic implementations are.
            for (impl_ind, lower_impl) in impls.iter().rev().enumerate() {
                let no_conflict = impls
                    .iter()
                    .rev()
                    .skip(impl_ind + 1)
                    .all(|upper_impl| Self::no_conflict(upper_impl, lower_impl));

                // If there is a conflict, the less generic implementation is removed
                // (not added to the cleaned up ones).
                if no_conflict {
                    cleaned_up_init_impls.push(lower_impl.clone());
                }
            }

            impls = cleaned_up_init_impls;
        }

        // Recycling allocated vector.
        new_additional_impls.clear();
        let mut cleaned_up_additional_impls = new_additional_impls;

        // Remove conflicting additional implementations.
        for (additional_impl_ind, additional_impl) in additional_impls.iter().enumerate() {
            let no_conflict = impls
                .iter()
                .chain(additional_impls[..additional_impl_ind].iter())
                .all(|implementation| Self::no_conflict(implementation, additional_impl));

            if no_conflict {
                cleaned_up_additional_impls.push(additional_impl.clone());
            }
        }

        impls.extend_from_slice(&cleaned_up_additional_impls);

        Ok(impls)
    }
}

pub fn impls<T>(expr: &syn::Expr, field_names: &[T]) -> Result<Vec<Vec<Dependency>>, Error>
where
    T: PartialEq<String>,
{
    let group = Group::try_from(expr)?;

    group
        .impls(field_names)
        .map_err(|e| Error::new_spanned(expr, e))
}