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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
//! Create sort orders for Rust objects from query URLs.
//!
//! Django permits sorting by multiple fields simultaneously, and each
//! field can be in ascending or descending order. There is only one
//! natural ordering permitted however per field, although it is
//! possible to sort by foreign keys (i.e. when applied in Rust, it's
//! possible to sort a collection of objects by a common member whose
//! type is also structured).
//!
//! The main trait in this module is [Sortable] which has an
//! associated derive macro. Deriving [Sortable] means it's possible
//! to construct an [OrderingSet] for this type, which contains a
//! lookup for boxed [Sorter]s. A sorter can be used as a simple
//! comparison, paying the virtual function overhead for each
//! comparison made, or more efficiently it can be applied to an
//! entire [Vec] at a time to sort it.
//!
//! Example:
//!
//! ```rust
//! use django_query::{Sortable, OrderingSet};
//!
//! #[derive(Sortable)]
//! struct Bar {
//!   #[django(sort)]
//!   a: i32,
//!   #[django(sort)]
//!   b: i32,
//! }
//!
//! #[derive(Sortable)]
//! struct Foo {
//!   #[django(sort)]
//!   c: i32,
//!   #[django(sort("b"))]
//!   d: Bar,
//! }
//!
//! let mut foos = vec![
//!     Foo { c: 0, d: Bar { a: 1, b: 1 } },
//!     Foo { c: 1, d: Bar { a: 0, b: 0 } },
//!     Foo { c: 2, d: Bar { a: 2, b: 1 } },
//!     Foo { c: 3, d: Bar { a: 3, b: 0 } },
//! ];
//!
//! let qr = OrderingSet::<Foo>::new();
//!
//! let sort = qr.create_sort("-c").unwrap();
//! sort.sort_vec(&mut foos);
//! assert_eq!(foos[0].c, 3);
//! assert_eq!(foos[1].c, 2);
//! assert_eq!(foos[2].c, 1);
//! assert_eq!(foos[3].c, 0);
//!
//! let sort = qr.create_sort("d,c").unwrap();
//! sort.sort_vec(&mut foos);
//! assert_eq!(foos[0].c, 1);
//! assert_eq!(foos[1].c, 3);
//! assert_eq!(foos[2].c, 0);
//! assert_eq!(foos[3].c, 2);
//! ```

use core::cmp::Ordering;
use core::ops::Deref;

use std::collections::BTreeMap;
use std::sync::Arc;

use thiserror::Error;

#[derive(Debug, Error)]
pub enum SorterError {
    #[error("cannot sort by expression '{0}'")]
    NoSort(String),
}

/// A [Sorter] defined for all types that are [Ord].
pub struct Compare;

impl<T: Ord> Sorter<T> for Compare {
    fn compare(&self, a: &T, b: &T) -> Ordering {
        a.cmp(b)
    }
}

/// A [Sorter] defined for all types that are [Ord], which reverses
/// the natural order.
pub struct ReverseCompare;

impl<T: Ord> Sorter<T> for ReverseCompare {
    fn compare(&self, a: &T, b: &T) -> Ordering {
        b.cmp(a)
    }
}

/// The [SorterClass] for [Compare]
#[derive(Clone)]
pub struct CompareClass;

impl<T: Ord> SorterClass<T> for CompareClass {
    fn instantiate(&self, reverse: bool) -> Box<dyn Sorter<T>> {
        if reverse {
            Box::new(ReverseCompare)
        } else {
            Box::new(Compare)
        }
    }
}

impl<T: Ord> SorterTypedClass<T> for CompareClass {
    type Sorter = Compare;
    fn instantiate(&self) -> Compare {
        Compare
    }
}

/// Something that can apply a [Sorter] to a member of a type.
///
/// By asking the type to apply the operator, we can avoid cloning
/// potentially large objects in more complex cases of nesting.  If we
/// used [Accessor] directly for everything, which is a much simpler
/// definition, there are some constructs that we cannot handle
/// efficiently, because of nested comparisons within members of type
/// [Option], for example (i.e. in the case that a `Foo` is to be
/// sorted by the `a` member of a contained object of type
/// `Option<Bar>`.
pub trait Field<R>: Clone {
    type Value;
    /// Extract whichever member this [Field] obtains from `R`, from
    /// both `a` and `b` and apply `op` to the results.
    fn apply_sorter<V: Sorter<Self::Value>>(&self, op: &V, a: &R, b: &R) -> Ordering;
}

