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
use std::fmt;
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub struct Span {
pub start: usize,
pub end: usize
}
impl From<(usize, usize)> for Span {
fn from(tup: (usize, usize)) -> Span {
Span::new(tup.0, tup.1)
}
}
impl Span {
pub fn new(start: usize, end: usize) -> Self {
Span { start, end }
}
pub(crate) fn from_node(node: &crate::parser::Node) -> Self {
let span = node.as_span();
Span {
start: span.start(),
end: span.end()
}
}
}
impl fmt::Display for Span {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({}, {})", self.start, self.end)
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum PromDuration {
Seconds(u64),
Minutes(u64),
Hours(u64),
Days(u64),
Weeks(u64),
Years(u64)
}
type StringResult<T> = std::result::Result<T, String>;
impl PromDuration {
pub fn from_pair(unit: &str, value: u64) -> StringResult<PromDuration> {
Ok(match unit {
"s" => PromDuration::Seconds(value),
"m" => PromDuration::Minutes(value),
"h" => PromDuration::Hours(value),
"d" => PromDuration::Days(value),
"w" => PromDuration::Weeks(value),
"y" => PromDuration::Years(value),
u => return Err(format!("invalid duration unit: {:?}", u))
})
}
pub fn as_char(self) -> char {
match self {
PromDuration::Seconds(_) => 's',
PromDuration::Minutes(_) => 'm',
PromDuration::Hours(_) => 'h',
PromDuration::Days(_) => 'd',
PromDuration::Weeks(_) => 'w',
PromDuration::Years(_) => 'y'
}
}
}
impl fmt::Display for PromDuration {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let v = match self {
PromDuration::Seconds(v) => v,
PromDuration::Minutes(v) => v,
PromDuration::Hours(v) => v,
PromDuration::Days(v) => v,
PromDuration::Weeks(v) => v,
PromDuration::Years(v) => v
};
write!(f, "{}{}", v, self.as_char())
}
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Subquery {
pub range: PromDuration,
pub resolution: Option<PromDuration>,
pub span: Option<Span>
}
impl Subquery {
pub fn new(range: PromDuration) -> Self {
Subquery {
range,
resolution: None,
span: None
}
}
pub fn resolution(mut self, res: PromDuration) -> Self {
self.resolution = Some(res);
self
}
pub fn clear_resolution(mut self) -> Self {
self.resolution = None;
self
}
pub fn span<S: Into<Span>>(mut self, span: S) -> Self {
self.span = Some(span.into());
self
}
}
impl fmt::Display for Subquery {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(res) = self.resolution {
write!(f, "[{}:{}]", self.range, res)
} else {
write!(f, "[{}:]", self.range)
}
}
}