summaryrefslogtreecommitdiffstats
path: root/src/types/block.rs
blob: 1927d557967236d06c8cb208bfa2c351cd066920 (plain)
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
use crate::types::util::IPFSHash;
use crate::types::util::Version;
use crate::types::content::Content;
use crate::types::payload::*;
use crate::types::content::LoadedContent;
use crate::repository::repository::Repository;

use anyhow::Error;

#[derive(Serialize, Deserialize, Debug)]
pub struct Block {
    /// The version of the API in use
    #[serde(rename = "v")]
    version: Version,

    /// The parents of this Profile instance
    ///
    /// Multiple are required for merging.
    #[serde(rename = "parents")]
    #[serde(default)]
    parents: Vec<IPFSHash>,

    /// The content of this block, pointed to by IPFS hash.
    #[serde(rename = "content")]
    content: IPFSHash,
}

impl Block {
    pub fn new(version: Version, parents: Vec<IPFSHash>, content: IPFSHash) -> Self {
        Block { version, parents, content }
    }

    pub fn version(&self) -> &Version {
        &self.version
    }

    pub fn parents(&self) -> &Vec<IPFSHash> {
        &self.parents
    }

    pub fn content(&self) -> &IPFSHash {
        &self.content
    }

    pub async fn load(self, r: &Repository) -> Result<LoadedBlock, Error> {
        Ok({
            LoadedBlock {
                version: self.version,
                parents: self.parents,
                content: r.get_content(&self.content)
                    .await?
                    .load(r)
                    .await?
            }
        })
    }
}

impl AsRef<Block> for Block {
    fn as_ref(&self) -> &Self {
        &self
    }
}

#[derive(Debug)]
pub struct LoadedBlock {
    version: Version,
    parents: Vec<IPFSHash>,
    content: LoadedContent,
}

impl LoadedBlock {
    pub fn version(&self) -> &Version {
        &self.version
    }

    pub fn parents(&self) -> &Vec<IPFSHash> {
        &self.parents
    }

    pub fn content(&self) -> &LoadedContent {
        &self.content
    }
}