aboutsummaryrefslogtreecommitdiff
path: root/src/server/attrs.rs
blob: eeb273a4563a4a98f441d19edcd26b06e9e5ecdf (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
use super::blobref::BlobRef;
use crate::common::{json, mime::MimeType};
use rusqlite::ToSql;
use serde::{Deserialize, Serialize};
use time::OffsetDateTime;

#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Attr {
    Name(String),
    Group(BlobRef),
    Mime(MimeType),
    CreatedAt(#[serde(with = "time::serde::rfc3339")] OffsetDateTime),
    UpdatedAt(#[serde(with = "time::serde::rfc3339")] OffsetDateTime),
    DeletedAt(#[serde(with = "time::serde::rfc3339")] OffsetDateTime),
    Tags(Vec<String>),
    Note(String),
}

pub fn key(attr: &Attr) -> String {
    match attr {
        Attr::Name(_) => "name",
        Attr::Group(_) => "group",
        Attr::Mime(_) => "mime",
        Attr::CreatedAt(_) => "created_at",
        Attr::UpdatedAt(_) => "updated_at",
        Attr::DeletedAt(_) => "deleted_at",
        Attr::Tags(_) => "tags",
        Attr::Note(_) => "note",
    }
    .into()
}

impl ToSql for Attr {
    fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
        match self {
            Attr::Name(name) => name.to_sql(),
            Attr::Group(group) => group.to_sql(),
            Attr::Mime(mime) => mime.to_sql(),
            Attr::CreatedAt(created_at) => created_at.to_sql(),
            Attr::UpdatedAt(updated_at) => updated_at.to_sql(),
            Attr::DeletedAt(deleted_at) => deleted_at.to_sql(),
            Attr::Tags(tags) => Ok(json::serialize(tags).into()),
            Attr::Note(note) => note.to_sql(),
        }
    }
}