Highlight resource type elements

This commit is contained in:
Adam Cooper 2021-12-20 04:52:09 -05:00
parent 57da34b095
commit 348cf4d843

View file

@ -7,7 +7,7 @@ use env_logger::{ Env, Target };
use log::{debug, info, log_enabled, warn, error}; use log::{debug, info, log_enabled, warn, error};
use reqwest; use reqwest;
#[allow(unused)] #[allow(unused)]
use std::io::{self, Write}; use std::io::{self, BufReader, Write};
#[allow(unused)] #[allow(unused)]
use xml::reader::{EventReader, XmlEvent}; use xml::reader::{EventReader, XmlEvent};
@ -57,6 +57,44 @@ fn get_password() -> Result<String, std::io::Error> {
} }
} }
fn indent(size: usize) -> String {
const INDENT: &'static str = " ";
(0..size).map(|_| INDENT)
.fold(String::with_capacity(size*INDENT.len()), |r, s| r + s)
}
fn parse_xml(xml_string: String) {
let xml_str = &xml_string;
let parser = EventReader::from_str(xml_str);
let mut depth = 0;
let mut resourcetype_element = false;
let mut share_type_element = false;
for e in parser {
match e {
Ok(XmlEvent::StartElement { name, .. }) => {
println!("{}+{}", indent(depth), name);
depth += 1;
if name.local_name.matches("resourcetype").collect::<Vec<&str>>().len() > 0 {
println!("{} Element: resourcetype", indent(depth));
resourcetype_element = true;
}
}
Ok(XmlEvent::Characters(s)) => {
println!("{} {}", indent(depth), s);
}
Ok(XmlEvent::EndElement { name }) => {
depth -= 1;
println!("{}-{}", indent(depth), name);
}
Err(e) => {
println!("Error: {}", e);
break;
}
_ => {}
}
}
}
#[tokio::main] #[tokio::main]
async fn main() -> std::io::Result<()> { async fn main() -> std::io::Result<()> {
env_logger::Builder::from_env(Env::default().default_filter_or("trace")).target(Target::Stdout).init(); env_logger::Builder::from_env(Env::default().default_filter_or("trace")).target(Target::Stdout).init();
@ -64,7 +102,9 @@ async fn main() -> std::io::Result<()> {
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);
let folder_contents = get_folder_contents(password, "/nextcloud/remote.php/dav/files/adam/public").await; let folder_contents = get_folder_contents(password, "/nextcloud/remote.php/dav/files/adam/test_public").await.unwrap();
debug!("{:?}", folder_contents); debug!("{:?}", folder_contents);
parse_xml(folder_contents);
Ok(()) Ok(())
} }