From 9821f2db62a839312aa71ad1d3dbdba557c45543 Mon Sep 17 00:00:00 2001 From: Dastan-glitch Date: Wed, 30 Mar 2022 00:15:50 -0400 Subject: [PATCH] accepting float numbers for rank and implementing Encodable and Decodable traits for f64 --- bin/tau-cli/src/main.rs | 10 +++++----- bin/taud/src/jsonrpc.rs | 4 ++-- bin/taud/src/main.rs | 4 ++-- bin/taud/src/task_info.rs | 6 +++--- src/util/endian.rs | 12 ++++++++++++ src/util/serial.rs | 22 ++++++++++++++++++++++ 6 files changed, 46 insertions(+), 12 deletions(-) diff --git a/bin/tau-cli/src/main.rs b/bin/tau-cli/src/main.rs index 5569071e6..336565cbc 100644 --- a/bin/tau-cli/src/main.rs +++ b/bin/tau-cli/src/main.rs @@ -43,7 +43,7 @@ pub enum CliTauSubCommands { due: Option, /// Project rank #[clap(short, long)] - rank: Option, + rank: Option, }, /// Update/Edit an existing task by ID Update { @@ -299,7 +299,7 @@ async fn start(options: CliTau) -> Result<()> { None => None, }; - let rank = rank.unwrap_or(0); + let rank = rank.unwrap_or(0.0); add( rpc_addr, @@ -316,9 +316,9 @@ async fn start(options: CliTau) -> Result<()> { table.set_titles(row!["ID", "Title", "Project", "Assigned", "Due", "Rank"]); let mut tasks = rep.as_array().unwrap().to_owned(); - tasks.sort_by(|a, b| b["rank"].as_u64().cmp(&a["rank"].as_u64())); + tasks.sort_by(|a, b| b["rank"].as_f64().partial_cmp(&a["rank"].as_f64()).unwrap()); - let max_rank = if !tasks.is_empty() { tasks[0]["rank"].as_u64().unwrap() } else { 0 }; + let max_rank = if !tasks.is_empty() { tasks[0]["rank"].as_f64().unwrap() } else { 0.0 }; for task in tasks { let project = task["project"].as_array().unwrap(); @@ -346,7 +346,7 @@ async fn start(options: CliTau) -> Result<()> { "".to_string() }; - let rank = task["rank"].as_u64().unwrap_or(0); + let rank = task["rank"].as_f64().unwrap_or(0.0); table.add_row(Row::new(vec![ Cell::new(&task["id"].to_string()), diff --git a/bin/taud/src/jsonrpc.rs b/bin/taud/src/jsonrpc.rs index 9ed985f47..d2256ad99 100644 --- a/bin/taud/src/jsonrpc.rs +++ b/bin/taud/src/jsonrpc.rs @@ -33,7 +33,7 @@ pub struct BaseTaskInfo { assign: Vec, project: Vec, due: Option, - rank: u32, + rank: f64, } #[async_trait] @@ -231,7 +231,7 @@ impl JsonRpcInterface { if data.contains_key("rank") { let rank = data.get("rank").unwrap().clone(); - let rank: u32 = serde_json::from_value(rank)?; + let rank: f64 = serde_json::from_value(rank)?; task.set_rank(rank); } diff --git a/bin/taud/src/main.rs b/bin/taud/src/main.rs index 26c8fa23d..7481cf6aa 100644 --- a/bin/taud/src/main.rs +++ b/bin/taud/src/main.rs @@ -134,7 +134,7 @@ mod tests { // load and save TaskInfo /////////////////////// - let mut task = TaskInfo::new("test_title", "test_desc", None, 0, &settings)?; + let mut task = TaskInfo::new("test_title", "test_desc", None, 0.0, &settings)?; task.save()?; @@ -174,7 +174,7 @@ mod tests { // activate task /////////////////////// - let task = TaskInfo::new("test_title_3", "test_desc", None, 0, &settings)?; + let task = TaskInfo::new("test_title_3", "test_desc", None, 0.0, &settings)?; task.save()?; diff --git a/bin/taud/src/task_info.rs b/bin/taud/src/task_info.rs index a8ca6dce9..248efaf62 100644 --- a/bin/taud/src/task_info.rs +++ b/bin/taud/src/task_info.rs @@ -55,7 +55,7 @@ pub struct TaskInfo { assign: TaskAssigns, project: TaskProjects, due: Option, - rank: u32, + rank: f64, created_at: Timestamp, events: TaskEvents, comments: TaskComments, @@ -68,7 +68,7 @@ impl TaskInfo { title: &str, desc: &str, due: Option, - rank: u32, + rank: f64, settings: &Settings, ) -> TaudResult { // generate ref_id @@ -160,7 +160,7 @@ impl TaskInfo { self.comments.0.push(c); } - pub fn set_rank(&mut self, r: u32) { + pub fn set_rank(&mut self, r: f64) { self.rank = r; } diff --git a/src/util/endian.rs b/src/util/endian.rs index 50aa0bb10..b6c8c43a8 100644 --- a/src/util/endian.rs +++ b/src/util/endian.rs @@ -87,6 +87,18 @@ pub fn i64_to_array_le(val: i64) -> [u8; 8] { u64_to_array_le(val as u64) } +#[inline] +pub fn f64_to_array_le(val: f64) -> [u8; 8] { + assert_eq!(::std::mem::size_of::(), 8); // size_of isn't a constfn in 1.22 + val.to_le_bytes() +} + +#[inline] +pub fn slice_to_f64_le(slice: &[u8; 8]) -> f64 { + assert_eq!(slice.len(), ::std::mem::size_of::()); + f64::from_le_bytes(*slice) +} + macro_rules! define_chunk_slice_to_int { ($name: ident, $type: ty, $converter: ident) => { #[inline] diff --git a/src/util/serial.rs b/src/util/serial.rs index 2d4cbb7e9..b683bfc4d 100644 --- a/src/util/serial.rs +++ b/src/util/serial.rs @@ -73,6 +73,9 @@ pub trait WriteExt { /// Output a 8-bit int fn write_i8(&mut self, v: i8) -> Result<()>; + /// Output a 64-bit float + fn write_f64(&mut self, v: f64) -> Result<()>; + /// Output a boolean fn write_bool(&mut self, v: bool) -> Result<()>; @@ -102,6 +105,9 @@ pub trait ReadExt { /// Read a 8-bit int fn read_i8(&mut self) -> Result; + /// Read a 64-bit float + fn read_f64(&mut self) -> Result; + /// Read a boolean fn read_bool(&mut self) -> Result; @@ -138,6 +144,7 @@ impl WriteExt for W { encoder_fn!(write_i64, i64, i64_to_array_le); encoder_fn!(write_i32, i32, i32_to_array_le); encoder_fn!(write_i16, i16, i16_to_array_le); + encoder_fn!(write_f64, f64, f64_to_array_le); #[inline] fn write_i8(&mut self, v: i8) -> Result<()> { @@ -165,6 +172,7 @@ impl ReadExt for R { decoder_fn!(read_i64, i64, slice_to_i64_le, 8); decoder_fn!(read_i32, i32, slice_to_i32_le, 4); decoder_fn!(read_i16, i16, slice_to_i16_le, 2); + decoder_fn!(read_f64, f64, slice_to_f64_le, 8); #[inline] fn read_u8(&mut self) -> Result { @@ -310,6 +318,20 @@ impl Decodable for VarInt { } } +impl Decodable for f64 { + #[inline] + fn decode(mut d: D) -> Result { + ReadExt::read_f64(&mut d) + } +} +impl Encodable for f64 { + #[inline] + fn encode(&self, mut s: S) -> Result { + s.write_f64(*self)?; + Ok(mem::size_of::()) + } +} + // Booleans impl Encodable for bool { #[inline]