drizzle_types/sqlite/ddl/
table.rs1use crate::alloc_prelude::*;
8
9#[cfg(feature = "serde")]
10use crate::serde_helpers::cow_from_string;
11
12#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
34pub struct TableDef {
35 pub name: &'static str,
37 pub strict: bool,
39 pub without_rowid: bool,
41}
42
43impl TableDef {
44 #[must_use]
46 pub const fn new(name: &'static str) -> Self {
47 Self {
48 name,
49 strict: false,
50 without_rowid: false,
51 }
52 }
53
54 #[must_use]
56 pub const fn strict(self) -> Self {
57 Self {
58 name: self.name,
59 strict: true,
60 without_rowid: self.without_rowid,
61 }
62 }
63
64 #[must_use]
66 pub const fn without_rowid(self) -> Self {
67 Self {
68 name: self.name,
69 strict: self.strict,
70 without_rowid: true,
71 }
72 }
73
74 #[must_use]
76 pub const fn into_table(self) -> Table {
77 Table {
78 name: Cow::Borrowed(self.name),
79 strict: self.strict,
80 without_rowid: self.without_rowid,
81 }
82 }
83}
84
85impl Default for TableDef {
86 fn default() -> Self {
87 Self::new("")
88 }
89}
90
91#[derive(Clone, Debug, PartialEq, Eq)]
122#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
123#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
124pub struct Table {
125 #[cfg_attr(feature = "serde", serde(deserialize_with = "cow_from_string"))]
127 pub name: Cow<'static, str>,
128
129 #[cfg_attr(
131 feature = "serde",
132 serde(default, skip_serializing_if = "core::ops::Not::not")
133 )]
134 pub strict: bool,
135
136 #[cfg_attr(
138 feature = "serde",
139 serde(default, skip_serializing_if = "core::ops::Not::not")
140 )]
141 pub without_rowid: bool,
142}
143
144impl Table {
145 #[must_use]
147 pub fn new(name: impl Into<Cow<'static, str>>) -> Self {
148 Self {
149 name: name.into(),
150 strict: false,
151 without_rowid: false,
152 }
153 }
154
155 #[must_use]
157 pub const fn strict(mut self) -> Self {
158 self.strict = true;
159 self
160 }
161
162 #[must_use]
164 pub const fn without_rowid(mut self) -> Self {
165 self.without_rowid = true;
166 self
167 }
168
169 #[inline]
171 #[must_use]
172 pub fn name(&self) -> &str {
173 &self.name
174 }
175}
176
177impl Default for Table {
178 fn default() -> Self {
179 Self::new("")
180 }
181}
182
183impl From<TableDef> for Table {
184 fn from(def: TableDef) -> Self {
185 def.into_table()
186 }
187}
188
189#[cfg(test)]
190mod tests {
191 use super::*;
192
193 #[test]
194 fn test_const_table_def() {
195 const TABLE: TableDef = TableDef::new("users").strict().without_rowid();
196 assert_eq!(TABLE.name, "users");
197 const {
198 assert!(TABLE.strict);
199 }
200 const {
201 assert!(TABLE.without_rowid);
202 }
203 }
204
205 #[test]
206 fn test_table_def_to_table() {
207 const DEF: TableDef = TableDef::new("users").strict();
208 let table = DEF.into_table();
209 assert_eq!(table.name(), "users");
210 assert!(table.strict);
211 }
212
213 #[test]
214 fn test_runtime_table() {
215 let table = Table::new("posts").strict();
216 assert_eq!(table.name(), "posts");
217 assert!(table.strict);
218 assert!(!table.without_rowid);
219 }
220
221 #[cfg(feature = "serde")]
222 #[test]
223 fn test_serde_roundtrip() {
224 let table = Table::new("users").strict();
225 let json = serde_json::to_string(&table).unwrap();
226 let parsed: Table = serde_json::from_str(&json).unwrap();
227 assert_eq!(parsed.name(), "users");
228 assert!(parsed.strict);
229 }
230}