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
use super::{FieldSort, GeoDistanceSort, ScriptSort, SortSpecialField};
use std::borrow::Cow;

/// Sorting criterion
#[derive(Clone, PartialEq, Serialize)]
#[serde(untagged)]
pub enum Sort {
    /// Special sort field,
    SpecialField(SortSpecialField),

    /// Sorts by field name
    Field(String),

    /// Sorts by field name with finer control
    FieldSort(FieldSort),

    /// Sorts by a geo distance
    GeoDistanceSort(GeoDistanceSort),

    /// Sort by a script
    ScriptSort(ScriptSort),
}

impl std::fmt::Debug for Sort {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::SpecialField(sort) => sort.fmt(f),
            Self::Field(sort) => sort.fmt(f),
            Self::FieldSort(sort) => sort.fmt(f),
            Self::GeoDistanceSort(sort) => sort.fmt(f),
            Self::ScriptSort(sort) => sort.fmt(f),
        }
    }
}

impl From<SortSpecialField> for Sort {
    fn from(value: SortSpecialField) -> Self {
        Self::SpecialField(value)
    }
}

impl From<&str> for Sort {
    fn from(value: &str) -> Self {
        Self::Field(value.to_string())
    }
}

impl From<Cow<'_, str>> for Sort {
    fn from(value: Cow<'_, str>) -> Self {
        Self::Field(value.to_string())
    }
}

impl From<String> for Sort {
    fn from(value: String) -> Self {
        Self::Field(value)
    }
}

impl From<FieldSort> for Sort {
    fn from(value: FieldSort) -> Self {
        Self::FieldSort(value)
    }
}

impl From<GeoDistanceSort> for Sort {
    fn from(value: GeoDistanceSort) -> Self {
        Self::GeoDistanceSort(value)
    }
}

impl From<ScriptSort> for Sort {
    fn from(value: ScriptSort) -> Self {
        Self::ScriptSort(value)
    }
}

impl IntoIterator for Sort {
    type Item = Self;

    type IntoIter = std::option::IntoIter<Self::Item>;

    fn into_iter(self) -> Self::IntoIter {
        Some(self).into_iter()
    }
}