Skip to main content

systemprompt_files/models/
content_file.rs

1use anyhow::{anyhow, Result};
2use chrono::{DateTime, Utc};
3use serde::{Deserialize, Serialize};
4use sqlx::FromRow;
5use systemprompt_identifiers::ContentId;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
8#[serde(rename_all = "lowercase")]
9pub enum FileRole {
10    Featured,
11    #[default]
12    Attachment,
13    Inline,
14    OgImage,
15    Thumbnail,
16}
17
18impl FileRole {
19    pub const fn as_str(&self) -> &'static str {
20        match self {
21            Self::Featured => "featured",
22            Self::Attachment => "attachment",
23            Self::Inline => "inline",
24            Self::OgImage => "og_image",
25            Self::Thumbnail => "thumbnail",
26        }
27    }
28
29    pub fn parse(s: &str) -> Result<Self> {
30        match s.to_lowercase().as_str() {
31            "featured" => Ok(Self::Featured),
32            "attachment" => Ok(Self::Attachment),
33            "inline" => Ok(Self::Inline),
34            "og_image" => Ok(Self::OgImage),
35            "thumbnail" => Ok(Self::Thumbnail),
36            _ => Err(anyhow!("Invalid file role: {s}")),
37        }
38    }
39}
40
41
42impl std::fmt::Display for FileRole {
43    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
44        write!(f, "{}", self.as_str())
45    }
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize, FromRow)]
49pub struct ContentFile {
50    pub id: i32,
51    pub content_id: ContentId,
52    pub file_id: uuid::Uuid,
53    pub role: String,
54    pub display_order: i32,
55    pub created_at: DateTime<Utc>,
56}
57
58impl ContentFile {
59    pub fn parsed_role(&self) -> Result<FileRole> {
60        FileRole::parse(&self.role)
61    }
62}