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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
 * smartcalc v1.0.2
 * Copyright (c) Erhan BARIS (Ruslan Ognyanov Asenov)
 * Licensed under the GNU General Public License v2.0.
 */

use alloc::string::String;
use alloc::string::ToString;
use alloc::collections::btree_map::BTreeMap;
use alloc::sync::Arc;
use core::ops::Deref;

use chrono::{Duration, Timelike};

use crate::config::SmartCalcConfig;
use crate::{constants::ConstantType, tokinizer::Tokinizer, types::{TokenType}, worker::tools::{get_duration, get_number, get_text, get_time, get_date}};
use crate::tokinizer::TokenInfo;
use crate::formatter::{MINUTE, HOUR, DAY, WEEK, MONTH, YEAR};

pub fn duration_parse(config: &SmartCalcConfig, tokinizer: &Tokinizer, fields: &BTreeMap<String, Arc<TokenInfo>>) -> core::result::Result<TokenType, String> {
    if (fields.contains_key("duration")) && fields.contains_key("type") {
        let duration = match get_number("duration", fields) {
            Some(number) => number as i64,
            _ => return Err("Duration information not valid".to_string())
        };

        let duration_type = match get_text("type", fields) {
            Some(number) => number,
            _ => return Err("Duration type information not valid".to_string())
        };

        let constant_type = match config.constant_pair.get(&tokinizer.language).unwrap().get(&duration_type) {
            Some(constant) => constant.clone(),
            None => return Err("Duration type not valid".to_string())
        };

        let calculated_duration = match constant_type {
            ConstantType::Year => Duration::days(365 * duration),
            ConstantType::Month => {
                let years = duration / 12;
                let month = duration % 12;

                Duration::days((365 * years) + (30 * month))
            },
            ConstantType::Day => {
                let years = duration / 365;
                let month = (duration % 365) / 30;
                let day = (duration % 365) % 30;

                Duration::days((365 * years) + (30 * month) + day)
            },
            ConstantType::Week => Duration::weeks(duration),
            ConstantType::Hour => Duration::hours(duration),
            ConstantType::Minute => Duration::minutes(duration),
            ConstantType::Second => Duration::seconds(duration),            
            _ => return Err("Duration type not valid".to_string()) 
        };

        return Ok(TokenType::Duration(calculated_duration));
    }
    Err("Date type not valid".to_string())
}

pub fn combine_durations(_: &SmartCalcConfig, _: &Tokinizer, fields: &BTreeMap<String, Arc<TokenInfo>>) -> core::result::Result<TokenType, String> {
    if (fields.contains_key("1")) && fields.contains_key("2") {
        let mut sum_duration = Duration::zero();

        for key in fields.keys() {
            let duration = match get_duration(key, fields) {
                Some(duration) => duration,
                _ => return Err("Duration information not valid".to_string())
            };

            sum_duration = sum_duration + duration;
        }

        return Ok(TokenType::Duration(sum_duration));
    }
    Err("Date type not valid".to_string())
}

