Skip to content

Commit

Permalink
Added mandatory patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
Mutoxicated committed Mar 18, 2024
1 parent a30608e commit 417825c
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 57 deletions.
60 changes: 51 additions & 9 deletions data.json
Original file line number Diff line number Diff line change
@@ -1,37 +1,79 @@
{
"monday": {
"day_type": "Monday",
"patterns": []
"patterns": [
{
"name": "Sleep",
"date_time": "2024-03-18T23:30:00",
"once": false,
"mandatory": true
}
]
},
"tuesday": {
"day_type": "Tuesday",
"patterns": []
"patterns": [
{
"name": "Sleep",
"date_time": "2024-03-18T23:30:00",
"once": false,
"mandatory": true
}
]
},
"wednesday": {
"day_type": "Wednesday",
"patterns": []
"patterns": [
{
"name": "Sleep",
"date_time": "2024-03-18T23:30:00",
"once": false,
"mandatory": true
}
]
},
"thursday": {
"day_type": "Thursday",
"patterns": []
"patterns": [
{
"name": "Sleep",
"date_time": "2024-03-18T23:30:00",
"once": false,
"mandatory": true
}
]
},
"friday": {
"day_type": "Friday",
"patterns": []
"patterns": [
{
"name": "Sleep",
"date_time": "2024-03-18T23:30:00",
"once": false,
"mandatory": true
}
]
},
"saturday": {
"day_type": "Saturday",
"patterns": [
{
"name": "o",
"date_time": "2024-03-16T20:00:00",
"name": "Sleep",
"date_time": "2024-03-18T23:30:00",
"once": false,
"mandatory": false
"mandatory": true
}
]
},
"sunday": {
"day_type": "Sunday",
"patterns": []
"patterns": [
{
"name": "Sleep",
"date_time": "2024-03-18T23:30:00",
"once": false,
"mandatory": true
}
]
}
}
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ fn main() {
/* TODO:
1.Timeline of a given day
2.Change pattern command
3.Add mandatory pattern "sleep"
2.Change pattern command CHECK
3.Add mandatory pattern "sleep" CHECK
*/
74 changes: 46 additions & 28 deletions src/time/day.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt::Display;
use std::{fmt::Display, str::FromStr};

use crate::time::pattern::Pattern;
use chrono::NaiveDateTime;
use chrono::{Local, NaiveDateTime, NaiveTime};
use serde::{Deserialize, Serialize};

use super::PatternInfo;
Expand Down Expand Up @@ -41,7 +41,14 @@ impl Day {
pub fn new(day_type: DayType) -> Self {
Self {
day_type,
patterns: Vec::new(),
patterns: vec![
Pattern {
name:"Sleep".to_owned(),
date_time: NaiveDateTime::new(Local::now().date_naive(),NaiveTime::from_str("23:30").unwrap()),
once: false,
mandatory: true
}
],
}
}

Expand Down Expand Up @@ -100,34 +107,45 @@ impl Day {
self.patterns.sort();
}

fn remove_pat(&mut self, index:usize){
if !self.patterns[index].mandatory {
self.patterns.remove(index);
}
}

pub fn remove_pattern(&mut self, name: String, pdt: PatternDetectionType) {
if pdt == PatternDetectionType::All {
if name.is_empty() {
self.patterns.clear();
return;
}
self.patterns.retain(|x| x.name != name);
}else {
let mut ui = 0;
let mut occurences = 0;
for i in 0..self.patterns.len() {
if self.patterns[i].name == name {
occurences += 1;
ui = i;
if let PatternDetectionType::Nth(x) = pdt {
if x == occurences {
self.patterns.remove(ui);
return;
}
let mut ui = 0;
let mut occurences = 0;
for i in 0..self.patterns.len() {
if self.patterns[i].name == name {
occurences += 1;
ui = i;
if let PatternDetectionType::Nth(x) = pdt {
if x == occurences {
self.remove_pat(ui);
return;
}
}else {
self.remove_pat(i)
}
}
if let PatternDetectionType::Nth(x) = pdt {
if occurences >= x || occurences == 0 {
return;
}
self.patterns.remove(ui);
}
if let PatternDetectionType::Nth(x) = pdt {
if occurences >= x || occurences == 0 {
return;
}
self.patterns.remove(ui);
}
}

fn change_pat(&mut self, index:usize, pi: &PatternInfo){
if !self.patterns[index].mandatory {
self.patterns[index].from(pi);
}else {
let oldname = self.patterns[index].name.clone();
self.patterns[index].from(pi);
self.patterns[index].name = oldname;
self.patterns[index].once = false;
}
}

Expand All @@ -140,11 +158,11 @@ impl Day {
ui = i;
if let PatternDetectionType::Nth(x) = pdt {
if x == occurences {
self.patterns[ui].from(pi);
self.change_pat(ui,pi);
return;
}
}else {
self.patterns[ui].from(pi);
self.change_pat(i,pi);
}
}
}
Expand Down
15 changes: 0 additions & 15 deletions src/time/pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,6 @@ impl Ord for Pattern {
}

impl Pattern {
// pub fn new_empty() -> Self {
// Self {
// name: String::new(),
// date_time:NaiveDateTime::MAX,
// once: false,
// }
// }

pub fn from(&mut self,other:&PatternInfo){
println!("from");
if let PatternInfoType::New(value) = &other.name {
Expand All @@ -113,13 +105,6 @@ impl Pattern {
}
}

pub fn change_once(&mut self, new_val:bool){
if self.mandatory {
return;
}
self.once = new_val;
}

fn cmp_date_time(&self, other: &NaiveDateTime) -> Ordering {
let comp = self.date_time.hour().cmp(&other.hour());
if comp.is_eq() {
Expand Down
3 changes: 2 additions & 1 deletion src/time/schedule.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,9 @@ impl ScheduleData {

if pri.cmib.pdt.as_ref().unwrap() == &PatternDetectionType::All {
println!(
"All {} removed from {:?}!",
"All {} named '{}' removed from {:?}!",
"Patterns".yellow(),
pri.cmib.pattern_name.as_ref().unwrap(),
pri.cmib.valid_daytypes.as_ref().unwrap()
);
}else {
Expand Down
4 changes: 2 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn format_time(time: &str) -> Result<String, ArgError> {
let mut formatted_time: String = String::from(time);

if formatted_time.len() == 1 {
if !formatted_time.chars().nth(0).unwrap().is_ascii_digit(){
if !formatted_time.chars().next().unwrap().is_ascii_digit(){
return Err(ArgError::TimeFormat);
}
let mut full_str:String = "0".to_string();
Expand Down Expand Up @@ -165,7 +165,7 @@ pub fn limit_to(string:String,limit:usize)->String{
string
}

pub fn process_str_num(mut str:&str) -> Option<String> {
pub fn process_str_num(str:&str) -> Option<String> {

let mut processed_str:String = String::new();
for char in str.chars().collect::<Vec<char>>() {
Expand Down

0 comments on commit 417825c

Please sign in to comment.