/// Something which returns a particular member of an instance by reference.
///
/// When `Accessor` is combined with the marker trait `ReferenceField`, you will automatically
/// get a derivation of `Field`.
///
/// Example:
/// ```rust
/// use django_query::ordering::{Accessor, Field, Compare, ReferenceField};
/// use core::cmp::Ordering;
///
/// struct Foo {
///   a: i32
/// }
///
/// #[derive(Clone)]
/// struct FooA;
///
/// impl Accessor<Foo> for FooA {
///     type Value = i32;
///     fn value<'a>(&self, data: &'a Foo) -> &'a i32 {
///        &data.a
///     }
/// }
///
/// impl ReferenceField for FooA {}
///
/// let f_a = FooA;
/// let foo1 = Foo { a: 20 };
/// let foo2 = Foo { a: 10 };
/// assert_eq!(f_a.value(&foo1), &20i32);
/// assert_eq!(f_a.apply_sorter(&Compare, &foo1, &foo2), Ordering::Greater);
/// ```
pub trait Accessor<R>: Clone {
    type Value;
    /// Return a reference to a member of `data`
    fn value<'a>(&self, data: &'a R) -> &'a Self::Value;
}

/// A marker for an [Accessor] that can act as a field.
///
/// Not all types of [Field] can be defined as a simple pass through,
/// based on [Accessor].  Some fields need to create intermediate
/// values, for example fields nested inside [Option] typed members of
/// a containing structure. This marker indicates that the
/// implementation of [Accessor] can be used for this type to
/// automatically derive [Field].
pub trait ReferenceField {}

impl<R, F> Field<R> for F
where
    F: Accessor<R> + ReferenceField,
{
    type Value = <F as Accessor<R>>::Value;
    fn apply_sorter<V: Sorter<Self::Value>>(&self, op: &V, a: &R, b: &R) -> Ordering {
        op.compare(self.value(a), self.value(b))
    }
}

/// Something that can compare two values of another type `T`.
///
/// While [Ord] is a sensible trait for types that can have only one
/// ordering, it needs to be generalised when a type can have multiple
/// orderings according to some parameterisation. `Sorter` is just
/// that generalisation.
pub trait Sorter<R> {
    fn compare(&self, a: &R, b: &R) -> Ordering;

    /// Sort a [Vec] in place.
    fn sort_vec(&self, vec: &mut Vec<R>) {
        vec.sort_by(|x, y| self.compare(x, y))
    }

    /// Sort a [Vec] of references in place.
    fn sort_ref_vec(&self, vec: &mut Vec<&R>) {
        vec.sort_by(|x, y| self.compare(x, y))
    }
}

struct SorterImpl<F, S> {
    field: F,
    sorter: S,
}

impl<F, S> SorterImpl<F, S> {
    pub fn new(field: F, sorter: S) -> Self {
        Self { field, sorter }
    }
}

impl<R, F, T, S> Sorter<R> for SorterImpl<F, S>
where
    F: Field<R, Value = T>,
    S: Sorter<T>,
{
    fn compare(&self, a: &R, b: &R) -> Ordering {
        self.field.apply_sorter(&self.sorter, a, b)
    }
}

struct Reverser<S> {
    inner: S,
}

impl<S> Reverser<S> {
    pub fn new(inner: S) -> Self {
        Self { inner }
    }
}

impl<R, S> Sorter<R> for Reverser<S>
where
    S: Sorter<R>,
{
    fn compare(&self, a: &R, b: &R) -> Ordering {
        self.inner.compare(a, b).reverse()
    }
}

/// Something that can make a boxed [Sorter].
///
/// This is the boxed object-like trait that is stored in an
/// [OrderingSet]. When a specific sort is needed, it can be
/// used to create one.
pub trait SorterClass<R> {
    /// Create a [Sorter], optionally reversing the order.
    fn instantiate(&self, reverse: bool) -> Box<dyn Sorter<R>>;
}

