From ca9419ca4fee58e0efde179b2b069f2b6c15dda8 Mon Sep 17 00:00:00 2001 From: Default Cube <56493793+Quicksilver151@users.noreply.github.com> Date: Fri, 27 Jan 2023 09:34:56 +0500 Subject: [PATCH] island data parse set to build time - made structs for more readability - formatted code with whitespace - changed a bunch of usize to u32 --- src/functions/edit.rs | 50 +++++++++++++-------------- src/functions/extras.rs | 4 +-- src/structs.rs | 76 ++++++++++++++++++++++++++--------------- 3 files changed, 74 insertions(+), 56 deletions(-) diff --git a/src/functions/edit.rs b/src/functions/edit.rs index 9ca0627..c7c67c8 100755 --- a/src/functions/edit.rs +++ b/src/functions/edit.rs @@ -1,28 +1,21 @@ use crate::*; -// pub static ATOLLS_DAT: &str = include_str!("../data/atolls.csv"); -pub static ISLAND_DAT: &str = include_str!("../data/islands.csv"); pub fn edit() { // start new buffer print!("\x1b[?1049h"); println!("EDIT MODE\n changes are made to the config file\n"); - let atoll_data : Vec = ATOLL_DATA.iter().map(|x| AtollData::new_from_array(*x)).collect(); - let raw_island_data: Vec = get_vec_from_db(ISLAND_DAT); - - // [row][column: 0,1,2] (0 = atoll_index, 1=name, 2=dhi_name) - // let atoll_data: Vec> = raw_atoll_data - // .iter() - // .map(|x| x.split(';').collect()) - // .collect(); - - // [row][coloumn: 0,2,3,4] (0 = time index, 2=atoll, 3=name, 4=dhi_name) - let island_data: Vec> = raw_island_data + let atoll_data : Vec = ATOLL_DATA + .iter() + .map(|x| AtollData::new_from_array(*x)) + .collect(); + let island_data: Vec = ISLAND_DATA .iter() - .map(|x| x.split(';').collect()) + .map(|x| IslandData::new_from_array(*x)) .collect(); + clear_screen(); // atoll title // println!("Index\tName\tDhiName"); @@ -36,10 +29,11 @@ pub fn edit() { atoll_data .iter() .for_each(|atoll| println!("{0: <5} | {1: <10} | {2: <10}", atoll.index, atoll.en_code, atoll.dh_code)); - println!("Input a number from the first colum to select Atoll(1-20) or select a timeset(42-82):"); - let selected_atoll_index: usize = - get_number_input().expect("Must be a non zero positive integer"); - let selected_time_index: usize; + + println!("\nInput a number from the first colum to select Atoll(1-20) or select a timeset(42-82):"); + + let selected_atoll_index: u32 = get_number_input().expect("Must be a non zero positive integer"); + let selected_time_index : u32; if std::ops::RangeInclusive::new(1, 20).contains(&selected_atoll_index) { clear_screen(); @@ -49,26 +43,30 @@ pub fn edit() { "Index", "Timeset", "Island Name", "Dhi Name" ); println!("-------------------------------------------"); + let mut i = 0; - let mut selectables: Vec = vec![]; + let mut selectables: Vec = vec![]; // print island list for island in island_data.iter() { - if island[2].parse::().unwrap_or(1) == selected_atoll_index { + if island.atoll == selected_atoll_index{ i += 1; - selectables.append(&mut vec![island[0].parse::().unwrap_or(41)]); + selectables.append(&mut vec![island.timeset]); println!( "{0: <5} | {1: <7} | {2: <15} | {3: <10}", - i, island[0], island[3], island[4] + i, island.timeset, island.en_name, island.dh_name ); } } - println!("Input a number from the first column to select prefered timeset:"); - selected_time_index = selectables[get_number_input().unwrap()]; - } else if std::ops::RangeInclusive::new(41, 82).contains(&selected_atoll_index) { + println!("\nInput a number from the first column to select your island:"); + selected_time_index = selectables[get_number_input().unwrap() as usize]; + + } + else if std::ops::RangeInclusive::new(41, 82).contains(&selected_atoll_index) { selected_time_index = selected_atoll_index; - } else { + } + else { println!("\x1b[?1049l"); panic!("value not within range"); diff --git a/src/functions/extras.rs b/src/functions/extras.rs index df89917..4ca3c19 100755 --- a/src/functions/extras.rs +++ b/src/functions/extras.rs @@ -28,14 +28,14 @@ pub fn get_current_time(format: &TimeFormat) -> (u32, u32, u32, String) { } // input management -pub fn get_number_input() -> Result { +pub fn get_number_input() -> Result { let mut input_text = String::new(); std::io::stdin() .read_line(&mut input_text) .expect("failed to read from stdin"); let trimmed = input_text.trim(); - trimmed.parse::() + trimmed.parse::() } // get db data diff --git a/src/structs.rs b/src/structs.rs index f34d73f..1e539b8 100755 --- a/src/structs.rs +++ b/src/structs.rs @@ -2,18 +2,16 @@ use crate::*; #[derive(Default, Debug, Serialize, Deserialize)] pub struct Config { - pub island_index: usize, + pub island_index: u32, pub island_name: String, } -#[derive(Serialize,Deserialize)] pub struct AtollData{ pub index: u32, pub en_code: String, pub dh_code: String, pub ar_code: String, } - impl AtollData{ pub fn new_from_array(data: [&str;4]) -> AtollData{ let new_atoll_data: AtollData = AtollData { @@ -27,23 +25,45 @@ impl AtollData{ } } +pub struct IslandData{ + pub timeset: u32, + pub index: u32, + pub atoll: u32, + pub en_name: String, + pub dh_name: String, + pub ar_name: String, +} +impl IslandData{ + pub fn new_from_array(data: [&str;10]) -> IslandData{ + let new_island_data: IslandData = IslandData { + timeset: data[0].parse::().unwrap(), + index: data[1].parse::().unwrap(), + atoll: data[2].parse::().unwrap(), + en_name: data[3].to_string(), + dh_name: data[4].to_string(), + ar_name: data[5].to_string(), + }; + + new_island_data + + } + +} + + #[derive(Debug)] pub struct PrayerData { pub island_index: u32, - pub day: u32, - pub fajr: u32, - pub sun: u32, + pub day: u32, + pub fajr: u32, + pub sun: u32, pub dhuhur: u32, - pub asr: u32, + pub asr: u32, pub magrib: u32, - pub isha: u32, + pub isha: u32, } - impl PrayerData { - // fn new() -> PrayerData{ - // PrayerData { island_index: 0, day: 0, fajr: 0, sun: 0, dhuhur: 0, asr: 0, magrib: 0, isha: 0} - // } - + pub fn new_from_array(val: &[u32; 8]) -> PrayerData { PrayerData { island_index: val[0], @@ -56,7 +76,7 @@ impl PrayerData { isha: val[7], } } - + pub fn vec_from_island_set(&self) -> Vec { let mut val = vec![0; 6]; val[0] = self.fajr as i32; @@ -65,20 +85,20 @@ impl PrayerData { val[3] = self.asr as i32; val[4] = self.magrib as i32; val[5] = self.isha as i32; - + val } - + pub fn flag_formatted_output(&self, flag: &Flag) { let (flag, pt_vec) = (flag, self.vec_from_island_set()); - + // some temporary inits let names = vec!["Fajr", "Sun", "Dhuhur", "Asr", "Magrib", "Isha"]; - + // optional title if flag.title && !flag.tui && !flag.edit { let (hour, minute, second, time) = get_current_time(&flag.time); - + println!("Salat_MV-cli"); println!("---------------------"); println!( @@ -92,7 +112,7 @@ impl PrayerData { println!("---------------------"); println!(); } - + for (i, pt) in pt_vec.iter().enumerate() { match flag.disp { // only numbers or with info @@ -165,7 +185,7 @@ impl TimeConversion for i32 { self.to_string() } } - + fn to_12(self) -> (u32, String) { let half = if self > 11 { "pm" } else { "am" }; if self > 12 { @@ -174,17 +194,17 @@ impl TimeConversion for i32 { (self as u32, half.to_string()) } } - + fn minutes_to_time(self, time_format: &TimeFormat) -> String { let minute = &self % 60; let mut hour = self as u32 / 60; let mut period = "".to_string(); - + match time_format { TimeFormat::TWHour => (hour, period) = hour.to_12(), TimeFormat::TFHour => {} } - + let hour = hour.add_zero(); let minute = minute.add_zero(); format!("{}:{} {}", hour, minute, period) @@ -201,7 +221,7 @@ impl TimeConversion for u32 { self.to_string() } } - + fn to_12(self) -> (u32, String) { let half = if self > 11 { "pm" } else { "am" }; if self > 12 { @@ -210,17 +230,17 @@ impl TimeConversion for u32 { (self, half.to_string()) } } - + fn minutes_to_time(self, time_format: &TimeFormat) -> String { let minute = &self % 60; let mut hour = self / 60; let mut period = "".to_string(); - + match time_format { TimeFormat::TWHour => (hour, period) = hour.to_12(), TimeFormat::TFHour => {} } - + let hour = hour.add_zero(); let minute = minute.add_zero(); format!("{}:{} {}", hour, minute, period)