Skip to main content

news_flash/models/
image.rs

1use std::hash::{Hash, Hasher};
2
3use super::{ArticleID, Url};
4use crate::schema::images;
5
6#[derive(Identifiable, Clone, Insertable, Queryable, Eq, Debug)]
7#[diesel(primary_key(image_url))]
8#[diesel(table_name = images)]
9#[diesel(check_for_backend(SQLite))]
10pub struct ImageMetadata {
11    pub image_url: Url,
12    pub article_id: ArticleID,
13    pub file_path: String,
14    pub width: Option<i32>,
15    pub height: Option<i32>,
16}
17
18impl Hash for ImageMetadata {
19    fn hash<H: Hasher>(&self, state: &mut H) {
20        self.image_url.hash(state);
21    }
22}
23
24impl PartialEq for ImageMetadata {
25    fn eq(&self, other: &ImageMetadata) -> bool {
26        self.image_url == other.image_url
27    }
28}
29
30#[derive(Clone, Eq, Debug)]
31pub struct Image {
32    pub image_url: Url,
33    pub article_id: ArticleID,
34    pub file_path: String,
35    pub width: Option<i32>,
36    pub height: Option<i32>,
37    pub data: Vec<u8>,
38}
39
40impl PartialEq for Image {
41    fn eq(&self, other: &Image) -> bool {
42        self.image_url == other.image_url
43    }
44}
45
46impl Image {
47    pub fn from_metadata(image: ImageMetadata, data: Vec<u8>) -> Self {
48        Self {
49            image_url: image.image_url,
50            article_id: image.article_id,
51            file_path: image.file_path,
52            width: image.width,
53            height: image.height,
54            data,
55        }
56    }
57}