[wip] Commit before appeasing the borrow checker

To appease the borrow checker, we'll move the whole struct from main()
into tree_traversal().
This commit is contained in:
Adam Cooper 2022-01-27 08:46:55 -05:00
parent 767d77ffcc
commit 3b69ca58cd

View file

@ -3,11 +3,12 @@ use env_logger::{Env, Target};
#[allow(unused)] #[allow(unused)]
use log::{debug, error, info, log_enabled, warn}; use log::{debug, error, info, log_enabled, warn};
use reqwest; use reqwest;
#[allow(unused)]
use std::io::{self, BufReader, Write};
use std::collections::HashMap; use std::collections::HashMap;
#[allow(unused)] #[allow(unused)]
use std::io::{self, BufReader, Write};
#[allow(unused)]
use yaserde_derive::{YaDeserialize, YaSerialize}; use yaserde_derive::{YaDeserialize, YaSerialize};
use yaserde::de::from_str;
#[allow(unused)] #[allow(unused)]
fn publicise() {} fn publicise() {}
@ -19,7 +20,7 @@ fn publicise() {}
namespace = "d: DAV:", namespace = "d: DAV:",
namespace = "nc: http://nextcloud.org/ns", namespace = "nc: http://nextcloud.org/ns",
namespace = "oc: http://owncloud.org/ns", namespace = "oc: http://owncloud.org/ns",
namespace = "s: http://sabredav.org/ns", namespace = "s: http://sabredav.org/ns"
)] )]
pub struct Multistatus { pub struct Multistatus {
#[yaserde(prefix = "d")] #[yaserde(prefix = "d")]
@ -30,7 +31,8 @@ pub struct Multistatus {
#[yaserde(rename = "response", prefix = "d", namespace = "d: DAV:")] #[yaserde(rename = "response", prefix = "d", namespace = "d: DAV:")]
pub struct NextcloudResponse { pub struct NextcloudResponse {
#[yaserde(prefix = "d")] #[yaserde(prefix = "d")]
href: Option<String>, // href: Option<String>,
href: String,
#[yaserde(prefix = "d")] #[yaserde(prefix = "d")]
propstat: Vec<Propstat>, propstat: Vec<Propstat>,
} }
@ -45,7 +47,11 @@ pub struct Propstat {
} }
#[derive(Default, PartialEq, Debug, YaDeserialize)] #[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 { pub struct Prop {
#[yaserde(prefix = "d", rename = "getlastmodified")] #[yaserde(prefix = "d", rename = "getlastmodified")]
get_last_modified: Option<String>, get_last_modified: Option<String>,
@ -84,7 +90,7 @@ pub struct Collection {
collection: Option<String>, collection: Option<String>,
} }
async fn get_folder_contents(url_tail: &str) -> Result<String, Box<dyn std::error::Error>> { async fn get_folder_contents(url_tail: &String) -> Result<String, Box<dyn std::error::Error>> {
debug!("Entering: get_folder_contents()"); debug!("Entering: get_folder_contents()");
let password = get_password().unwrap().trim().to_string(); let password = get_password().unwrap().trim().to_string();
debug!("Received password: {}[END]", password); debug!("Received password: {}[END]", password);
@ -120,21 +126,24 @@ async fn get_folder_contents(url_tail: &str) -> Result<String, Box<dyn std::erro
} }
#[tokio::main] #[tokio::main]
async fn traverse(queue: Vec<NextcloudResponse>) { async fn traverse(mut queue: Vec<NextcloudResponse>) {
debug!("Entering: traverse()"); debug!("Entering: traverse()");
// Initialize the indexed "pointer" // Initialize the indexed "pointer"
let mut current_index: usize = 0; let mut current_index: usize = 0;
let mut already_visited: bool; let mut already_visited: bool;
// Initialize the hashmap of visited items (by etag?) // Initialize the hashmap of visited items (by etag?)
let mut visited_items = HashMap::new(); let mut visited_items = HashMap::new();
match &queue[0].propstat[0].prop { // match queue[0].propstat[0].prop {
Some(prop) => { match queue.into_iter().nth(0).unwrap()
match &prop.get_etag { .propstat.into_iter().nth(0).unwrap().prop {
Some(etag) => { visited_items.insert(etag, true); () }, Some(prop) => match &prop.get_etag {
_ => current_index += 1 Some(etag) => {
visited_items.insert(etag, true);
()
} }
_ => current_index += 1,
}, },
_ => current_index += 1 _ => current_index += 1,
} }
// Depth first traversal // Depth first traversal
while !queue.is_empty() { while !queue.is_empty() {
@ -142,38 +151,39 @@ async fn traverse(queue: Vec<NextcloudResponse>) {
Some(prop) => { Some(prop) => {
match &prop.get_etag { match &prop.get_etag {
Some(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 !visited_items.contains_key(etag) {
// If item is a collection // If item is a collection
// TODO(amcooper): Again, replace unwrap with the match implementation, // TODO(amcooper): Again, replace unwrap with the match implementation,
// or find another way to handle these structures. // or find another way to handle these structures.
match &prop.resource_type { match &prop.resource_type {
Some(resource_type) => { Some(resource_type) => {
if !resource_type.collection.is_empty() { if !resource_type.collection.is_empty() {
// Get the contents XML // Get the contents XML
/* TODO(amcooper): Fix argument let folder_contents: String = get_folder_contents(
let folder_contents: String = &queue[current_index].href,
get_folder_contents(&queue[current_index].href.unwrap()) )
.await .await
.unwrap(); .unwrap();
debug!("{:?}", folder_contents); debug!("{:?}", folder_contents);
*/ // Parse the contents XML into Multistatus
// Parse the contents XML into Multistatus let mut result: Multistatus = from_str(&String::from(folder_contents)).unwrap();
// Append the NextcloudResponse vector to queue // Append the NextcloudResponse vector to queue
} queue.append(&mut result.response);
}, }
_ => current_index += 1 }
_ => current_index += 1,
} }
} }
// else // else
// if item is not public, publicise it. // if item is not public, publicise it.
// Add item to visited items hashmap // Add item to visited items hashmap
// Increment current_index by 1 // Increment current_index by 1
() ()
}, }
_ => println!("something's happening"), _ => println!("something's happening"),
} }
}, }
_ => println!("something's happening"), _ => println!("something's happening"),
} }
current_index += 1; current_index += 1;
@ -201,7 +211,7 @@ fn indent(size: usize) -> String {
#[tokio::main] #[tokio::main]
async fn main() -> std::io::Result<()> { 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")) env_logger::Builder::from_env(Env::default().default_filter_or("trace"))
.target(Target::Stdout) .target(Target::Stdout)
@ -210,12 +220,12 @@ async fn main() -> std::io::Result<()> {
let folder_contents: String = 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/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 .await
.unwrap(); .unwrap();
debug!("{:?}", folder_contents); debug!("{:?}", folder_contents);
let result: Multistatus = from_str(&folder_contents).unwrap(); let mut result: Multistatus = from_str(&folder_contents).unwrap();
println!("{:?}", result); println!("{:?}", result);
// traverse(result.response); // traverse(result.response);