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
// SPDX-FileCopyrightText: 2022 Profian Inc. <opensource@profian.com>
// SPDX-License-Identifier: AGPL-3.0-only

use super::{CreateError, Entity, Node};

use std::ops::Deref;

use drawbridge_type::{Meta, TreeDirectory, TreeEntry, TreePath};

use camino::{Utf8Path, Utf8PathBuf};
use futures::{try_join, AsyncRead};
use log::debug;

#[repr(transparent)]
#[derive(Copy, Clone, Debug)]
pub struct Tag<'a, P = Utf8PathBuf>(Entity<'a, P>);

impl<'a, P> Deref for Tag<'a, P> {
    type Target = Entity<'a, P>;

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

impl<'a, P> From<Entity<'a, P>> for Tag<'a, P> {
    fn from(entity: Entity<'a, P>) -> Self {
        Self(entity)
    }
}

impl<'a, P: AsRef<Utf8Path>> Tag<'a, P> {
    pub fn node(&self, path: &TreePath) -> Node<'a, Utf8PathBuf> {
        if path.is_empty() {
            self.0.child("tree").into()
        } else {
            self.0
                .child(format!("tree/entries/{}", path.intersperse("/entries/")))
                .into()
        }
    }

    pub async fn create_file_node(
        &self,
        path: &TreePath,
        meta: Meta,
        rdr: impl Unpin + AsyncRead,
    ) -> Result<Node<'a, Utf8PathBuf>, CreateError<anyhow::Error>> {
        // TODO: Validate node hash against parents' expected values
        // https://github.com/profianinc/drawbridge/issues/77
        let node = self.node(path);
        node.create_dir("").await.map_err(|e| {
            debug!(target: "app::store::Tag::create_file_node", "failed to create content directory: {:?}", e);
            e
        })?;
        node.create_from_reader(meta, rdr).await?;
        Ok(node)
    }

    pub async fn create_directory_node(
        &self,
        path: &TreePath,
        meta: Meta,
        dir: &TreeDirectory<TreeEntry>,
    ) -> Result<Node<'a, Utf8PathBuf>, CreateError<anyhow::Error>> {
        // TODO: Validate node hash against parents' expected values
        // https://github.com/profianinc/drawbridge/issues/77
        let node = self.node(path);
        node.create_dir("").await.map_err(|e| {
            debug!(target: "app::store::Tag::create_directory_node", "failed to create content directory: {:?}", e);
            e
        })?;
        try_join!(node.create_json(meta, dir), node.create_dir("entries"))?;
        Ok(node)
    }
}