Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow editing time entries #86

Draft
wants to merge 13 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
🌳 Implement project/task deserialization
shantanuraj committed Dec 11, 2024
commit 0be7c46132ce65ccd2334f05634365a023c31d77
1 change: 1 addition & 0 deletions src/constants.rs
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ pub const CONFIG_UNRECOGNIZED_MACRO_ERROR: &str = "Unrecognized macro in config
pub const CONFIG_SHELL_MACRO_RESOLUTION_ERROR: &str = "Failed to resolve shell macro";
pub const CONFIG_INVALID_WORKSPACE_ERROR: &str = "Workspace not found";
pub const NO_PROJECT: &str = "No Project";
pub const NO_TASK: &str = "No Task";
pub const NO_DESCRIPTION: &str = "(no description)";
pub const DIRECTORY_NOT_FOUND_ERROR: &str = "Directory not found";
pub const NOT_A_DIRECTORY_ERROR: &str = "Not a directory";
52 changes: 46 additions & 6 deletions src/models.rs
Original file line number Diff line number Diff line change
@@ -170,6 +170,23 @@ impl std::fmt::Display for Project {
}
}

impl Default for Project {
fn default() -> Self {
Self {
id: -1,
name: constants::NO_PROJECT.to_string(),
workspace_id: -1,
client: None,
is_private: false,
active: true,
at: Utc::now(),
created_at: Utc::now(),
color: "0".to_string(),
billable: None,
}
}
}

#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Client {
pub id: i64,
@@ -185,6 +202,17 @@ pub struct Task {
pub project: Project,
}

impl Default for Task {
fn default() -> Self {
Self {
id: -1,
name: constants::NO_TASK.to_string(),
workspace_id: -1,
project: Project::default(),
}
}
}

impl TimeEntry {
pub fn get_description(&self) -> String {
match self.description.as_ref() {
@@ -338,8 +366,6 @@ impl Parcel for TimeEntry {

fn deserialize(&self, data: Vec<u8>) -> Self {
let mut time_entry = self.clone();
let project: Option<Project> = None;
let task: Option<Task> = None;

let data = String::from_utf8(data).unwrap();

@@ -357,14 +383,28 @@ impl Parcel for TimeEntry {
"Billable" => time_entry.billable = value.parse().unwrap(),
"Tags" => time_entry.tags = value.split(", ").map(String::from).collect(),
"Project" => {
if project.is_some() {
time_entry.project = project.clone();
let project_parts: Vec<&str> = value.split(" -- ").collect();
if project_parts.len() < 2 {
continue;
}
time_entry.project = Some(Project {
id: project_parts[1].parse().unwrap(),
name: project_parts[0].to_string(),
workspace_id: time_entry.workspace_id,
..Project::default()
});
}
"Task" => {
if task.is_some() {
time_entry.task = task.clone();
let task_parts: Vec<&str> = value.split(" -- ").collect();
if task_parts.len() < 2 {
continue;
}
time_entry.task = Some(Task {
id: task_parts[1].parse().unwrap(),
name: task_parts[0].to_string(),
workspace_id: time_entry.workspace_id,
project: time_entry.project.clone().unwrap_or(Project::default()),
});
}
"Description" => time_entry.description = value.to_string(),
_ => {}
4 changes: 2 additions & 2 deletions src/parcel.rs
Original file line number Diff line number Diff line change
@@ -25,8 +25,8 @@ pub trait Parcel {
utilities::open_path_in_editor(&file_path).expect("Failed to open file in editor");
drop(file);

let contents = fs::read(file_path)
.expect("Failed to read file time-entry editing in editor");
let contents =
fs::read(file_path).expect("Failed to read file time-entry editing in editor");

dir.close().expect("Failed to clear temp directory");