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
use serde::ser::{Serialize, Serializer};

/// Whenever durations need to be specified, e.g. for a `timeout` parameter,
/// the duration must specify the unit, like `2d` for 2 days.
///
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#time-units>
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(missing_docs)]
pub enum Time {
    Days(u64),
    Hours(u64),
    Minutes(u64),
    Seconds(u64),
    Milliseconds(u64),
    Microseconds(u64),
    Nanoseconds(u64),
}

impl Serialize for Time {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Days(u) => format!("{}d", u),
            Self::Hours(u) => format!("{}h", u),
            Self::Minutes(u) => format!("{}m", u),
            Self::Seconds(u) => format!("{}s", u),
            Self::Milliseconds(u) => format!("{}ms", u),
            Self::Microseconds(u) => format!("{}micros", u),
            Self::Nanoseconds(u) => format!("{}nanos", u),
        }
        .serialize(serializer)
    }
}

/// Whenever the byte size of data needs to be specified, e.g. when setting a
/// buffer size parameter, the value must specify the unit,
/// like `10kb` for 10 kilobytes.
/// Note that these units use powers of 1024, so `1kb` means 1024 bytes.
///
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#byte-units>
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(missing_docs)]
pub enum Byte {
    Bytes(u64),
    Kilobytes(u64),
    Megabytes(u64),
    Gigabytes(u64),
    Terabytes(u64),
    Petabytes(u64),
}

impl Serialize for Byte {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Bytes(u) => format!("{}b", u),
            Self::Kilobytes(u) => format!("{}kb", u),
            Self::Megabytes(u) => format!("{}mb", u),
            Self::Gigabytes(u) => format!("{}gb", u),
            Self::Terabytes(u) => format!("{}tb", u),
            Self::Petabytes(u) => format!("{}pb", u),
        }
        .serialize(serializer)
    }
}

/// Unit-less quantities means that they don’t have a "unit"
/// like "bytes" or "Hertz" or "meter" or "long tonne".
///
/// If one of these quantities is large we’ll print it out like 10m for
/// 10,000,000 or 7k for 7,000. We’ll still print 87 when we mean 87 though.
///
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/common-options.html#size-units>
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(missing_docs)]
pub enum Size {
    Kilo(u64),
    Mega(u64),
    Giga(u64),
    Tera(u64),
    Peta(u64),
}

impl Serialize for Size {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Kilo(u) => format!("{}k", u),
            Self::Mega(u) => format!("{}m", u),
            Self::Giga(u) => format!("{}g", u),
            Self::Tera(u) => format!("{}t", u),
            Self::Peta(u) => format!("{}p", u),
        }
        .serialize(serializer)
    }
}

/// Wherever distances need to be specified, such as the `distance` parameter
/// in the
/// [Geo-distance](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html)
/// ), the default unit is meters if none is specified.
/// Distances can be specified in other units,
/// such as `"1km"` or `"2mi"` (2 miles).
///
/// <https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-distance-query.html>
#[derive(Debug, PartialEq, Clone, Copy)]
#[allow(missing_docs)]
pub enum Distance {
    Miles(u64),
    Yards(u64),
    Feet(u64),
    Inches(u64),
    Kilometers(u64),
    Meters(u64),
    Centimeter(u64),
    Millimeters(u64),
    NauticalMiles(u64),
}

impl Serialize for Distance {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        match self {
            Self::Miles(u) => format!("{}mi", u),
            Self::Yards(u) => format!("{}yd", u),
            Self::Feet(u) => format!("{}ft", u),
            Self::Inches(u) => format!("{}in", u),
            Self::Kilometers(u) => format!("{}km", u),
            Self::Meters(u) => format!("{}m", u),
            Self::Centimeter(u) => format!("{}cm", u),
            Self::Millimeters(u) => format!("{}mm", u),
            Self::NauticalMiles(u) => format!("{}nmi", u),
        }
        .serialize(serializer)
    }
}