1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
pub use reqwest as request;
use reqwest::Response;
use serde::{Deserialize, Serialize};
use std::ops::Deref;

#[derive(Deserialize, Debug, Serialize)]
pub struct Multistatus {
    #[serde(rename = "response", default)]
    pub responses: Vec<DResponse>,
}

impl Multistatus {
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Result<Self, quick_xml::DeError> {
        quick_xml::de::from_str(s)
    }
    pub async fn from_response(res: Response) -> Result<Self, String> {
        if res.status().is_success() {
            let text = res.text().await.map_err(|err| err.to_string())?;
            Self::from_str(&text).map_err(|err| err.to_string())
        } else {
            Err(res.status().to_string())
        }
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct DResponse {
    #[serde(rename = "href")]
    pub href: String,
    pub propstat: Propstat,
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Propstat {
    prop: Prop,
    pub status: String,
}
impl Deref for Propstat {
    type Target = Prop;

    fn deref(&self) -> &Self::Target {
        &self.prop
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct Prop {
    #[serde(rename = "displayname")]
    pub display_name: String,
    #[serde(default, rename = "getcontenttype")]
    pub content_type: String,
    #[serde(default, rename = "getlastmodified")]
    pub last_modified: String,
    #[serde(rename = "getcontentlength", default)]
    pub content_length: u64,
    #[serde(alias = "iscollection", default)]
    pub collection: bool,
    #[serde(rename = "resourcetype", default)]
    pub resource_type: Option<ResourceType>,
}



impl Prop {
    pub fn is_collection(&self) -> bool {
        self.collection
            || self
                .resource_type
                .as_ref()
                .is_some_and(|ty| ty.collection.is_some())
    }
}

#[derive(Debug, Deserialize, Serialize)]
pub struct ResourceType {
    #[serde(default)]
    collection: Option<String>,
}

#[derive(Default, Debug)]
pub struct FileTree {
    pub href: String,
    pub filename: String,
    pub children: Vec<FileNode>,
}
impl From<Multistatus> for FileTree {
    fn from(value: Multistatus) -> Self {
        if value.responses.is_empty() {
            return Default::default();
        }
        let mut iter = value.responses.into_iter();
        let header_node = iter.next().expect("This will not be empty!");
        FileTree {
            href: header_node.href,
            filename: header_node.propstat.prop.display_name,
            children: iter
                .map(|node| FileNode {
                    is_dir: node.propstat.is_collection(),
                    href: percent_encoding::percent_decode_str(&node.href)
                        .decode_utf8()
                        .map(|s| s.to_string())
                        .unwrap_or(node.href),
                    filename: node.propstat.prop.display_name,
                    last_modified: node.propstat.prop.last_modified,
                    len: node.propstat.prop.content_length,
                    content_type: node.propstat.prop.content_type,
                })
                .collect(),
        }
    }
}
#[derive(Default, Debug, Clone)]
pub struct FileNode {
    pub is_dir: bool,
    pub href: String,
    pub filename: String,
    pub last_modified: String,
    pub len: u64,
    pub content_type: String
}