drawbridge_client/
tree.rs

1// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com>
2// SPDX-License-Identifier: Apache-2.0
3
4use super::{scope, Entity, Result, Scope};
5
6use std::io::Read;
7use std::ops::Deref;
8
9use drawbridge_type::{Meta, TreeDirectory, TreeEntry, TreePath};
10
11use mime::Mime;
12use ureq::serde::Serialize;
13
14#[derive(Clone, Debug)]
15pub struct Node<'a, S: Scope>(Entity<'a, S, scope::Node>);
16
17impl<'a, S: Scope> Deref for Node<'a, S> {
18    type Target = Entity<'a, S, scope::Node>;
19
20    fn deref(&self) -> &Self::Target {
21        &self.0
22    }
23}
24
25impl<'a, S: Scope> Node<'a, S> {
26    pub fn new(entity: Entity<'a, S, scope::Node>, path: &TreePath) -> Self {
27        if path.is_empty() {
28            Self(entity)
29        } else {
30            Self(entity.child(&path.to_string()))
31        }
32    }
33
34    pub fn create_bytes(&self, mime: &Mime, data: impl AsRef<[u8]>) -> Result<bool> {
35        self.0.create_bytes(mime, data)
36    }
37
38    pub fn create_json(&self, mime: &Mime, val: &impl Serialize) -> Result<bool> {
39        self.0.create_json(mime, val)
40    }
41
42    pub fn create_from(&self, meta: &Meta, rdr: impl Read) -> Result<bool> {
43        self.0.create_from(meta, rdr)
44    }
45
46    pub fn create_directory<C>(&self, dir: &TreeDirectory<TreeEntry<C>>) -> Result<bool> {
47        let mime = TreeDirectory::<C>::TYPE
48            .parse()
49            .expect("failed to parse tree directory media type");
50        self.create_json(&mime, dir)
51    }
52}