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
use ::Response;

#[derive(Debug, Deserialize, Clone, PartialEq)]
#[serde(deny_unknown_fields)]
pub struct ContentMetadata {
    #[serde(rename="resourceURI")]
    pub resource_uri: String,
    #[serde(rename="relativePath")]
    pub relative_path: String,
    pub text: String,
    pub leaf: bool,
    #[serde(rename="lastModified")]
    pub last_modified: String,
    #[serde(rename="sizeOnDisk")]
    pub size_on_disk: i64,
}

impl<'a> Response<'a, ContentMetadata> {
    pub fn children(&self) -> Result<Vec<Self>, String> {
        if self.item.leaf {
            Ok(Vec::new())
        } else {
            let children_uri = self.item.resource_uri.as_str();
            self.client.get_absolute::<Vec<ContentMetadata>>(children_uri).map(|x| x.into())
        }
    }

    pub fn with_children(self) -> Result<Vec<Self>, String> {
        match self.children() {
            Ok(mut children) => {
                children.insert(0, self);
                Ok(children)
            },
            Err(x) => Err(x)
        }
    }

    pub fn descendants(&self) -> Result<Vec<Self>, String> {
        match self.children() {
            Ok(children) => {
                if children.is_empty() {
                    Ok(children)
                } else {
                    Ok(children.iter().flat_map(|child| child.clone().with_descendants().unwrap()).collect::<Vec<Self>>())
                }
            },
            Err(x) => Err(x)
        }
    }

    pub fn with_descendants(self) -> Result<Vec<Self>, String> {
        match self.descendants() {
            Ok(mut descendants) => {
                descendants.insert(0, self);
                Ok(descendants)
            },
            Err(x) => Err(x)
        }
    }

    pub fn leaves(&self) -> Result<Vec<Self>, String> {
        match self.descendants() {
            Ok(descendants) => Ok(descendants.iter().filter(|&d| d.item.leaf).map(|d| d.to_owned()).collect::<Vec<Self>>()),
            Err(x) => Err(x)
        }
    }

    /*pub fn with_leaves(self) -> impl Iterator<Item = Response<'a, Content>> {
        let self_leaf = if self.item.leaf { Some(self) } else { None };
        self.descendants().chain(self_leaf)
    }*/
}