/// Something which can make a typed [Sorter].
///
/// This trait is used for any underlying [Sorter] that wants to be
/// used directly on a field, for example as a replacement for
/// [Compare]. It does not incur virtual function overhead or storage
/// overhead, but it also cannot be stored in collections for a given
/// type.
pub trait SorterTypedClass<R>: Clone {
    type Sorter: Sorter<R>;
    fn instantiate(&self) -> Self::Sorter;
}

#[derive(Clone)]
struct SorterClassImpl<F, S> {
    field: F,
    sorter: S,
}

impl<F, S> SorterClassImpl<F, S> {
    pub fn new(field: F, sorter: S) -> Self {
        Self { field, sorter }
    }
}

impl<R, F, T, S> SorterClass<R> for SorterClassImpl<F, S>
where
    F: Field<R, Value = T> + 'static,
    S: SorterTypedClass<T>,
    <S as SorterTypedClass<T>>::Sorter: 'static,
{
    fn instantiate(&self, reverse: bool) -> Box<dyn Sorter<R>> {
        if reverse {
            Box::new(Reverser::new(SorterImpl::new(
                self.field.clone(),
                self.sorter.instantiate(),
            )))
        } else {
            Box::new(SorterImpl::new(
                self.field.clone(),
                self.sorter.instantiate(),
            ))
        }
    }
}

impl<R, F, T, S> SorterTypedClass<R> for SorterClassImpl<F, S>
where
    F: Field<R, Value = T> + 'static,
    S: SorterTypedClass<T>,
{
    type Sorter = SorterImpl<F, <S as SorterTypedClass<T>>::Sorter>;
    fn instantiate(&self) -> Self::Sorter {
        SorterImpl::new(self.field.clone(), self.sorter.instantiate())
    }
}

/// Something that can receive callbacks about the sort orders a type
/// provides.
///
/// Each [Sortable] instance will accept a [SortVisitor] and describe
/// to it the list of sorts it supports, using the provided methods.
pub trait SortVisitor {
    type Target;
    /// Receive a basic sort on a given raw [Field], named `name`.
    /// The comparison operator itself is given as `sort`.
    fn visit_sort<F, T, S>(&mut self, name: &str, field: &F, sort: &S)
    where
        F: Field<Self::Target, Value = T> + 'static,
        S: SorterTypedClass<T> + 'static,
        <S as SorterTypedClass<T>>::Sorter: 'static;

    /// Receive a key sort on a member field which is itself [Sortable].
    ///
    /// Here, `name` is the name of the sort as usual, `field` is the
    /// accessor for the member, and `sort_key` is the name of the
    /// sort in the field's own type which should be used.
    fn visit_key_sort<F, T>(&mut self, name: &str, field: &F, sort_key: &str)
    where
        F: Field<Self::Target, Value = T> + 'static,
        T: Sortable + 'static;
}

/// Something that can be sorted.
///
/// This is the central trait of this module. It has a derive macro
/// which will automatically create all necessary supporting types
/// using the markup on the type.
pub trait Sortable {
    /// `visitor` will receive a callback for each sort that is
    /// defined for this type.
    fn accept_visitor<V: SortVisitor<Target = Self>>(visitor: &mut V)
    where
        Self: Sized;
}

impl<T> Sortable for Arc<T>
where
    T: Sortable,
{
    fn accept_visitor<V: SortVisitor<Target = Self>>(visitor: &mut V) {
        let mut v = VisitorWrapper { parent: visitor };
        T::accept_visitor(&mut v);
    }
}

struct VisitorWrapper<'a, P> {
    parent: &'a mut P,
}

impl<'a, P, R, U> SortVisitor for VisitorWrapper<'a, P>
where
    P: SortVisitor<Target = U>,
    U: Deref<Target = R>,
{
    type Target = R;
    fn visit_sort<F, T, S>(&mut self, name: &str, field: &F, sort: &S)
    where
        F: Field<R, Value = T> + 'static,
        S: SorterTypedClass<T> + 'static,
        <S as SorterTypedClass<T>>::Sorter: 'static,
    {
        self.parent.visit_sort(
            name,
            &WrappedField {
                inner: field.clone(),
            },
            sort,
        );
    }

    fn visit_key_sort<F, T>(&mut self, name: &str, field: &F, sort_key: &str)
    where
        F: Field<R, Value = T> + 'static,
        T: Sortable + 'static,
    {
        self.parent.visit_key_sort(
            name,
            &WrappedField {
                inner: field.clone(),
            },
            sort_key,
        );
    }
}

