diff options
Diffstat (limited to 'rust/actualbudget_models')
| -rw-r--r-- | rust/actualbudget_models/Cargo.toml | 7 | ||||
| -rw-r--r-- | rust/actualbudget_models/src/actualbudget_account.rs | 8 | ||||
| -rw-r--r-- | rust/actualbudget_models/src/actualbudget_category.rs | 8 | ||||
| -rw-r--r-- | rust/actualbudget_models/src/actualbudget_date.rs | 75 | ||||
| -rw-r--r-- | rust/actualbudget_models/src/actualbudget_schema.rs | 47 | ||||
| -rw-r--r-- | rust/actualbudget_models/src/actualbudget_transaction.rs | 18 | ||||
| -rw-r--r-- | rust/actualbudget_models/src/actualbudget_zero_budget.rs | 13 | ||||
| -rw-r--r-- | rust/actualbudget_models/src/lib.rs | 6 |
8 files changed, 182 insertions, 0 deletions
diff --git a/rust/actualbudget_models/Cargo.toml b/rust/actualbudget_models/Cargo.toml new file mode 100644 index 0000000..83d3093 --- /dev/null +++ b/rust/actualbudget_models/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "actualbudget_models" +version = "0.1.0" +edition = "2021" + +[dependencies] +diesel = { workspace = true, features = ["sqlite"] } diff --git a/rust/actualbudget_models/src/actualbudget_account.rs b/rust/actualbudget_models/src/actualbudget_account.rs new file mode 100644 index 0000000..9f058cd --- /dev/null +++ b/rust/actualbudget_models/src/actualbudget_account.rs @@ -0,0 +1,8 @@ +use diesel::prelude::{Queryable, Selectable}; + +#[derive(Queryable, Selectable)] +#[diesel(table_name = crate::actualbudget_schema::accounts)] +pub struct ActualbudgetAccount { + pub id: String, + pub name: String, +} diff --git a/rust/actualbudget_models/src/actualbudget_category.rs b/rust/actualbudget_models/src/actualbudget_category.rs new file mode 100644 index 0000000..8702f19 --- /dev/null +++ b/rust/actualbudget_models/src/actualbudget_category.rs @@ -0,0 +1,8 @@ +use diesel::{prelude::Queryable, Selectable}; + +#[derive(Queryable, Selectable)] +#[diesel(table_name = crate::actualbudget_schema::categories)] +pub struct ActualbudgetCategory { + pub id: String, + pub name: String, +} diff --git a/rust/actualbudget_models/src/actualbudget_date.rs b/rust/actualbudget_models/src/actualbudget_date.rs new file mode 100644 index 0000000..bef160c --- /dev/null +++ b/rust/actualbudget_models/src/actualbudget_date.rs @@ -0,0 +1,75 @@ +use diesel::{backend::Backend, deserialize::{FromSql, FromSqlRow}, expression::AsExpression, sql_types::Integer, sqlite::Sqlite}; + +#[derive(Clone, PartialEq, Eq, Debug, AsExpression, FromSqlRow)] +#[diesel(sql_type=Integer)] +pub struct ActualbudgetDate(i32); + +#[derive(Clone, PartialOrd, PartialEq, Eq)] +enum DatePart { + MonthOfYear, + DayOfMonth, +} + +// actualbudget represents dates as numbers which "look like" the corresponding +// ISO string when written in decimal. E.g. 20240901 represents 1 Sep 2024. It +// also uses month specifiers, e.g. 202409 to represent Sep 2024. In this case, +// we interpret this as the first day of the month, e.g. 2024-09-01. + +impl ActualbudgetDate { + pub fn to_iso(&self) -> String { + format!("{0}-{1}-{2}", self.year(), self.month(), self.day()) + } + + pub fn year(&self) -> String { + self.0.to_string()[0..4].to_string() + } + + pub fn month(&self) -> String { + self.0.to_string()[4..6].to_string() + } + + pub fn day(&self) -> String { + match self.date_part() { + DatePart::DayOfMonth => self.0.to_string()[6..8].to_string(), + DatePart::MonthOfYear => String::from("01"), + } + } + + pub fn from_i32(i: i32) -> Self { + ActualbudgetDate(i) + } + + fn date_part(&self) -> DatePart { + if self.0 < 10000000 { + DatePart::MonthOfYear + } else { + DatePart::DayOfMonth + } + } + + fn dayified_value(&self) -> i32 { + match self.date_part() { + DatePart::DayOfMonth => self.0, + DatePart::MonthOfYear => self.0 * 100 + 1, + } + } +} + +impl Ord for ActualbudgetDate { + fn cmp(&self, other: &Self) -> std::cmp::Ordering { + self.dayified_value().cmp(&other.dayified_value()) + } +} + +impl PartialOrd for ActualbudgetDate { + fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> { + Some(self.cmp(other)) + } +} + +impl FromSql<Integer, Sqlite> for ActualbudgetDate { + fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> diesel::deserialize::Result<Self> { + let int: i32 = FromSql::<Integer, Sqlite>::from_sql(bytes)?; + Ok(ActualbudgetDate(int)) + } +} diff --git a/rust/actualbudget_models/src/actualbudget_schema.rs b/rust/actualbudget_models/src/actualbudget_schema.rs new file mode 100644 index 0000000..a872231 --- /dev/null +++ b/rust/actualbudget_models/src/actualbudget_schema.rs @@ -0,0 +1,47 @@ +diesel::table! { + accounts (id) { + id -> Text, + name -> Text, + } +} + +diesel::table! { + categories (id) { + id -> Text, + name -> Text, + } +} + +diesel::table! { + transactions (id) { + id -> Text, + #[sql_name = "isParent"] + is_parent -> Bool, + #[sql_name = "isChild"] + is_child -> Bool, + #[sql_name = "acct"] + account_id -> Text, + #[sql_name = "category"] + category_id -> Text, + amount -> Integer, + #[sql_name = "description"] + payee -> Text, + notes -> Text, + date -> Integer, + parent_id -> Nullable<Text>, + } +} + +diesel::table! { + zero_budgets (id) { + id -> Text, + month -> Integer, + #[sql_name = "category"] + category_id -> Text, + amount -> Integer, + #[sql_name = "carryover"] + do_carry_over -> Integer, + // goal -> Integer, + // long_goal -> Integer, + } +} diff --git a/rust/actualbudget_models/src/actualbudget_transaction.rs b/rust/actualbudget_models/src/actualbudget_transaction.rs new file mode 100644 index 0000000..2b2f67b --- /dev/null +++ b/rust/actualbudget_models/src/actualbudget_transaction.rs @@ -0,0 +1,18 @@ +use diesel::prelude::{Identifiable, Queryable, QueryableByName, Selectable}; + +use crate::actualbudget_date::ActualbudgetDate; + +#[derive(Identifiable, QueryableByName, Queryable, Selectable, Clone)] +#[diesel(table_name = crate::actualbudget_schema::transactions)] +pub struct ActualbudgetTransaction { + pub id: String, + pub is_parent: bool, + pub is_child: bool, + pub account_id: String, + pub category_id: String, + pub amount: i32, + pub payee: String, + pub notes: String, + pub date: ActualbudgetDate, + pub parent_id: Option<String>, +} diff --git a/rust/actualbudget_models/src/actualbudget_zero_budget.rs b/rust/actualbudget_models/src/actualbudget_zero_budget.rs new file mode 100644 index 0000000..41b3443 --- /dev/null +++ b/rust/actualbudget_models/src/actualbudget_zero_budget.rs @@ -0,0 +1,13 @@ +use crate::actualbudget_date::ActualbudgetDate; +use diesel::{prelude::{Identifiable, Queryable, QueryableByName, Selectable}, sqlite::Sqlite}; + +#[derive(Identifiable, QueryableByName, Queryable, Selectable, Clone)] +#[diesel(table_name = crate::actualbudget_schema::zero_budgets)] +#[diesel(check_for_backend(Sqlite))] +pub struct ActualbudgetZeroBudget { + pub id: String, + pub month: ActualbudgetDate, + pub category_id: String, + pub amount: i32, + pub do_carry_over: i32, +} diff --git a/rust/actualbudget_models/src/lib.rs b/rust/actualbudget_models/src/lib.rs new file mode 100644 index 0000000..2fae261 --- /dev/null +++ b/rust/actualbudget_models/src/lib.rs @@ -0,0 +1,6 @@ +pub mod actualbudget_account; +pub mod actualbudget_category; +pub mod actualbudget_date; +pub mod actualbudget_schema; +pub mod actualbudget_transaction; +pub mod actualbudget_zero_budget; |
