summaryrefslogtreecommitdiff
path: root/src/viewmodel/utility.rs
blob: 617ee45672abe7d141a18300b7c6165bd3a20535 (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
#[derive(Clone)]
pub struct Download {
    pub file_name: String,
    pub content: String,
    pub mime_type: MimeType,
}

impl Download {
    pub fn as_data_link(&self) -> String {
        let data = base64::encode(&*self.content);
        let uri_component: String = js_sys::encode_uri_component(&data).into();

        format!("data:{};base64,{}", self.mime_type.as_text(), uri_component)
    }
}

#[derive(Clone, Copy)]
pub enum MimeType {
    PDF,
    VCard,
    SVG,
}

impl MimeType {
    pub fn as_text(&self) -> &str {
        match self {
            MimeType::PDF => "application/pdf",
            MimeType::VCard => "text/vcard",
            MimeType::SVG => "image/svg+xml",
        }
    }
}

#[derive(Clone, Copy, PartialEq)]
pub enum DownloadOption {
    PDF,
    VCard,
    QrCode,
}

#[derive(Clone, Debug, PartialEq)]
pub struct File {
    pub name: String,
    pub content: String,
}