post_archiver/file_meta/
mod.rs

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
use serde::{Deserialize, Serialize};

#[cfg(feature = "typescript")]
use ts_rs::TS;

use std::{collections::HashMap, hash::Hash, path::PathBuf};

use crate::id::{AuthorId, FileMetaId, PostId};

#[cfg_attr(feature = "typescript", derive(TS))]
#[cfg_attr(feature = "typescript", ts(export))]
#[derive(Deserialize, Serialize, Debug, Clone)]
pub struct FileMeta {
    pub id: FileMetaId,
    pub filename: String,
    pub author: AuthorId,
    pub post: PostId,
    pub mime: String,
    pub extra: HashMap<String, String>,
}

impl FileMeta {
    /// Returns the path of the file  
    /// example: `author/post/filename`
    pub fn path(&self) -> PathBuf {
        PathBuf::from(self.author.to_string())
            .join(self.post.to_string())
            .join(self.filename.to_string())
    }
}

impl Hash for FileMeta {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.id.hash(state);
        self.post.hash(state);
        self.author.hash(state);
        self.mime.hash(state);
        self.filename.hash(state);
    }
}

impl PartialEq for FileMeta {
    fn eq(&self, other: &Self) -> bool {
        self.id == other.id
            && self.post == other.post
            && self.author == other.author
            && self.filename == other.filename
            && self.mime == other.mime
            && self.extra == other.extra
    }
}

impl Eq for FileMeta {}