1use serde::{Deserialize, Serialize};
2use std::marker::PhantomData;
3use uuid::Uuid;
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct Id<K: IdKind>(Uuid, #[serde(skip)] PhantomData<K>);
11
12impl<K: IdKind> std::fmt::Display for Id<K> {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 write!(f, "{}{}", K::MNEMONIC, self.0)
15 }
16}
17
18impl<K: IdKind> Id<K> {
19 pub const NULL: Id<K> = Id::<K>(Uuid::nil(), PhantomData::<K>);
20
21 pub const fn from_uuid(id: Uuid) -> Self {
22 Id(id, PhantomData)
23 }
24}
25
26pub trait IdKind {
27 const MNEMONIC: char;
28}
29
30#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
31pub struct BlockKind;
32
33impl IdKind for BlockKind {
34 const MNEMONIC: char = 'B';
35}
36
37pub type BlockId = Id<BlockKind>;
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
40pub struct PinKind;
41
42impl IdKind for PinKind {
43 const MNEMONIC: char = 'P';
44}
45
46pub type PinId = Id<PinKind>;
47
48#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
49pub struct RouteKind;
50
51impl IdKind for RouteKind {
52 const MNEMONIC: char = 'R';
53}
54
55pub type RouteId = Id<RouteKind>;
56
57#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
58pub struct TextKind;
59
60impl IdKind for TextKind {
61 const MNEMONIC: char = 'T';
62}
63
64pub type TextId = Id<TextKind>;
65
66#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
67pub struct CommentKind;
68
69impl IdKind for CommentKind {
70 const MNEMONIC: char = 'C';
71}
72
73pub type CommentId = Id<CommentKind>;
74
75#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
76pub struct ImageKind;
77
78impl IdKind for ImageKind {
79 const MNEMONIC: char = 'I';
80}
81
82pub type ImageId = Id<ImageKind>;
83
84#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
85pub struct LabelKind;
86
87impl IdKind for LabelKind {
88 const MNEMONIC: char = 'L';
89}
90
91pub type LabelId = Id<LabelKind>;
92
93#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
94pub struct RouteLabelKind;
95
96impl IdKind for RouteLabelKind {
97 const MNEMONIC: char = 'X';
98}
99
100pub type RouteLabelId = Id<RouteLabelKind>;