From 3b69ca58cdc7a16b2fdd537f0e3914310ca90463 Mon Sep 17 00:00:00 2001 From: Adam Cooper Date: Thu, 27 Jan 2022 08:46:55 -0500 Subject: [PATCH] [wip] Commit before appeasing the borrow checker To appease the borrow checker, we'll move the whole struct from main() into tree_traversal(). --- src/main.rs | 90 +++++++++++++++++++++++++++++------------------------ 1 file changed, 50 insertions(+), 40 deletions(-) diff --git a/src/main.rs b/src/main.rs index 090f724..bce591e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,11 +3,12 @@ use env_logger::{Env, Target}; #[allow(unused)] use log::{debug, error, info, log_enabled, warn}; use reqwest; -#[allow(unused)] -use std::io::{self, BufReader, Write}; use std::collections::HashMap; #[allow(unused)] +use std::io::{self, BufReader, Write}; +#[allow(unused)] use yaserde_derive::{YaDeserialize, YaSerialize}; +use yaserde::de::from_str; #[allow(unused)] fn publicise() {} @@ -19,7 +20,7 @@ fn publicise() {} namespace = "d: DAV:", namespace = "nc: http://nextcloud.org/ns", namespace = "oc: http://owncloud.org/ns", - namespace = "s: http://sabredav.org/ns", + namespace = "s: http://sabredav.org/ns" )] pub struct Multistatus { #[yaserde(prefix = "d")] @@ -30,7 +31,8 @@ pub struct Multistatus { #[yaserde(rename = "response", prefix = "d", namespace = "d: DAV:")] pub struct NextcloudResponse { #[yaserde(prefix = "d")] - href: Option, + // href: Option, + href: String, #[yaserde(prefix = "d")] propstat: Vec, } @@ -45,7 +47,11 @@ pub struct Propstat { } #[derive(Default, PartialEq, Debug, YaDeserialize)] -#[yaserde(rename = "prop", namespace = "d: DAV:", namespace = "oc: http://owncloud.org/ns")] +#[yaserde( + rename = "prop", + namespace = "d: DAV:", + namespace = "oc: http://owncloud.org/ns" +)] pub struct Prop { #[yaserde(prefix = "d", rename = "getlastmodified")] get_last_modified: Option, @@ -84,7 +90,7 @@ pub struct Collection { collection: Option, } -async fn get_folder_contents(url_tail: &str) -> Result> { +async fn get_folder_contents(url_tail: &String) -> Result> { debug!("Entering: get_folder_contents()"); let password = get_password().unwrap().trim().to_string(); debug!("Received password: {}[END]", password); @@ -120,21 +126,24 @@ async fn get_folder_contents(url_tail: &str) -> Result) { +async fn traverse(mut queue: Vec) { debug!("Entering: traverse()"); // Initialize the indexed "pointer" let mut current_index: usize = 0; let mut already_visited: bool; // Initialize the hashmap of visited items (by etag?) let mut visited_items = HashMap::new(); - match &queue[0].propstat[0].prop { - Some(prop) => { - match &prop.get_etag { - Some(etag) => { visited_items.insert(etag, true); () }, - _ => current_index += 1 + // match queue[0].propstat[0].prop { + match queue.into_iter().nth(0).unwrap() + .propstat.into_iter().nth(0).unwrap().prop { + Some(prop) => match &prop.get_etag { + Some(etag) => { + visited_items.insert(etag, true); + () } + _ => current_index += 1, }, - _ => current_index += 1 + _ => current_index += 1, } // Depth first traversal while !queue.is_empty() { @@ -142,38 +151,39 @@ async fn traverse(queue: Vec) { Some(prop) => { match &prop.get_etag { Some(etag) => { - // If queue[current_index] has not been visited + // If queue[current_index] has not been visited if !visited_items.contains_key(etag) { - // If item is a collection - // TODO(amcooper): Again, replace unwrap with the match implementation, - // or find another way to handle these structures. - match &prop.resource_type { - Some(resource_type) => { - if !resource_type.collection.is_empty() { - // Get the contents XML - /* TODO(amcooper): Fix argument - let folder_contents: String = - get_folder_contents(&queue[current_index].href.unwrap()) + // If item is a collection + // TODO(amcooper): Again, replace unwrap with the match implementation, + // or find another way to handle these structures. + match &prop.resource_type { + Some(resource_type) => { + if !resource_type.collection.is_empty() { + // Get the contents XML + let folder_contents: String = get_folder_contents( + &queue[current_index].href, + ) .await .unwrap(); - debug!("{:?}", folder_contents); - */ - // Parse the contents XML into Multistatus - // Append the NextcloudResponse vector to queue - } - }, - _ => current_index += 1 + debug!("{:?}", folder_contents); + // Parse the contents XML into Multistatus + let mut result: Multistatus = from_str(&String::from(folder_contents)).unwrap(); + // Append the NextcloudResponse vector to queue + queue.append(&mut result.response); + } + } + _ => current_index += 1, } } - // else - // if item is not public, publicise it. - // Add item to visited items hashmap - // Increment current_index by 1 + // else + // if item is not public, publicise it. + // Add item to visited items hashmap + // Increment current_index by 1 () - }, + } _ => println!("something's happening"), } - }, + } _ => println!("something's happening"), } current_index += 1; @@ -201,7 +211,7 @@ fn indent(size: usize) -> String { #[tokio::main] async fn main() -> std::io::Result<()> { - use yaserde::de::from_str; +// use yaserde::de::from_str; env_logger::Builder::from_env(Env::default().default_filter_or("trace")) .target(Target::Stdout) @@ -210,12 +220,12 @@ async fn main() -> std::io::Result<()> { let folder_contents: String = // get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public/2019_test_public") - get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public") + get_folder_contents(&String::from("/nextcloud/remote.php/dav/files/adam/test_public")) .await .unwrap(); debug!("{:?}", folder_contents); - let result: Multistatus = from_str(&folder_contents).unwrap(); + let mut result: Multistatus = from_str(&folder_contents).unwrap(); println!("{:?}", result); // traverse(result.response);