Explorar o código

fud/resource: add file selections to json

epiphany hai 7 meses
pai
achega
f386645ce4
Modificáronse 2 ficheiros con 57 adicións e 6 borrados
  1. 6 2
      bin/fud/fud/src/resource.rs
  2. 51 4
      bin/fud/fud/src/util.rs

+ 6 - 2
bin/fud/fud/src/resource.rs

@@ -309,6 +309,8 @@ impl From<Resource> for JsonValue {
                 }),
                 }),
             ),
             ),
             ("status", JsonValue::String(rs.status.as_str().to_string())),
             ("status", JsonValue::String(rs.status.as_str().to_string())),
+            ("file_selection", rs.file_selection.into()),
+            ("last_file_selection", rs.last_file_selection.into()),
             ("total_chunks_count", JsonValue::Number(rs.total_chunks_count as f64)),
             ("total_chunks_count", JsonValue::Number(rs.total_chunks_count as f64)),
             ("target_chunks_count", JsonValue::Number(rs.target_chunks_count as f64)),
             ("target_chunks_count", JsonValue::Number(rs.target_chunks_count as f64)),
             ("total_chunks_downloaded", JsonValue::Number(rs.total_chunks_downloaded as f64)),
             ("total_chunks_downloaded", JsonValue::Number(rs.total_chunks_downloaded as f64)),
@@ -333,6 +335,8 @@ impl From<JsonValue> for Resource {
         let rtype = ResourceType::from_str(value["type"].get::<String>().unwrap()).unwrap();
         let rtype = ResourceType::from_str(value["type"].get::<String>().unwrap()).unwrap();
         let path = PathBuf::from(value["path"].get::<String>().unwrap());
         let path = PathBuf::from(value["path"].get::<String>().unwrap());
         let status = ResourceStatus::from_str(value["status"].get::<String>().unwrap()).unwrap();
         let status = ResourceStatus::from_str(value["status"].get::<String>().unwrap()).unwrap();
+        let file_selection: FileSelection = value["file_selection"].clone().into();
+        let last_file_selection: FileSelection = value["last_file_selection"].clone().into();
 
 
         let total_chunks_count = *value["total_chunks_count"].get::<f64>().unwrap() as u64;
         let total_chunks_count = *value["total_chunks_count"].get::<f64>().unwrap() as u64;
         let target_chunks_count = *value["target_chunks_count"].get::<f64>().unwrap() as u64;
         let target_chunks_count = *value["target_chunks_count"].get::<f64>().unwrap() as u64;
@@ -358,8 +362,8 @@ impl From<JsonValue> for Resource {
             rtype,
             rtype,
             path,
             path,
             status,
             status,
-            file_selection: FileSelection::All, // TODO
-            last_file_selection: FileSelection::All, // TODO
+            file_selection,
+            last_file_selection,
             total_chunks_count,
             total_chunks_count,
             target_chunks_count,
             target_chunks_count,
             total_chunks_downloaded,
             total_chunks_downloaded,

+ 51 - 4
bin/fud/fud/src/util.rs

@@ -16,10 +16,6 @@
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
  */
  */
 
 
-use smol::{
-    fs::{self, File},
-    stream::StreamExt,
-};
 use std::{
 use std::{
     collections::HashSet,
     collections::HashSet,
     path::{Path, PathBuf},
     path::{Path, PathBuf},
@@ -27,9 +23,16 @@ use std::{
     time::Instant,
     time::Instant,
 };
 };
 
 
+use smol::{
+    fs::{self, File},
+    stream::StreamExt,
+};
+use tinyjson::JsonValue;
+
 pub use darkfi::geode::hash_to_string;
 pub use darkfi::geode::hash_to_string;
 use darkfi::{
 use darkfi::{
     net::{Message, MessageSubscription},
     net::{Message, MessageSubscription},
+    rpc::util::json_map,
     Error, Result,
     Error, Result,
 };
 };
 
 
@@ -128,6 +131,50 @@ impl FromIterator<PathBuf> for FileSelection {
     }
     }
 }
 }
 
 
+impl From<FileSelection> for JsonValue {
+    fn from(fs: FileSelection) -> JsonValue {
+        json_map([
+            (
+                "type",
+                JsonValue::String(
+                    match fs {
+                        FileSelection::All => "all",
+                        FileSelection::Set(_) => "set",
+                    }
+                    .to_string(),
+                ),
+            ),
+            (
+                "set",
+                JsonValue::Array(match fs {
+                    FileSelection::All => vec![],
+                    FileSelection::Set(set) => set
+                        .into_iter()
+                        .map(|path| JsonValue::String(path.to_string_lossy().to_string()))
+                        .collect(),
+                }),
+            ),
+        ])
+    }
+}
+
+impl From<JsonValue> for FileSelection {
+    fn from(value: JsonValue) -> Self {
+        match value["type"].get::<String>().unwrap().as_str() {
+            "set" => {
+                let set = value["set"]
+                    .get::<Vec<JsonValue>>()
+                    .unwrap()
+                    .iter()
+                    .map(|path| PathBuf::from(path.get::<String>().unwrap()))
+                    .collect::<HashSet<PathBuf>>();
+                FileSelection::Set(set)
+            }
+            _ => FileSelection::All,
+        }
+    }
+}
+
 /// Wait for a [`crate::proto::ResourceMessage`] on `msg_subscriber` with a timeout.
 /// Wait for a [`crate::proto::ResourceMessage`] on `msg_subscriber` with a timeout.
 /// If we receive a message with the wrong resource hash, it's skipped.
 /// If we receive a message with the wrong resource hash, it's skipped.
 pub async fn receive_resource_msg<M: Message + ResourceMessage + std::fmt::Debug>(
 pub async fn receive_resource_msg<M: Message + ResourceMessage + std::fmt::Debug>(