diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/db.rs | 28 | ||||
-rw-r--r-- | src/error.rs | 9 | ||||
-rw-r--r-- | src/lib.rs | 7 | ||||
-rw-r--r-- | src/main.rs | 3 | ||||
-rw-r--r-- | src/task.rs | 89 | ||||
-rw-r--r-- | src/timestamp.rs | 1 |
6 files changed, 137 insertions, 0 deletions
diff --git a/src/db.rs b/src/db.rs new file mode 100644 index 0000000..dd35e4d --- /dev/null +++ b/src/db.rs @@ -0,0 +1,28 @@ +use sqlx::sqlite::SqlitePool; + +use crate::{task::Task, Result}; + +struct Db { + pool: SqlitePool, +} + +impl Db { + pub async fn create_task(&self, task: Task) -> Result<i64> { + let id = sqlx::query!( + "insert into tasks ( name, cron, archived, description ) values ( ?1, ?2, ?3, ?4 )", + task.name, + task.cron, + task.archived, + task.description + ) + .execute(&self.pool) + .await? + .last_insert_rowid(); + + Ok(id) + } + + pub async fn read_tasks(&self) -> Result<Vec<Task>> { + todo!() + } +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..6716b58 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,9 @@ +pub enum Error { + Database(sqlx::Error), +} + +impl From<sqlx::Error> for Error { + fn from(e: sqlx::Error) -> Self { + Error::Database(e) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..2bcd120 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,7 @@ +mod db; +mod error; +mod task; +mod timestamp; + +pub use error::Error; +pub type Result<T> = std::result::Result<T, Error>; diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e7a11a9 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,3 @@ +fn main() { + println!("Hello, world!"); +} diff --git a/src/task.rs b/src/task.rs new file mode 100644 index 0000000..dcfe776 --- /dev/null +++ b/src/task.rs @@ -0,0 +1,89 @@ +use std::{ + borrow::{Borrow, Cow}, + str::FromStr, +}; + +use chrono::{NaiveDateTime, TimeDelta}; +use sqlx::{ + encode::IsNull, + sqlite::{SqliteArgumentValue, SqliteTypeInfo, SqliteValue}, + Decode, Sqlite, Type, TypeInfo, Value, ValueRef, +}; + +pub struct Task { + pub name: String, + pub cron: Option<Schedule>, + pub archived: bool, + pub description: Option<String>, +} + +impl Task { + pub fn new( + name: String, + cron: Option<Schedule>, + archived: bool, + description: Option<String>, + ) -> Self { + Self { + name, + cron, + archived, + description, + } + } +} + +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() + } +} + +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) + } +} diff --git a/src/timestamp.rs b/src/timestamp.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/timestamp.rs @@ -0,0 +1 @@ + |