#[derive(Clone)]
struct WrappedField<F> {
    inner: F,
}

impl<R, F, T, U> Field<U> for WrappedField<F>
where
    F: Field<R, Value = T>,
    U: Deref<Target = R>,
{
    type Value = T;
    fn apply_sorter<V: Sorter<Self::Value>>(&self, op: &V, a: &U, b: &U) -> Ordering {
        self.inner.apply_sorter(op, a, b)
    }
}

impl<T> Sortable for Option<T>
where
    T: Sortable,
{
    fn accept_visitor<V: SortVisitor<Target = Self>>(visitor: &mut V)
    where
        Self: Sized,
    {
        let mut v = OptionVisitor { parent: visitor };
        T::accept_visitor(&mut v);
    }
}

struct OptionVisitor<'a, P> {
    parent: &'a mut P,
}

impl<'a, P, R> SortVisitor for OptionVisitor<'a, P>
where
    P: SortVisitor<Target = Option<R>>,
{
    type Target = R;
    fn visit_sort<F, T, S>(&mut self, name: &str, field: &F, sort: &S)
    where
        F: Field<R, Value = T> + 'static,
        S: SorterTypedClass<T> + 'static,
        <S as SorterTypedClass<T>>::Sorter: 'static,
    {
        self.parent.visit_sort(
            name,
            &OptionField {
                inner: field.clone(),
            },
            sort,
        );
    }

    fn visit_key_sort<F, T>(&mut self, name: &str, field: &F, sort_key: &str)
    where
        F: Field<R, Value = T> + 'static,
        T: Sortable + 'static,
    {
        self.parent.visit_key_sort(
            name,
            &OptionField {
                inner: field.clone(),
            },
            sort_key,
        );
    }
}

#[derive(Clone)]
struct OptionField<F> {
    inner: F,
}

impl<R, F, T> Field<Option<R>> for OptionField<F>
where
    F: Field<R, Value = T>,
{
    type Value = T;
    fn apply_sorter<V: Sorter<Self::Value>>(
        &self,
        op: &V,
        a: &Option<R>,
        b: &Option<R>,
    ) -> Ordering {
        match (a.as_ref(), b.as_ref()) {
            (Some(a), Some(b)) => self.inner.apply_sorter(&OptionOp { parent: op }, a, b),
            (Some(_), None) => Ordering::Greater,
            (None, Some(_)) => Ordering::Less,
            (None, None) => Ordering::Equal,
        }
    }
}

struct OptionOp<'a, V> {
    parent: &'a V,
}

impl<'a, V, T> Sorter<T> for OptionOp<'a, V>
where
    V: Sorter<T>,
{
    fn compare(&self, a: &T, b: &T) -> Ordering {
        self.parent.compare(a, b)
    }
}

/// A collection of sort orders, built from a [Sortable] type.
pub struct OrderingSet<R> {
    sorts: BTreeMap<String, Box<dyn SorterClass<R>>>,
}

impl<R: Sortable + 'static> Default for OrderingSet<R> {
    fn default() -> Self {
        Self::new()
    }
}

impl<R: Sortable + 'static> OrderingSet<R> {
    /// Create a new [OrderingSet] for a [Sortable] type.
    pub fn new() -> Self {
        let mut res = Self {
            sorts: BTreeMap::new(),
        };
        R::accept_visitor(&mut res);
        res
    }

    /// Parse a sort expression and return a [Sorter].
    ///
    /// Valid sort expressions consist of a comma-separated list of sorts, each of
    /// which may be optionally preceded by a `-` to reverse its sense.
    pub fn create_sort(&self, expr: &str) -> Result<Box<dyn Sorter<R>>, SorterError> {
        let parts = expr.split(',').collect::<Vec<&str>>();
        let mut full_sort: Option<Box<dyn Sorter<R>>> = None;
        for part in parts.iter().rev() {
            let part_sort = if let Some(name) = part.strip_prefix('-') {
                self.sorts
                    .get(name)
                    .ok_or_else(|| SorterError::NoSort(part.to_string()))?
                    .instantiate(true)
            } else {
                self.sorts
                    .get(*part)
                    .ok_or_else(|| SorterError::NoSort(part.to_string()))?
                    .instantiate(false)
            };
            full_sort = if let Some(sort) = full_sort {
                Some(Box::new(StackedSorter::new(part_sort, sort)))
            } else {
                Some(part_sort)
            };
        }
        full_sort.ok_or_else(|| SorterError::NoSort(expr.to_string()))
    }
}

