aboutsummaryrefslogblamecommitdiffstats
path: root/src/task.rs
blob: 59aa5020d466c03c1a70faf4db82c7b0a02b7512 (plain) (tree)
1
2
3
4
5
6
7
8
9

                          
                         





                                       
                     



                                                               
                  
                 
                    



                                    
                                    





                               
                                    
                                            

               
                     

                 
                            
                        
                                                                     

         








                                              
































                                                                  
                    






















                                                 
use std::{
    borrow::{Borrow, Cow},
    collections::HashSet,
    str::FromStr,
};

use chrono::{NaiveDateTime, TimeDelta};
use sqlx::{
    encode::IsNull,
    prelude::FromRow,
    sqlite::{SqliteArgumentValue, SqliteTypeInfo, SqliteValue},
    Decode, Sqlite, Type, TypeInfo, Value, ValueRef,
};

#[derive(FromRow)]
pub struct Task {
    id: Option<i64>,
    pub name: String,
    pub cron: Option<Schedule>,
    pub archived: bool,
    pub description: Option<String>,
    pub categories: HashSet<String>,
}

impl Task {
    pub fn new(
        name: String,
        cron: Option<Schedule>,
        description: Option<String>,
        categories: Option<HashSet<String>>,
    ) -> Self {
        Self {
            id: None,
            name,
            cron,
            archived: false,
            description,
            categories: categories.unwrap_or_else(|| HashSet::new()),
        }
    }

    pub fn id(&self) -> Option<i64> {
        self.id
    }

    pub fn add_id(mut self, id: i64) -> Self {
        self.id = Some(id);
        self
    }
}

pub struct Schedule(cron::Schedule);

impl sqlx::Encode<'_, Sqlite> for Schedule {
    fn encode_by_ref(
        &self,
        buf: &mut <Sqlite as sqlx::Database>::ArgumentBuffer<'_>,
    ) -> Result<sqlx::encode::IsNull, sqlx::error::BoxDynError> {
        let schedule = &self.0;
        let schedule = schedule.to_string();
        buf.push(SqliteArgumentValue::Text(Cow::Owned(schedule)));

        Ok(IsNull::No)
    }
}

impl sqlx::Decode<'_, Sqlite> for Schedule {
    fn decode(
        value: <Sqlite as sqlx::Database>::ValueRef<'_>,
    ) -> Result<Self, sqlx::error::BoxDynError> {
        let schedule = Decode::<Sqlite>::decode(value)?;
        let schedule = cron::Schedule::from_str(schedule)?;
        Ok(Self(schedule))
    }
}

impl Type<Sqlite> for Schedule {
    fn type_info() -> <Sqlite as sqlx::Database>::TypeInfo {
        <&str as Type<Sqlite>>::type_info()
    }
}

#[repr(transparent)]
pub struct Category(String);

impl Category {
    pub fn new(category: String) -> Self {
        Self(category)
    }
}

pub struct Log(NaiveDateTime);

impl Log {
    pub fn new(datetime: NaiveDateTime) -> Self {
        Self(datetime)
    }
}

pub struct Reminder(TimeDelta);

impl Reminder {
    pub fn new(time_delta: TimeDelta) -> Self {
        Self(time_delta)
    }
}