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

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

use std::collections::BTreeMap;
use std::ops::Deref;
use std::path::Path;

use drawbridge_jose::jws::Jws;
use drawbridge_jose::MediaTyped;
use drawbridge_type::TreeContent::{Directory, File};
use drawbridge_type::{TagEntry, TagName, Tree, TreeEntry, TreePath};

use ureq::serde::Serialize;

pub struct Tag<'a>(Entity<'a>);

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

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

impl<'a> Tag<'a> {
    pub fn new(entity: Entity<'a>, name: &TagName) -> Self {
        Tag(entity.child(&name.to_string()))
    }

    pub fn create(&self, entry: &TagEntry<impl Serialize>) -> Result<bool> {
        let mime = match entry {
            TagEntry::Unsigned(..) => TreeEntry::<()>::TYPE,
            TagEntry::Signed(..) => Jws::TYPE,
        }
        .parse()
        .expect("failed to parse tag entry media type");
        self.0.create_json(&mime, entry)
    }

    // TODO: Support signed tags
    pub fn create_from_path_unsigned(
        &self,
        path: impl AsRef<Path>,
    ) -> Result<(bool, BTreeMap<TreePath, bool>)> {
        let tree = Tree::from_path_sync(path)?;
        let tag_created = self.create(&TagEntry::Unsigned(tree.root()))?;
        let tree_created = tree
            .into_iter()
            .map(
                |(
                    path,
                    TreeEntry {
                        ref meta,
                        ref content,
                        ..
                    },
                )| {
                    let node = Node::new(self.child("tree"), &path);
                    let created = match content {
                        File(file) => node.create_from(meta, file)?,
                        Directory(buf) => node.create_from(meta, buf.as_slice())?,
                    };
                    Ok((path, created))
                },
            )
            .collect::<Result<_>>()?;
        Ok((tag_created, tree_created))
    }

    pub fn get(&self) -> Result<TagEntry> {
        self.0.get_json()
    }

    pub fn path(&self, path: &TreePath) -> Node<'a> {
        Node::new(self.child("tree"), path)
    }
}