impl<R: Sortable> SortVisitor for OrderingSet<R> {
    type Target = R;
    fn visit_sort<F, T, S>(&mut self, name: &str, field: &F, sort: &S)
    where
        F: Field<R, Value = T> + 'static,
        S: SorterTypedClass<T> + 'static,
        <S as SorterTypedClass<T>>::Sorter: 'static,
    {
        self.sorts.insert(
            name.to_string(),
            Box::new(SorterClassImpl::new(field.clone(), sort.clone())),
        );
    }

    fn visit_key_sort<F, T>(&mut self, name: &str, field: &F, key: &str)
    where
        F: Field<R, Value = T> + 'static,
        T: Sortable + 'static,
    {
        let mut v = KeyVisitor {
            name,
            key,
            field: field.clone(),
            parent: self,
        };
        T::accept_visitor(&mut v);
    }
}

struct KeyVisitor<'a, 'b, P, F> {
    name: &'a str,
    key: &'a str,
    field: F,
    parent: &'b mut P,
}

impl<'a, 'b, P, G, R, S> SortVisitor for KeyVisitor<'a, 'b, P, G>
where
    P: SortVisitor<Target = S>,
    G: Field<S, Value = R> + 'static,
    R: 'static,
{
    type Target = R;

    fn visit_sort<F, T, Srt>(&mut self, name: &str, field: &F, sort: &Srt)
    where
        F: Field<R, Value = T> + 'static,
        Srt: SorterTypedClass<T> + 'static,
        <Srt as SorterTypedClass<T>>::Sorter: 'static,
    {
        if name == self.key {
            self.parent.visit_sort(
                self.name,
                &NestedField {
                    outer: self.field.clone(),
                    inner: field.clone(),
                },
                sort,
            );
        }
    }

    fn visit_key_sort<F, T>(&mut self, name: &str, field: &F, key: &str)
    where
        F: Field<R, Value = T> + 'static,
        T: Sortable + 'static,
    {
        if name == self.key {
            let mut v = KeyVisitor {
                name: self.name,
                key,
                field: NestedField {
                    outer: self.field.clone(),
                    inner: field.clone(),
                },
                parent: self.parent,
            };
            T::accept_visitor(&mut v);
        }
    }
}
#[derive(Clone)]
struct NestedField<F, G> {
    outer: F,
    inner: G,
}

impl<F, G, R, T, U> Field<R> for NestedField<F, G>
where
    F: Field<R, Value = T>,
    G: Field<T, Value = U>,
    T: 'static,
{
    type Value = U;
    fn apply_sorter<V: Sorter<Self::Value>>(&self, op: &V, a: &R, b: &R) -> Ordering {
        let n = NestedSorter {
            inner: &self.inner,
            op,
        };
        self.outer.apply_sorter(&n, a, b)
    }
}

struct NestedSorter<'a, 'b, F, P> {
    inner: &'a F,
    op: &'b P,
}

impl<'a, 'b, F, P, T, U> Sorter<T> for NestedSorter<'a, 'b, F, P>
where
    F: Field<T, Value = U>,
    P: Sorter<U>,
{
    fn compare(&self, a: &T, b: &T) -> Ordering {
        self.inner.apply_sorter(self.op, a, b)
    }
}

struct StackedSorter<R> {
    primary: Box<dyn Sorter<R>>,
    secondary: Box<dyn Sorter<R>>,
}

impl<R> StackedSorter<R> {
    pub fn new(primary: Box<dyn Sorter<R>>, secondary: Box<dyn Sorter<R>>) -> Self {
        Self { primary, secondary }
    }
}

impl<R> Sorter<R> for StackedSorter<R> {
    fn compare(&self, a: &R, b: &R) -> Ordering {
        match self.primary.compare(a, b) {
            Ordering::Less => Ordering::Less,
            Ordering::Greater => Ordering::Greater,
            Ordering::Equal => self.secondary.compare(a, b),
        }
    }
}