Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/command/handler/uds_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub async fn handle(matches: ArgMatches, socket: UnixDatagram) -> HandleUdsResul

async fn handle_create(socket: UnixDatagram, sub_matches: &ArgMatches) -> HandleUdsResult {
let (work_time, break_time) =
util::parse_work_and_break_time(sub_matches).map_err(UdsHandlerError::ParseError)?;
util::parse_work_and_break_time(sub_matches, None).map_err(UdsHandlerError::ParseError)?;

socket
.send(
Expand All @@ -62,7 +62,7 @@ async fn handle_create(socket: UnixDatagram, sub_matches: &ArgMatches) -> Handle

async fn handle_queue(socket: UnixDatagram, sub_matches: &ArgMatches) -> HandleUdsResult {
let (work_time, break_time) =
util::parse_work_and_break_time(sub_matches).map_err(UdsHandlerError::ParseError)?;
util::parse_work_and_break_time(sub_matches, None).map_err(UdsHandlerError::ParseError)?;

debug!("handle_queue");
socket
Expand Down
48 changes: 38 additions & 10 deletions src/command/util.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
use clap::ArgMatches;
use clap_complete::Shell;
use std::io::{self, Write};
use std::str::FromStr;
use std::sync::Arc;

use crate::command::{DEFAULT_BREAK_TIME, DEFAULT_WORK_TIME};
use crate::configuration::Configuration;
use crate::error::ParseError;
use clap::ArgMatches;
use clap_complete::Shell;

pub fn parse_work_and_break_time(matches: &ArgMatches) -> Result<(u16, u16), ParseError> {
let (work_time, break_time) = if matches.get_flag("default") {
(DEFAULT_WORK_TIME, DEFAULT_BREAK_TIME)
pub fn parse_work_and_break_time(
matches: &ArgMatches,
configuration: Option<&Arc<Configuration>>,
) -> Result<(Option<u16>, Option<u16>), ParseError> {
if let Some(conf) = configuration {
let mut work_time = match conf.get_work_time() {
Some(work_time) => work_time,
None => DEFAULT_WORK_TIME,
};

let mut break_time = match conf.get_break_time() {
Some(break_time) => break_time,
None => DEFAULT_BREAK_TIME,
};

if let Ok(val) = parse_arg::<u16>(matches, "work") {
work_time = val;
};

if let Ok(val) = parse_arg::<u16>(matches, "break") {
break_time = val;
};

Ok((Some(work_time), Some(break_time)))
} else {
let work_time = parse_arg::<u16>(matches, "work")?;
let break_time = parse_arg::<u16>(matches, "break")?;
let mut work_time = None;
let mut break_time = None;

(work_time, break_time)
};
if let Ok(val) = parse_arg::<u16>(matches, "work") {
work_time = Some(val);
}

Ok((work_time, break_time))
if let Ok(val) = parse_arg::<u16>(matches, "break") {
break_time = Some(val);
}
Ok((work_time, break_time))
}
}

pub fn parse_shell(matches: &ArgMatches) -> Option<Shell> {
Expand Down
57 changes: 40 additions & 17 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,25 @@ pub trait Bincodec {

#[derive(bincode::Encode, bincode::Decode, PartialEq, Debug, Eq)]
pub enum MessageRequest {
Create { work: u16, r#break: u16 },
Queue { work: u16, r#break: u16 },
Delete { id: u16, all: bool },
List { show_percentage: bool },
Create {
work: Option<u16>,
r#break: Option<u16>,
},
Queue {
work: Option<u16>,
r#break: Option<u16>,
},
Delete {
id: u16,
all: bool,
},
List {
show_percentage: bool,
},
Test,
History { should_clear: bool },
History {
should_clear: bool,
},
}

impl Bincodec for MessageRequest {
Expand All @@ -75,20 +88,30 @@ impl From<MessageRequest> for UserInput {
fn from(request: MessageRequest) -> Self {
let input = match request {
MessageRequest::Create { work, r#break } => {
format!(
"{} -w {} -b {}",
String::from(ActionType::Create),
work,
r#break
)
let mut data = format!("{} ", String::from(ActionType::Create));

if let Some(val) = work {
data.push_str(&format!("-w {} ", val))
}

if let Some(val) = r#break {
data.push_str(&format!("-b {}", val))
}

data
}
MessageRequest::Queue { work, r#break } => {
format!(
"{} -w {} -b {}",
String::from(ActionType::Queue),
work,
r#break
)
let mut data = format!("{} ", String::from(ActionType::Queue));

if let Some(val) = work {
data.push_str(&format!("-w {} ", val))
}

if let Some(val) = r#break {
data.push_str(&format!("-b {}", val))
}

data
}
MessageRequest::Delete { id, all } => {
if all {
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ async fn main() -> Result<(), Box<dyn Error>> {
CommandType::UdsClient(matches) => {
debug!("CommandType::UdsClient");
let socket = create_client_uds().await?;

handler::uds_client::handle(matches, socket).await?;
}
CommandType::AutoComplete(sub_matches) => {
Expand Down
29 changes: 7 additions & 22 deletions src/notification/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
pub(crate) mod archived_notification;
pub(crate) mod notify;

use std::sync::Arc;

pub use archived_notification::*;
pub use notify::*;

Expand All @@ -11,10 +9,10 @@ use clap::ArgMatches;
use gluesql::core::data::Value;
use gluesql::prelude::Row;
use std::borrow::Cow;
use std::sync::Arc;
use tabled::Tabled;

use crate::command::util::parse_arg;
use crate::command::{DEFAULT_BREAK_TIME, DEFAULT_WORK_TIME};
use crate::command::util;
use crate::configuration::Configuration;
use crate::db;
use crate::error::NotificationError;
Expand Down Expand Up @@ -261,25 +259,12 @@ pub fn get_new_notification(
created_at: DateTime<Utc>,
configuration: Arc<Configuration>,
) -> Result<Notification, NotificationError> {
let mut work_time = match configuration.get_work_time() {
Some(work_time) => work_time,
None => DEFAULT_WORK_TIME,
};

let mut break_time = match configuration.get_break_time() {
Some(break_time) => break_time,
None => DEFAULT_BREAK_TIME,
};

if matches.get_one::<String>("work").is_some() {
work_time =
parse_arg::<u16>(matches, "work").map_err(NotificationError::NewNotification)?;
}
let (work_time, break_time) = util::parse_work_and_break_time(matches, Some(&configuration))
.map_err(NotificationError::NewNotification)?;

if matches.get_one::<String>("break").is_some() {
break_time =
parse_arg::<u16>(matches, "break").map_err(NotificationError::NewNotification)?;
}
// should never panic on unwrap as parse_work_and_break_time already handles it
let work_time = work_time.unwrap();
let break_time = break_time.unwrap();

debug!("work_time: {}", work_time);
debug!("break_time: {}", break_time);
Expand Down