Module django_query::ordering
source · [−]Expand description
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 Sorters. 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:
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);
Structs
Enums
Traits
Something which returns a particular member of an instance by reference.
A marker for an Accessor that can act as a field.
Something that can receive callbacks about the sort orders a type provides.
Something that can be sorted.
Something that can compare two values of another type T
.
Something that can make a boxed Sorter.
Something which can make a typed Sorter.