Use macros to make the code a little less ugly
This commit is contained in:
parent
25a52fa0e5
commit
cd3d14c7de
66
src/main.rs
66
src/main.rs
@ -1,14 +1,28 @@
|
||||
use crate::toggl::types::{Project, Tag, TimeEntry, TogglReportFilters, TrackingClient};
|
||||
use chrono::{DateTime, DurationRound, NaiveDate, TimeDelta, Utc};
|
||||
use chrono::{DateTime, NaiveDate, TimeDelta, Utc};
|
||||
use itertools::Itertools;
|
||||
use soa_rs::Soa;
|
||||
use sqlx::{Connection, PgConnection, Postgres, QueryBuilder};
|
||||
use sqlx::{Connection, PgConnection};
|
||||
use toggl::TogglApi;
|
||||
use tracing_subscriber::fmt::time;
|
||||
|
||||
mod sensitive;
|
||||
mod toggl;
|
||||
|
||||
macro_rules! cast_slice {
|
||||
($slice: expr, $typ: ty) => {
|
||||
&($slice.iter().map(|id| *id as $typ).collect_vec())[..]
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! cast_slice_opts {
|
||||
($slice: expr, $typ: ty) => {
|
||||
$slice
|
||||
.iter()
|
||||
.map(|opt| opt.map(|id| id as $typ))
|
||||
.collect_vec()
|
||||
};
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
enum AppError {
|
||||
#[error("Database error: {0}")]
|
||||
@ -177,14 +191,14 @@ impl Worker {
|
||||
server_deleted_at = excluded.server_deleted_at,
|
||||
permissions = excluded.permissions
|
||||
"#,
|
||||
&time_entries.id().iter().map(|id| *id as i64).collect_vec()[..],
|
||||
&time_entries.workspace_id().iter().map(|id| *id as i64).collect_vec()[..],
|
||||
&time_entries.user_id().iter().map(|id| *id as i64).collect_vec()[..],
|
||||
time_entries.project_id().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
||||
time_entries.task_id().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
||||
cast_slice!(time_entries.id(), i64),
|
||||
cast_slice!(time_entries.workspace_id(), i64),
|
||||
cast_slice!(time_entries.user_id(), i64),
|
||||
cast_slice_opts!(time_entries.project_id(), i64) as _,
|
||||
cast_slice_opts!(time_entries.task_id(), i64) as _,
|
||||
time_entries.start(),
|
||||
time_entries.stop() as _,
|
||||
time_entries.duration().iter().map(|opt| opt.map(|d| d as i32)).collect_vec() as _,
|
||||
cast_slice_opts!(time_entries.duration(), i32) as _,
|
||||
time_entries.updated_at(),
|
||||
time_entries.description() as _,
|
||||
time_entries.billable(),
|
||||
@ -217,16 +231,8 @@ impl Worker {
|
||||
organization_id = excluded.organization_id,
|
||||
name = excluded.name
|
||||
"#,
|
||||
&workspaces
|
||||
.id()
|
||||
.iter()
|
||||
.map(|id| *id as i64)
|
||||
.collect::<Vec<_>>()[..],
|
||||
&workspaces
|
||||
.organization_id()
|
||||
.iter()
|
||||
.map(|id| *id as i64)
|
||||
.collect::<Vec<_>>()[..],
|
||||
cast_slice!(workspaces.id(), i64),
|
||||
cast_slice!(workspaces.organization_id(), i64),
|
||||
workspaces.name(),
|
||||
)
|
||||
.execute(&mut self.db)
|
||||
@ -270,9 +276,9 @@ impl Worker {
|
||||
can_track_time = excluded.can_track_time,
|
||||
permissions = excluded.permissions
|
||||
"#,
|
||||
&projects.id().iter().map(|id| *id as i64).collect_vec()[..],
|
||||
&projects.workspace_id().iter().map(|id| *id as i64).collect_vec()[..],
|
||||
projects.client_id().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
||||
cast_slice!(projects.id(), i64),
|
||||
cast_slice!(projects.workspace_id(), i64),
|
||||
cast_slice_opts!(projects.client_id(), i64) as _,
|
||||
projects.name(),
|
||||
projects.color(),
|
||||
&projects.status().iter().map(|s| s.to_string()).collect::<Vec<_>>()[..],
|
||||
@ -281,8 +287,8 @@ impl Worker {
|
||||
projects.start_date() as _,
|
||||
projects.created_at(),
|
||||
projects.server_deleted_at() as _,
|
||||
&projects.actual_hours().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
||||
&projects.actual_seconds().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
||||
cast_slice_opts!(projects.actual_hours(), i64) as _,
|
||||
cast_slice_opts!(projects.actual_seconds(), i64) as _,
|
||||
projects.can_track_time(),
|
||||
projects.permissions() as _,
|
||||
)
|
||||
@ -308,10 +314,10 @@ impl Worker {
|
||||
deleted_at = excluded.deleted_at,
|
||||
permissions = excluded.permissions
|
||||
"#,
|
||||
&tags.id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
||||
cast_slice!(tags.id(), i64),
|
||||
tags.name(),
|
||||
&tags.workspace_id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
||||
&tags.creator_id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
||||
cast_slice!(tags.workspace_id(), i64),
|
||||
cast_slice!(tags.creator_id(), i64),
|
||||
tags.updated_at(),
|
||||
// Nullable fields fail to type check with UNNEST batch inserts so we silence the
|
||||
// errors using ` as _`.
|
||||
@ -344,16 +350,16 @@ impl Worker {
|
||||
workspace_id = excluded.workspace_id,
|
||||
permissions = excluded.permissions
|
||||
"#,
|
||||
&clients.id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
||||
cast_slice!(clients.id(), i64),
|
||||
clients.updated_at(),
|
||||
clients.archived(),
|
||||
&clients.creator_id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
||||
cast_slice!(clients.creator_id(), i64),
|
||||
// For the next two, we are assuming these are Option<String> as datatype. If these are different, please update accordingly.
|
||||
clients.integration_provider() as _,
|
||||
clients.notes() as _,
|
||||
clients.name(),
|
||||
clients.server_deleted_at() as _,
|
||||
&clients.workspace_id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
||||
cast_slice!(clients.workspace_id(), i64),
|
||||
clients.permissions() as _
|
||||
)
|
||||
.execute(&mut self.db)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user