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 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 itertools::Itertools;
|
||||||
use soa_rs::Soa;
|
use soa_rs::Soa;
|
||||||
use sqlx::{Connection, PgConnection, Postgres, QueryBuilder};
|
use sqlx::{Connection, PgConnection};
|
||||||
use toggl::TogglApi;
|
use toggl::TogglApi;
|
||||||
use tracing_subscriber::fmt::time;
|
|
||||||
|
|
||||||
mod sensitive;
|
mod sensitive;
|
||||||
mod toggl;
|
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)]
|
#[derive(Debug, thiserror::Error)]
|
||||||
enum AppError {
|
enum AppError {
|
||||||
#[error("Database error: {0}")]
|
#[error("Database error: {0}")]
|
||||||
@ -177,14 +191,14 @@ impl Worker {
|
|||||||
server_deleted_at = excluded.server_deleted_at,
|
server_deleted_at = excluded.server_deleted_at,
|
||||||
permissions = excluded.permissions
|
permissions = excluded.permissions
|
||||||
"#,
|
"#,
|
||||||
&time_entries.id().iter().map(|id| *id as i64).collect_vec()[..],
|
cast_slice!(time_entries.id(), i64),
|
||||||
&time_entries.workspace_id().iter().map(|id| *id as i64).collect_vec()[..],
|
cast_slice!(time_entries.workspace_id(), i64),
|
||||||
&time_entries.user_id().iter().map(|id| *id as i64).collect_vec()[..],
|
cast_slice!(time_entries.user_id(), i64),
|
||||||
time_entries.project_id().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
cast_slice_opts!(time_entries.project_id(), i64) as _,
|
||||||
time_entries.task_id().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
cast_slice_opts!(time_entries.task_id(), i64) as _,
|
||||||
time_entries.start(),
|
time_entries.start(),
|
||||||
time_entries.stop() as _,
|
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.updated_at(),
|
||||||
time_entries.description() as _,
|
time_entries.description() as _,
|
||||||
time_entries.billable(),
|
time_entries.billable(),
|
||||||
@ -217,16 +231,8 @@ impl Worker {
|
|||||||
organization_id = excluded.organization_id,
|
organization_id = excluded.organization_id,
|
||||||
name = excluded.name
|
name = excluded.name
|
||||||
"#,
|
"#,
|
||||||
&workspaces
|
cast_slice!(workspaces.id(), i64),
|
||||||
.id()
|
cast_slice!(workspaces.organization_id(), i64),
|
||||||
.iter()
|
|
||||||
.map(|id| *id as i64)
|
|
||||||
.collect::<Vec<_>>()[..],
|
|
||||||
&workspaces
|
|
||||||
.organization_id()
|
|
||||||
.iter()
|
|
||||||
.map(|id| *id as i64)
|
|
||||||
.collect::<Vec<_>>()[..],
|
|
||||||
workspaces.name(),
|
workspaces.name(),
|
||||||
)
|
)
|
||||||
.execute(&mut self.db)
|
.execute(&mut self.db)
|
||||||
@ -270,9 +276,9 @@ impl Worker {
|
|||||||
can_track_time = excluded.can_track_time,
|
can_track_time = excluded.can_track_time,
|
||||||
permissions = excluded.permissions
|
permissions = excluded.permissions
|
||||||
"#,
|
"#,
|
||||||
&projects.id().iter().map(|id| *id as i64).collect_vec()[..],
|
cast_slice!(projects.id(), i64),
|
||||||
&projects.workspace_id().iter().map(|id| *id as i64).collect_vec()[..],
|
cast_slice!(projects.workspace_id(), i64),
|
||||||
projects.client_id().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
cast_slice_opts!(projects.client_id(), i64) as _,
|
||||||
projects.name(),
|
projects.name(),
|
||||||
projects.color(),
|
projects.color(),
|
||||||
&projects.status().iter().map(|s| s.to_string()).collect::<Vec<_>>()[..],
|
&projects.status().iter().map(|s| s.to_string()).collect::<Vec<_>>()[..],
|
||||||
@ -281,8 +287,8 @@ impl Worker {
|
|||||||
projects.start_date() as _,
|
projects.start_date() as _,
|
||||||
projects.created_at(),
|
projects.created_at(),
|
||||||
projects.server_deleted_at() as _,
|
projects.server_deleted_at() as _,
|
||||||
&projects.actual_hours().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
cast_slice_opts!(projects.actual_hours(), i64) as _,
|
||||||
&projects.actual_seconds().iter().map(|opt| opt.map(|id| id as i64)).collect_vec() as _,
|
cast_slice_opts!(projects.actual_seconds(), i64) as _,
|
||||||
projects.can_track_time(),
|
projects.can_track_time(),
|
||||||
projects.permissions() as _,
|
projects.permissions() as _,
|
||||||
)
|
)
|
||||||
@ -308,10 +314,10 @@ impl Worker {
|
|||||||
deleted_at = excluded.deleted_at,
|
deleted_at = excluded.deleted_at,
|
||||||
permissions = excluded.permissions
|
permissions = excluded.permissions
|
||||||
"#,
|
"#,
|
||||||
&tags.id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
cast_slice!(tags.id(), i64),
|
||||||
tags.name(),
|
tags.name(),
|
||||||
&tags.workspace_id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
cast_slice!(tags.workspace_id(), i64),
|
||||||
&tags.creator_id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
cast_slice!(tags.creator_id(), i64),
|
||||||
tags.updated_at(),
|
tags.updated_at(),
|
||||||
// Nullable fields fail to type check with UNNEST batch inserts so we silence the
|
// Nullable fields fail to type check with UNNEST batch inserts so we silence the
|
||||||
// errors using ` as _`.
|
// errors using ` as _`.
|
||||||
@ -344,16 +350,16 @@ impl Worker {
|
|||||||
workspace_id = excluded.workspace_id,
|
workspace_id = excluded.workspace_id,
|
||||||
permissions = excluded.permissions
|
permissions = excluded.permissions
|
||||||
"#,
|
"#,
|
||||||
&clients.id().iter().map(|id| *id as i64).collect::<Vec<_>>()[..],
|
cast_slice!(clients.id(), i64),
|
||||||
clients.updated_at(),
|
clients.updated_at(),
|
||||||
clients.archived(),
|
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.
|
// 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.integration_provider() as _,
|
||||||
clients.notes() as _,
|
clients.notes() as _,
|
||||||
clients.name(),
|
clients.name(),
|
||||||
clients.server_deleted_at() as _,
|
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 _
|
clients.permissions() as _
|
||||||
)
|
)
|
||||||
.execute(&mut self.db)
|
.execute(&mut self.db)
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user