immigrant_schema/
names.rs1use std::fmt::{self, Debug};
2
3use crate::{
4 ids::{DbIdent, Ident, Kind},
5 span::SimpleSpan,
6 HasDefaultDbName, HasIdent,
7};
8
9macro_rules! def_kind {
10 ($($name:ident($v:expr_2021)),+ $(,)?) => {
11 $(
12 pub enum $name {}
13 impl Kind for $name {
14 fn id() -> u8 {
15 $v
16 }
17 }
18 )+
19 };
20}
21
22def_kind!(
23 ColumnKind(2),
24 FieldKind(2),
25 ProcedureKind(3),
26 EnumItemKind(4),
27 IndexKind(5),
28 ConstraintKind(6),
29 ForeignKeyKind(7),
30 NativeTypeKind(8),
31 ItemKind(9),
32 TableKind(9),
33 MixinKind(9),
34 TypeKind(9),
35 ViewKind(9),
36 TriggerKind(9),
37 UnknownKind(20),
38);
39
40pub struct DefName<K> {
41 pub code: Ident<K>,
42 pub db: Option<DbIdent<K>>,
43}
44impl<K: Kind> DefName<K> {
45 pub fn unchecked_new(code: Ident<K>, db: Option<DbIdent<K>>) -> Self {
46 Self { code, db }
47 }
48 pub fn alloc((span, code, db): (SimpleSpan, &str, Option<&str>)) -> Self {
49 Self {
50 code: Ident::alloc((span, code)),
51 db: db.map(DbIdent::new),
52 }
53 }
54 pub fn unchecked_cast<U: Kind>(v: DefName<U>) -> Self {
55 Self {
56 code: Ident::unchecked_cast(v.code),
57 db: v.db.map(DbIdent::unchecked_from),
58 }
59 }
60}
61impl<K> HasIdent for DefName<K> {
62 type Kind = K;
63 fn id(&self) -> Ident<Self::Kind> {
64 self.code
65 }
66}
67impl<K> HasDefaultDbName for DefName<K> {
68 type Kind = K;
69 fn default_db(&self) -> Option<DbIdent<Self::Kind>> {
70 self.db.clone()
71 }
72}
73impl<K> Debug for DefName<K> {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 write!(f, "{:?} \"{:?}\"", self.code, self.db)
76 }
77}
78impl<K> PartialEq<Ident<K>> for DefName<K> {
79 fn eq(&self, other: &Ident<K>) -> bool {
80 &self.code == other
81 }
82}
83impl<K> PartialEq for DefName<K> {
84 fn eq(&self, other: &Self) -> bool {
85 self.code == other.code && self.db == other.db
86 }
87}
88
89impl<K> Clone for DefName<K> {
90 fn clone(&self) -> Self {
91 Self {
92 code: self.code,
93 db: self.db.clone(),
94 }
95 }
96}
97
98pub type TableDefName = DefName<TableKind>;
99pub type ViewDefName = DefName<ViewKind>;
100pub type TriggerDefName = DefName<TriggerKind>;
101pub type ColumnDefName = DefName<ColumnKind>;
102pub type TypeDefName = DefName<TypeKind>;
103pub type EnumItemDefName = DefName<EnumItemKind>;
104pub type CompositeItemDefName = DefName<FieldKind>;
105
106pub type TableIdent = Ident<TableKind>;
107pub type ViewIdent = Ident<ViewKind>;
108pub type TriggerIdent = Ident<TriggerKind>;
109pub type ColumnIdent = Ident<ColumnKind>;
110pub type TypeIdent = Ident<TypeKind>;
111pub type FieldIdent = Ident<FieldKind>;
112pub type MixinIdent = Ident<MixinKind>;
113
114pub type DbTable = DbIdent<TableKind>;
115pub type DbView = DbIdent<ViewKind>;
116pub type DbTrigger = DbIdent<TriggerKind>;
117pub type DbColumn = DbIdent<ColumnKind>;
118pub type DbType = DbIdent<TypeKind>;
119pub type DbProcedure = DbIdent<ProcedureKind>;
120pub type DbIndex = DbIdent<IndexKind>;
121pub type DbConstraint = DbIdent<ConstraintKind>;
122pub type DbForeignKey = DbIdent<ForeignKeyKind>;
123pub type DbEnumItem = DbIdent<EnumItemKind>;
124pub type DbNativeType = DbIdent<NativeTypeKind>;
125pub type DbItem = DbIdent<ItemKind>;