-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructs.rs
More file actions
203 lines (171 loc) · 5.59 KB
/
structs.rs
File metadata and controls
203 lines (171 loc) · 5.59 KB
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
pub mod conf;
pub use conf::*;
pub mod input;
// pub use input::*;
pub mod data;
pub use data::*;
use crate::Screen;
#[derive(Debug, Default)]
pub struct AppState {
pub fullscreen: bool,
pub prayertime: PrayerTime,
pub input_map: input::InputMap,
pub input_char: char,
pub config: Config,
pub timeset_data: TimeSetData,
pub day_offset: i64,
pub screen: Screen,
}
use serde::*;
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Eq)]
pub struct PrayerTime {
pub index: u32,
pub day: u32,
pub fajr: u32,
pub sun: u32,
pub dhuhur: u32,
pub asr: u32,
pub magrib: u32,
pub isha: u32,
}
impl PrayerTime {
pub fn from_vec(list:Vec<u32>) -> PrayerTime {
PrayerTime {
index: list[0],
day: list[1],
fajr: list[2],
sun: list[3],
dhuhur:list[4],
asr: list[5],
magrib:list[6],
isha: list[7],
}
}
pub fn to_vec(&self) -> Vec<u32> {
vec![
// self.index,
// self.day,
self.fajr,
self.sun,
self.dhuhur,
self.asr,
self.magrib,
self.isha,
]
}
pub fn get_current_index(&self) -> usize {
use chrono::Timelike;
let current_time = chrono::offset::Local::now();
let minute = current_time.hour() * 60 + current_time.minute();
self.to_vec().into_iter().position(|x| x > minute).unwrap_or(0)
}
pub fn format(&self, config: &Config) -> Vec<String> {
let mut data_list: Vec<String> = vec![self.index.to_string(), self.day.to_string()];
data_list.append(&mut self.format_time(config));
data_list
}
pub fn format_time(&self, config: &Config) -> Vec<String> {
let time_list: Vec<u32> = self.to_vec();
match config.display.format {
TimeFormat::Twelve => {
return time_list.iter().map(to_time).map(|t| format!("{:0>2}:{:0>2} {}",{if t.0 >13{t.0%12}else{t.0}},t.1, {if t.0 > 11{"PM"} else{"AM"} })).collect();
},
TimeFormat::TwentyFour => {
return time_list.iter().map(to_time).map(|t| format!("{:0>2}:{:0>2}",t.0,t.1)).collect();
},
TimeFormat::Minutes => {
return time_list.iter().map(|t| format!("{}",t)).collect();
},
}
}
pub fn output_format(&self, config: &Config) -> String {
// use serde_json::to_writer_pretty;
let mut time_list: Vec<String> = self.format(config);
let outconf = &config.raw_output;
let current = self.get_current_index().to_string();
time_list.append(&mut vec![current]);
match config.raw_output.mode {
RawOutputMode::Array => format!("{:?}", time_list),
RawOutputMode::Custom => todo!(),
RawOutputMode::TOML => todo!(),
RawOutputMode::PrettyJson => to_json(time_list, true),
RawOutputMode::Json => to_json(time_list, false),
RawOutputMode::RawData => {
let mut string = "".to_owned();
for time in time_list.iter() {
if time == time_list.last().unwrap(){
string.push_str(time);
continue;
}
string.push_str(&format!("{}{}",time,outconf.raw_separator))
}
string
}
}
// let
}
}
fn to_time(minutes: &u32) -> (u32, u32){
(minutes / 60, minutes % 60)
}
fn to_json(time_list: Vec<String>, pretty: bool) -> String {
if pretty {
format!(
"{{
\"index\":\"{}\",
\"day\":\"{}\",
\"fajr\":\"{}\",
\"sun\":\"{}\",
\"dhuhur\":\"{}\",
\"asr\":\"{}\",
\"magrib\":\"{}\",
\"isha\":\"{}\",
\"current\":\"{}\"
}}",
time_list[0],
time_list[1],
time_list[2],
time_list[3],
time_list[4],
time_list[5],
time_list[6],
time_list[7],
time_list[8],
)
} else {
format!("{{\"index\":\"{}\",\"day\":\"{}\",\"fajr\":\"{}\",\"sun\":\"{}\",\"dhuhur\":\"{}\",\"asr\":\"{}\",\"magrib\":\"{}\",\"isha\":\"{}\",\"current\":\"{}\"}}",
time_list[0],
time_list[1],
time_list[2],
time_list[3],
time_list[4],
time_list[5],
time_list[6],
time_list[7],
time_list[8],
)
}
}
#[test]
fn test_format() {
let value = PrayerTime { index: 77, day: 225, fajr: 293, sun: 365, dhuhur: 736, asr: 932, magrib: 1098, isha: 1171 };
let mut config = Config::default();
config.display.format = TimeFormat::Twelve;
let expected:Vec<String> = vec!["77", "225", "04:53 AM", "06:05 AM", "12:16 PM", "03:32 PM", "06:18 PM", "07:31 PM"]
.into_iter().map(|x|x.to_owned()).collect();
let result = value.format(&config);
config.display.format = TimeFormat::TwentyFour;
let expected2:Vec<String> = vec!["77", "225", "04:53", "06:05", "12:16", "15:32", "18:18", "19:31"]
.into_iter().map(|x|x.to_owned()).collect();
let result2 = value.format(&config);
assert_eq!(expected, result);
assert_eq!(expected2,result2);
}
#[test]
fn test_prayertime() {
let expected = PrayerTime { index: 77, day: 225, fajr: 293, sun: 365, dhuhur: 736, asr: 932, magrib: 1098, isha: 1171 };
let result = PrayerTime::from_vec(vec![77, 225, 293, 365, 736, 932, 1098, 1171]);
let result2 = TimeSetData::load("GDh. Vilingili").unwrap().data_from_day(225);
assert_eq!(expected, result);
assert_eq!(expected, result2);
}