Skip to main content

post_archiver/
tag.rs

1use std::hash::Hash;
2
3use serde::{Deserialize, Serialize};
4
5#[cfg(feature = "typescript")]
6use ts_rs::TS;
7
8use crate::{id::TagId, PlatformId, PostId};
9
10/// A label that can be applied to posts
11///
12/// # Safety
13/// - Name must not be empty
14/// - Name should be kebab-case
15/// - Name can be chained (e.g. "x:y:z")
16#[cfg_attr(feature = "typescript", derive(TS))]
17#[cfg_attr(feature = "typescript", ts(export))]
18#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
19pub struct Tag {
20    pub id: TagId,
21    pub name: String,
22    pub platform: Option<PlatformId>,
23}
24
25/// Association type that creates a many-to-many relationship between posts and tags
26#[cfg_attr(feature = "typescript", derive(TS))]
27#[cfg_attr(feature = "typescript", ts(export))]
28#[derive(Deserialize, Serialize, Debug, Clone, PartialEq, Eq, Hash)]
29pub struct PostTag {
30    pub post: PostId,
31    pub tag: TagId,
32}
33
34#[cfg(feature = "utils")]
35mod definitions {
36    use crate::utils::macros::as_table;
37
38    use super::*;
39
40    as_table! {
41        "tags" => Tag {
42            id: "id",
43            name: "name",
44            platform: "platform",
45        }
46
47        "post_tags" => PostTag {
48            post: "post",
49            tag: "tag",
50        }
51    }
52}