diff options
Diffstat (limited to 'src/server/attrs.rs')
-rw-r--r-- | src/server/attrs.rs | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/src/server/attrs.rs b/src/server/attrs.rs new file mode 100644 index 0000000..eeb273a --- /dev/null +++ b/src/server/attrs.rs @@ -0,0 +1,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(), + } + } +} |