Преглед изворни кода

accepting float numbers for rank and implementing Encodable and Decodable traits for f64

Dastan-glitch пре 4 година
родитељ
комит
9821f2db62
6 измењених фајлова са 46 додато и 12 уклоњено
  1. 5 5
      bin/tau-cli/src/main.rs
  2. 2 2
      bin/taud/src/jsonrpc.rs
  3. 2 2
      bin/taud/src/main.rs
  4. 3 3
      bin/taud/src/task_info.rs
  5. 12 0
      src/util/endian.rs
  6. 22 0
      src/util/serial.rs

+ 5 - 5
bin/tau-cli/src/main.rs

@@ -43,7 +43,7 @@ pub enum CliTauSubCommands {
         due: Option<String>,
         /// Project rank
         #[clap(short, long)]
-        rank: Option<u32>,
+        rank: Option<f64>,
     },
     /// 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()),

+ 2 - 2
bin/taud/src/jsonrpc.rs

@@ -33,7 +33,7 @@ pub struct BaseTaskInfo {
     assign: Vec<String>,
     project: Vec<String>,
     due: Option<Timestamp>,
-    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);
         }
 

+ 2 - 2
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()?;
 

+ 3 - 3
bin/taud/src/task_info.rs

@@ -55,7 +55,7 @@ pub struct TaskInfo {
     assign: TaskAssigns,
     project: TaskProjects,
     due: Option<Timestamp>,
-    rank: u32,
+    rank: f64,
     created_at: Timestamp,
     events: TaskEvents,
     comments: TaskComments,
@@ -68,7 +68,7 @@ impl TaskInfo {
         title: &str,
         desc: &str,
         due: Option<Timestamp>,
-        rank: u32,
+        rank: f64,
         settings: &Settings,
     ) -> TaudResult<Self> {
         // 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;
     }
 

+ 12 - 0
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::<f64>(), 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>());
+    f64::from_le_bytes(*slice)
+}
+
 macro_rules! define_chunk_slice_to_int {
     ($name: ident, $type: ty, $converter: ident) => {
         #[inline]

+ 22 - 0
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<i8>;
 
+    /// Read a 64-bit float
+    fn read_f64(&mut self) -> Result<f64>;
+
     /// Read a boolean
     fn read_bool(&mut self) -> Result<bool>;
 
@@ -138,6 +144,7 @@ impl<W: Write> 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<R: Read> 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<u8> {
@@ -310,6 +318,20 @@ impl Decodable for VarInt {
     }
 }
 
+impl Decodable for f64 {
+    #[inline]
+    fn decode<D: io::Read>(mut d: D) -> Result<Self> {
+        ReadExt::read_f64(&mut d)
+    }
+}
+impl Encodable for f64 {
+    #[inline]
+    fn encode<S: WriteExt>(&self, mut s: S) -> Result<usize> {
+        s.write_f64(*self)?;
+        Ok(mem::size_of::<f64>())
+    }
+}
+
 // Booleans
 impl Encodable for bool {
     #[inline]