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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
use crate::data::position::Position;
use crate::data::primitive::{
PrimitiveArray, PrimitiveBoolean, PrimitiveFloat, PrimitiveInt, PrimitiveString,
};
use std::time::{SystemTime, UNIX_EPOCH};
use crate::data::{ast::Interval, ArgsType, Literal};
use crate::error_format::*;
use uuid::v1::{Context, Timestamp};
use uuid::Uuid;
use rand::seq::SliceRandom;
use rand::Rng;
pub fn one_of(args: ArgsType, flow_name: &str, interval: Interval) -> Result<Literal, ErrorInfo> {
match args.get("array", 0) {
Some(literal) => {
let res = Literal::get_value::<Vec<Literal>>(
&literal.primitive,
flow_name,
literal.interval,
ERROR_ONE_OF.to_owned(),
)?;
match res.get(rand::thread_rng().gen_range(0..res.len())) {
Some(lit) => Ok(lit.to_owned()),
None => Err(gen_error_info(
Position::new(literal.interval, flow_name),
ERROR_ONE_OF.to_owned(),
)),
}
}
None => Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_ONE_OF.to_owned(),
)),
}
}
pub fn shuffle(args: ArgsType, flow_name: &str, interval: Interval) -> Result<Literal, ErrorInfo> {
match args.get("array", 0) {
Some(literal) => {
let res = Literal::get_value::<Vec<Literal>>(
&literal.primitive,
flow_name,
interval,
ERROR_SHUFFLE.to_owned(),
)?;
let mut vec = res.to_owned();
vec.shuffle(&mut rand::thread_rng());
Ok(PrimitiveArray::get_literal(&vec, literal.interval))
}
None => Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_SHUFFLE.to_owned(),
)),
}
}
pub fn length(args: ArgsType, flow_name: &str, interval: Interval) -> Result<Literal, ErrorInfo> {
match args.get("length", 0) {
Some(literal) => {
if let Ok(res) = Literal::get_value::<Vec<Literal>>(
&literal.primitive,
flow_name,
interval,
ERROR_LENGTH.to_owned(),
) {
return Ok(PrimitiveInt::get_literal(
res.len() as i64,
literal.interval,
));
}
if let Ok(res) =
Literal::get_value::<String>(&literal.primitive, flow_name, interval, ERROR_LENGTH.to_owned())
{
return Ok(PrimitiveInt::get_literal(
res.len() as i64,
literal.interval,
));
}
Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_LENGTH.to_owned(),
))
}
None => Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_LENGTH.to_owned(),
)),
}
}
pub fn find(args: ArgsType, flow_name: &str, interval: Interval) -> Result<Literal, ErrorInfo> {
let mut string = None;
let mut case = false;
if let Some(literal) = args.get("in", 1) {
if let Ok(res) =
Literal::get_value::<String>(&literal.primitive, flow_name, interval, ERROR_FIND.to_owned())
{
string = Some(res);
}
} else if string.is_none() {
return Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_FIND.to_owned(),
));
}
if let Some(literal) = args.get("in", 1) {
if let Ok(res) =
Literal::get_value::<bool>(&literal.primitive, flow_name, interval, ERROR_FIND.to_owned())
{
case = *res;
}
}
match (args.get("value", 0), string) {
(Some(literal), Some(string)) => {
let res =
Literal::get_value::<String>(&literal.primitive, flow_name, interval, ERROR_FIND.to_owned())?;
if case {
Ok(PrimitiveBoolean::get_literal(
string.contains(res),
interval,
))
} else {
Ok(PrimitiveBoolean::get_literal(
string.to_lowercase().contains(&res.to_lowercase()),
interval,
))
}
}
(_, _) => Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_FIND.to_owned(),
)),
}
}
pub fn random(interval: Interval) -> Result<Literal, ErrorInfo> {
let mut rng = rand::thread_rng();
let random: f64 = rng.gen();
Ok(PrimitiveFloat::get_literal(random, interval))
}
pub fn floor(args: ArgsType, flow_name: &str, interval: Interval) -> Result<Literal, ErrorInfo> {
match args.get("float", 0) {
Some(literal) => {
let res =
Literal::get_value::<f64>(&literal.primitive, flow_name, interval, ERROR_FLOOR.to_owned())?;
Ok(PrimitiveFloat::get_literal(res.floor(), literal.interval))
}
_ => Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_FLOOR.to_owned(),
)),
}
}
pub fn uuid_command(args: ArgsType, flow_name: &str, interval: Interval) -> Result<Literal, ErrorInfo> {
if args.len() == 0 {
return Ok(PrimitiveString::get_literal(
&Uuid::new_v4().to_string(),
interval,
));
}
match args.get("value", 0) {
Some(literal) => {
let arg =
Literal::get_value::<String>(&literal.primitive, flow_name, interval, ERROR_FLOOR.to_owned())?;
match arg {
arg if arg == "v1" => {
let time = SystemTime::now().duration_since(UNIX_EPOCH)?;
let context = Context::new(rand::thread_rng().gen());
let ts = Timestamp::from_unix(&context, time.as_secs(), time.subsec_nanos());
let node_id = &[
rand::thread_rng().gen(),
rand::thread_rng().gen(),
rand::thread_rng().gen(),
rand::thread_rng().gen(),
rand::thread_rng().gen(),
rand::thread_rng().gen(),
];
Ok(PrimitiveString::get_literal(
&Uuid::new_v1(ts, node_id)?.to_string(),
interval,
))
}
arg if arg == "v4" => Ok(PrimitiveString::get_literal(
&Uuid::new_v4().to_string(),
interval,
)),
_ => Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_UUID.to_owned(),
)),
}
}
_ => Err(gen_error_info(
Position::new(interval, flow_name),
ERROR_UUID.to_owned(),
)),
}
}