summaryrefslogtreecommitdiffstats
path: root/src/backend/payload.rs
blob: 695ff96cbe48f6d3aea304455e598128b3de3e7f (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
use crate::backend::DateTime;
use crate::backend::MimeType;

#[derive(Debug, Eq, PartialEq, libipld::DagCbor)]
pub struct Payload {
    mime: MimeType,
    timestamp: DateTime,
    content: Vec<u8>,
}

impl Payload {
    pub fn new(mime: MimeType, timestamp: DateTime) -> Self {
        Payload { mime, timestamp, content: Vec::new() }
    }

    pub fn now_from_text(text: String) -> Payload {
        let mime = MimeType::from(mime::TEXT_PLAIN_UTF_8);
        let timestamp = DateTime::from(chrono::offset::Utc::now());

        Self::new(mime, timestamp).with_content(text.as_bytes().to_vec())
    }

    pub fn with_content(mut self, v: Vec<u8>) -> Self {
        self.content = v;
        self
    }

    pub fn with_mimetype(mut self, mime: MimeType) -> Self {
        self.mime = mime;
        self
    }

    pub fn with_timestamp(mut self, ts: DateTime) -> Self {
        self.timestamp = ts;
        self
    }

    pub fn content(&self) -> &Vec<u8> {
        &self.content
    }
}