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
54
55
56
|
use rusqlite::{
types::{FromSql, FromSqlError},
ToSql,
};
use serde::{Deserialize, Serialize};
use std::path::Path;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MimeType {
#[serde(rename = "application/x.group")]
ApplicationXGroup,
#[serde(rename = "application/pdf")]
ApplicationPdf,
#[serde(rename = "application/zip")]
ApplicationZip,
#[serde(rename = "image/png")]
ImagePng,
#[serde(rename = "image/jpeg")]
ImageJpeg,
#[serde(rename = "text/csv")]
ImageXXcf,
#[serde(rename = "image/x-xcf")]
TextCsv,
#[serde(rename = "text/css")]
TextCss,
}
pub fn guess(name: &str) -> Option<MimeType> {
match Path::new(name).extension()?.to_str()? {
"pdf" => Some(MimeType::ApplicationPdf),
"zip" => Some(MimeType::ApplicationZip),
"png" => Some(MimeType::ImagePng),
"jpg" | "jpeg" => Some(MimeType::ImageJpeg),
"csv" => Some(MimeType::TextCsv),
"css" => Some(MimeType::TextCss),
"xcf" => Some(MimeType::ImageXXcf),
_ => None,
}
}
impl ToSql for MimeType {
fn to_sql(&self) -> rusqlite::Result<rusqlite::types::ToSqlOutput<'_>> {
if let Ok(serde_json::Value::String(mime)) = serde_json::to_value(self) {
return Ok(mime.into());
}
unreachable!()
}
}
impl FromSql for MimeType {
fn column_result(value: rusqlite::types::ValueRef<'_>) -> rusqlite::types::FromSqlResult<Self> {
let v: String = FromSql::column_result(value)?;
serde_json::from_str(&format!("\"{}\"", v)).map_err(|_| FromSqlError::InvalidType)
}
}
|