gitql_core/values/
converters.rs1use crate::values::boolean::BoolValue;
2use crate::values::date::DateValue;
3use crate::values::datetime::DateTimeValue;
4use crate::values::null::NullValue;
5use crate::values::time::TimeValue;
6use crate::values::Value;
7
8pub fn string_literal_to_time(literal: &str) -> Box<dyn Value> {
9 Box::new(TimeValue {
10 value: literal.to_string(),
11 })
12}
13
14pub fn string_literal_to_date(literal: &str) -> Box<dyn Value> {
15 let date_time = chrono::NaiveDate::parse_from_str(literal, "%Y-%m-%d").ok();
16 let timestamp = if let Some(date) = date_time {
17 let zero_time = chrono::NaiveTime::from_hms_opt(0, 0, 0).unwrap();
18 date.and_time(zero_time).and_utc().timestamp()
19 } else {
20 0
21 };
22
23 Box::new(DateValue { timestamp })
24}
25
26pub fn string_literal_to_date_time(literal: &str) -> Box<dyn Value> {
27 let date_time_format = if literal.contains('.') {
28 "%Y-%m-%d %H:%M:%S%.3f"
29 } else {
30 "%Y-%m-%d %H:%M:%S"
31 };
32
33 let date_time = chrono::NaiveDateTime::parse_from_str(literal, date_time_format);
34 if date_time.is_err() {
35 return Box::new(DateTimeValue { value: 0 });
36 }
37
38 let timestamp = date_time.ok().unwrap().and_utc().timestamp();
39 Box::new(DateTimeValue { value: timestamp })
40}
41
42pub fn string_literal_to_boolean(literal: &str) -> Box<dyn Value> {
43 match literal {
44 "t" => Box::new(BoolValue::new_true()),
46 "true" => Box::new(BoolValue::new_true()),
47 "y" => Box::new(BoolValue::new_true()),
48 "yes" => Box::new(BoolValue::new_true()),
49 "1" => Box::new(BoolValue::new_true()),
50 "f" => Box::new(BoolValue::new_false()),
52 "false" => Box::new(BoolValue::new_false()),
53 "n" => Box::new(BoolValue::new_false()),
54 "no" => Box::new(BoolValue::new_false()),
55 "0" => Box::new(BoolValue::new_false()),
56 _ => Box::new(NullValue),
58 }
59}