Initial commit

This commit is contained in:
2021-12-23 10:10:59 +00:00
commit 021d067d86
11 changed files with 2320 additions and 0 deletions

82
src/lib.rs Normal file
View File

@@ -0,0 +1,82 @@
use actix_web::{dev::Server, middleware::Logger, web, App, HttpResponse, HttpServer};
use chrono::{DateTime, Utc};
use listenfd::ListenFd;
use serde::{Deserialize, Serialize};
use std::io::Error;
use std::net::TcpListener;
use uuid::Uuid;
pub mod taskwarrior;
#[derive(Deserialize)]
struct TaskQuery {
filter: String,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Task {
pub uuid: Uuid,
pub id: u32,
#[serde(with = "taskwarrior::tw_date_format")]
pub entry: DateTime<Utc>,
#[serde(with = "taskwarrior::tw_date_format")]
pub modified: DateTime<Utc>,
pub status: taskwarrior::Status,
pub description: String,
pub urgency: f64,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "taskwarrior::opt_tw_date_format"
)]
pub due: Option<DateTime<Utc>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "taskwarrior::opt_tw_date_format"
)]
pub end: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub recur: Option<taskwarrior::Recurrence>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub contextswitch: Option<taskwarrior::ContextSwitchMetadata>,
}
async fn list_tasks(task_query: web::Query<TaskQuery>) -> Result<HttpResponse, Error> {
let tasks = taskwarrior::export(task_query.filter.split(' ').collect())?;
Ok(HttpResponse::Ok()
.content_type("application/json")
.body(serde_json::to_string(&tasks)?))
}
async fn health_check() -> HttpResponse {
HttpResponse::Ok().finish()
}
pub fn run(listener: TcpListener) -> Result<Server, std::io::Error> {
let mut server = HttpServer::new(|| {
App::new()
.wrap(Logger::default())
.route("/ping", web::get().to(health_check))
.route("/tasks", web::get().to(list_tasks))
})
.keep_alive(60)
.shutdown_timeout(60);
let mut listenfd = ListenFd::from_env();
server = if let Some(fdlistener) = listenfd.take_tcp_listener(0)? {
server.listen(fdlistener)?
} else {
server.listen(listener)?
};
Ok(server.run())
}

22
src/main.rs Normal file
View File

@@ -0,0 +1,22 @@
extern crate dotenv;
extern crate env_logger;
extern crate listenfd;
use contextswitch::run;
use dotenv::dotenv;
use std::env;
use std::net::TcpListener;
pub mod taskwarrior;
#[actix_web::main]
async fn main() -> std::io::Result<()> {
env::set_var("RUST_LOG", "actix_web=info");
env_logger::init();
dotenv().ok();
let port = env::var("PORT").unwrap_or("8000".to_string());
let listener = TcpListener::bind(format!("0.0.0.0:{}", port)).expect("Failed to bind port");
run(listener)?.await
}

146
src/taskwarrior.rs Normal file
View File

@@ -0,0 +1,146 @@
use chrono::{DateTime, Utc};
use serde::{de, Deserialize, Deserializer, Serialize};
use serde_json;
use std::io::Error;
use std::process::Command;
use std::str;
use uuid::Uuid;
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Recurrence {
Daily,
Weekly,
Monthly,
Yearly,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Status {
Pending,
Completed,
Recurring,
Deleted,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct ContextSwitchMetadata {
pub test: u32,
}
#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct Task {
pub uuid: Uuid,
pub id: u32,
#[serde(with = "tw_date_format")]
pub entry: DateTime<Utc>,
#[serde(with = "tw_date_format")]
pub modified: DateTime<Utc>,
pub status: Status,
pub description: String,
pub urgency: f64,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "opt_tw_date_format"
)]
pub due: Option<DateTime<Utc>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
with = "opt_tw_date_format"
)]
pub end: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub parent: Option<Uuid>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub recur: Option<Recurrence>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
#[serde(
default,
skip_serializing_if = "Option::is_none",
deserialize_with = "deserialize_from_json"
)]
pub contextswitch: Option<ContextSwitchMetadata>,
}
fn deserialize_from_json<'de, D>(deserializer: D) -> Result<Option<ContextSwitchMetadata>, D::Error>
where
D: Deserializer<'de>,
{
let s: String = Deserialize::deserialize(deserializer)?;
serde_json::from_str(&s).map_err(de::Error::custom)
}
pub mod tw_date_format {
use chrono::{DateTime, TimeZone, Utc};
use serde::{self, Deserialize, Deserializer, Serializer};
const FORMAT: &'static str = "%Y%m%dT%H%M%SZ";
pub fn serialize<S>(date: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let s = format!("{}", date.format(FORMAT));
serializer.serialize_str(&s)
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Utc.datetime_from_str(&s, FORMAT)
.map_err(serde::de::Error::custom)
}
}
pub mod opt_tw_date_format {
use chrono::{DateTime, TimeZone, Utc};
use serde::{self, Deserialize, Deserializer, Serializer};
const FORMAT: &'static str = "%Y%m%dT%H%M%SZ";
pub fn serialize<S>(date: &Option<DateTime<Utc>>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if let Some(ref d) = *date {
return serializer.serialize_str(&d.format(FORMAT).to_string());
}
serializer.serialize_none()
}
pub fn deserialize<'de, D>(deserializer: D) -> Result<Option<DateTime<Utc>>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
Utc.datetime_from_str(&s, FORMAT)
.map(Some)
.map_err(serde::de::Error::custom)
}
}
pub fn export(filters: Vec<&str>) -> Result<Vec<Task>, Error> {
let mut args = vec!["export"];
args.extend(filters);
let export_output = Command::new("task").args(args).output()?;
let tasks: Vec<Task> = serde_json::from_slice(&export_output.stdout)?;
return Ok(tasks);
}
pub fn add(add_args: Vec<&str>) -> Result<(), Error> {
let mut args = vec!["add"];
args.extend(add_args);
Command::new("task").args(args).output()?;
return Ok(());
}