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
use super::SortOrder;
use crate::util::ShouldSkip;
use crate::{Script, ScriptSortType};
use serde::Serialize;
#[derive(Debug, Clone, PartialEq, Serialize)]
#[serde(remote = "Self")]
pub struct ScriptSort {
script: Script,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
order: Option<SortOrder>,
#[serde(skip_serializing_if = "ShouldSkip::should_skip")]
r#type: Option<ScriptSortType>,
}
impl ScriptSort {
pub fn new(script: Script) -> Self {
Self {
script,
order: None,
r#type: None,
}
}
pub fn ascending(script: Script) -> Self {
Self::new(script).order(SortOrder::Asc)
}
pub fn descending(script: Script) -> Self {
Self::new(script).order(SortOrder::Desc)
}
pub fn order(mut self, order: SortOrder) -> Self {
self.order = Some(order);
self
}
pub fn r#type(mut self, r#type: ScriptSortType) -> Self {
self.r#type = Some(r#type);
self
}
}
impl IntoIterator for ScriptSort {
type Item = Self;
type IntoIter = std::option::IntoIter<Self::Item>;
fn into_iter(self) -> Self::IntoIter {
Some(self).into_iter()
}
}
serialize_with_root!("_script": ScriptSort);
#[cfg(test)]
mod tests {
use super::*;
use crate::util::assert_serialize;
#[test]
fn serialization() {
assert_serialize(
ScriptSort::ascending(
Script::source("doc['numberOfCommits'].value * params.factor").param("factor", 1.1),
)
.r#type(ScriptSortType::Number),
json!({
"_script": {
"order": "asc",
"type": "number",
"script": {
"source": "doc['numberOfCommits'].value * params.factor",
"params": {
"factor": 1.1
}
}
}
}),
);
}
}