pub fn as_duration(config: &SmartCalcConfig, tokinizer: &Tokinizer, fields: &BTreeMap<String, Arc<TokenInfo>>) -> core::result::Result<TokenType, String> {
    if (fields.contains_key("source")) && fields.contains_key("type") {
        let duration_type = match get_text("type", fields) {
            Some(number) => number,
            _ => return Err("Duration type information not valid".to_string())
        };

        let constant_type = match config.constant_pair.get(&tokinizer.language).unwrap().get(&duration_type) {
            Some(constant) => constant.clone(),
            None => return Err("Duration type not valid".to_string())
        };

        match fields.get("source") {
            Some(token_info) => match token_info.token_type.borrow().deref()  {
                Some(TokenType::Duration(duration)) => {
                    let seconds = duration.num_seconds().abs() as i64;
                    
                    return match constant_type {
                        ConstantType::Day => Ok(TokenType::Duration(Duration::days(seconds / DAY))),
                        ConstantType::Second => Ok(TokenType::Duration(Duration::seconds(seconds))),
                        ConstantType::Minute => Ok(TokenType::Duration(Duration::minutes(seconds / MINUTE as i64))),
                        ConstantType::Hour => Ok(TokenType::Duration(Duration::hours(seconds / HOUR as i64))),
                        ConstantType::Week => Ok(TokenType::Duration(Duration::weeks(seconds / WEEK as i64))),
                        _ => return Err("Duration type not valid".to_string()) 
                    };
                },
                Some(TokenType::Time(time, _)) => {
                    let seconds = time.num_seconds_from_midnight() as i64;
                    
                    return match constant_type {
                        ConstantType::Month => Ok(TokenType::Duration(Duration::days(seconds / MONTH))),
                        ConstantType::Year => Ok(TokenType::Duration(Duration::days(seconds / YEAR))),
                        ConstantType::Day => Ok(TokenType::Duration(Duration::days(seconds / DAY))),
                        ConstantType::Second => Ok(TokenType::Duration(Duration::seconds(seconds))),
                        ConstantType::Minute => Ok(TokenType::Duration(Duration::minutes(seconds / MINUTE as i64))),
                        ConstantType::Hour => Ok(TokenType::Duration(Duration::hours(seconds / HOUR as i64))),
                        ConstantType::Week => Ok(TokenType::Duration(Duration::weeks(seconds / WEEK as i64))),

                        _ => return Err("Duration type not valid".to_string()) 
                    };
                }
                _ => ()
            },
            None => return Err("Source information not valid".to_string())
        };
        
        
        let duration = match get_number("duration", fields) {
            Some(number) => number as i64,
            _ => return Err("Duration information not valid".to_string())
        };

        let calculated_duration = match constant_type {
            ConstantType::Day => Duration::days(duration),
            ConstantType::Month => Duration::days(duration * 30),
            ConstantType::Year => Duration::days(duration * 365),
            ConstantType::Second => Duration::seconds(duration),
            ConstantType::Minute => Duration::minutes(duration),
            ConstantType::Hour => Duration::hours(duration),
            _ => return Err("Duration type not valid".to_string()) 
        };

        return Ok(TokenType::Duration(calculated_duration));
    }
    Err("Date type not valid".to_string())
}

pub fn to_duration(_: &SmartCalcConfig, _: &Tokinizer, fields: &BTreeMap<String, Arc<TokenInfo>>) -> core::result::Result<TokenType, String> {
    if (fields.contains_key("source")) && fields.contains_key("target") {
        //todo: calculate with timezone
        if let (Some((source, _)), Some((target, _))) = (get_time("source", fields), get_time("target", fields)) {
            let diff = if target > source { target - source } else { source - target};
            return Ok(TokenType::Duration(diff));
        }
        
        //todo: calculate with timezone
        return match (get_date("source", fields), get_date("target", fields)) {
            (Some((source, _)), Some((target, _))) => {
                let diff = if target > source { target - source } else { source - target};
                return Ok(TokenType::Duration(diff));
            },
            _ => Err("Time information not valid".to_string())
        }
    }

    Err("Time diff not valid".to_string())
}

#[cfg(test)]
#[test]
fn duration_parse_test_1() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("10 days".to_string());

    assert_eq!(tokens.len(), 3);
    
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::days(10))));
}

#[cfg(test)]
#[test]
fn duration_parse_test_2() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("10 weeks".to_string());

    assert_eq!(tokens.len(), 3);
    
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::weeks(10))));
}

#[cfg(test)]
#[test]
fn duration_parse_test_3() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("60 minutes".to_string());

    assert_eq!(tokens.len(), 3);
    
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::minutes(60))));
}

#[cfg(test)]
#[test]
fn duration_parse_test_4() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("5 weeks as seconds".to_string());
    assert_eq!(tokens.len(), 6);
    
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::seconds(3024000))));
}

#[cfg(test)]
#[test]
fn duration_parse_test_5() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("48 weeks as hours".to_string());

    assert_eq!(tokens.len(), 6);
    
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::hours(8064))));
}

#[cfg(test)]
#[test]
fn duration_parse_test_6() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("11:50 as hour".to_string());

    assert_eq!(tokens.len(), 4);
    
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::hours(11))));
}

#[cfg(test)]
#[test]
fn duration_parse_test_7() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("2 week 5 hours as hours".to_string());

    assert_eq!(tokens.len(), 10);
    
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::hours(341))));
}

#[cfg(test)]
#[test]
fn to_duration_1() {
    use core::ops::Deref;
    use crate::tokinizer::test::execute;
    
    let tokens = execute("17:30 to 20:45".to_string());

    assert_eq!(tokens.len(), 4);
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::seconds(11700))));
}

#[cfg(test)]
#[test]
fn to_duration_2() {
    use crate::tokinizer::test::execute;
    
    let tokens = execute("20:45 to 17:30".to_string());

    assert_eq!(tokens.len(), 4);
    assert_eq!(tokens[0].token_type.borrow().deref(), &Some(TokenType::Duration(Duration::seconds(11700))));
}