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), 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> { 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(), } } }