1use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7#[derive(Debug, Clone, PartialEq)]
10pub struct FieldDeconstruction {
11 pub name: Option<String>,
13 pub path: String,
15 pub args: Vec<FieldArg>,
17 pub kwargs: HashMap<String, FieldKwarg>,
19}
20
21#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
23#[serde(untagged)]
24pub enum FieldArg {
25 String(String),
27 Int(i64),
29 Bool(bool),
31 Float(f64),
33}
34
35#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
37#[serde(untagged)]
38pub enum FieldKwarg {
39 String(String),
41 Int(i64),
43 Uint(u64),
45 Bool(bool),
47 Float(f64),
49 Choices(Vec<(String, String)>),
51 Callable(String), }
54
55pub trait Field: Send + Sync {
57 fn deconstruct(&self) -> FieldDeconstruction;
60
61 fn set_attributes_from_name(&mut self, name: &str);
63
64 fn name(&self) -> Option<&str>;
66
67 fn is_primary_key(&self) -> bool {
69 false
70 }
71
72 fn is_null(&self) -> bool {
74 false
75 }
76
77 fn is_blank(&self) -> bool {
79 false
80 }
81}
82
83#[derive(Debug, Clone)]
85pub struct BaseField {
86 pub name: Option<String>,
88 pub null: bool,
90 pub blank: bool,
92 pub default: Option<FieldKwarg>,
94 pub db_default: Option<FieldKwarg>, pub db_column: Option<String>,
98 pub db_tablespace: Option<String>,
100 pub primary_key: bool,
102 pub unique: bool,
104 pub editable: bool,
106 pub choices: Option<Vec<(String, String)>>,
108}
109
110impl BaseField {
111 pub fn new() -> Self {
126 Self {
127 name: None,
128 null: false,
129 blank: false,
130 default: None,
131 db_default: None,
132 db_column: None,
133 db_tablespace: None,
134 primary_key: false,
135 unique: false,
136 editable: true,
137 choices: None,
138 }
139 }
140 pub fn get_kwargs(&self) -> HashMap<String, FieldKwarg> {
159 let mut kwargs = HashMap::new();
160
161 if self.null {
162 kwargs.insert("null".to_string(), FieldKwarg::Bool(true));
163 }
164 if self.blank {
165 kwargs.insert("blank".to_string(), FieldKwarg::Bool(true));
166 }
167 if let Some(ref default) = self.default {
168 kwargs.insert("default".to_string(), default.clone());
169 }
170 if let Some(ref db_default) = self.db_default {
171 kwargs.insert("db_default".to_string(), db_default.clone());
172 }
173 if let Some(ref db_column) = self.db_column {
174 kwargs.insert(
175 "db_column".to_string(),
176 FieldKwarg::String(db_column.clone()),
177 );
178 }
179 if let Some(ref db_tablespace) = self.db_tablespace {
180 kwargs.insert(
181 "db_tablespace".to_string(),
182 FieldKwarg::String(db_tablespace.clone()),
183 );
184 }
185 if self.primary_key {
186 kwargs.insert("primary_key".to_string(), FieldKwarg::Bool(true));
187 }
188 if self.unique {
189 kwargs.insert("unique".to_string(), FieldKwarg::Bool(true));
190 }
191 if !self.editable {
192 kwargs.insert("editable".to_string(), FieldKwarg::Bool(false));
193 }
194 if let Some(ref choices) = self.choices {
195 kwargs.insert("choices".to_string(), FieldKwarg::Choices(choices.clone()));
196 }
197
198 kwargs
199 }
200}
201
202impl Default for BaseField {
203 fn default() -> Self {
204 Self::new()
205 }
206}
207
208#[derive(Debug, Clone)]
210pub struct AutoField {
211 pub base: BaseField,
213}
214
215impl Default for AutoField {
216 fn default() -> Self {
217 Self::new()
218 }
219}
220
221impl AutoField {
222 pub fn new() -> Self {
235 let mut base = BaseField::new();
236 base.primary_key = true;
237 Self { base }
238 }
239}
240
241impl Field for AutoField {
242 fn deconstruct(&self) -> FieldDeconstruction {
243 let kwargs = self.base.get_kwargs();
244
245 FieldDeconstruction {
246 name: self.base.name.clone(),
247 path: "reinhardt.orm.models.AutoField".to_string(),
248 args: vec![],
249 kwargs,
250 }
251 }
252
253 fn set_attributes_from_name(&mut self, name: &str) {
254 self.base.name = Some(name.to_string());
255 }
256
257 fn name(&self) -> Option<&str> {
258 self.base.name.as_deref()
259 }
260
261 fn is_primary_key(&self) -> bool {
262 self.base.primary_key
263 }
264}
265
266#[derive(Debug, Clone)]
268pub struct BigIntegerField {
269 pub base: BaseField,
271}
272
273impl Default for BigIntegerField {
274 fn default() -> Self {
275 Self::new()
276 }
277}
278
279impl BigIntegerField {
280 pub fn new() -> Self {
293 Self {
294 base: BaseField::new(),
295 }
296 }
297}
298
299impl Field for BigIntegerField {
300 fn deconstruct(&self) -> FieldDeconstruction {
301 FieldDeconstruction {
302 name: self.base.name.clone(),
303 path: "reinhardt.orm.models.BigIntegerField".to_string(),
304 args: vec![],
305 kwargs: self.base.get_kwargs(),
306 }
307 }
308
309 fn set_attributes_from_name(&mut self, name: &str) {
310 self.base.name = Some(name.to_string());
311 }
312
313 fn name(&self) -> Option<&str> {
314 self.base.name.as_deref()
315 }
316}
317
318#[derive(Debug, Clone)]
320pub struct BooleanField {
321 pub base: BaseField,
323}
324
325impl Default for BooleanField {
326 fn default() -> Self {
327 Self::new()
328 }
329}
330
331impl BooleanField {
332 pub fn new() -> Self {
342 Self {
343 base: BaseField::new(),
344 }
345 }
346 pub fn with_default(default: bool) -> Self {
358 let mut field = Self::new();
359 field.base.default = Some(FieldKwarg::Bool(default));
360 field
361 }
362}
363
364impl Field for BooleanField {
365 fn deconstruct(&self) -> FieldDeconstruction {
366 FieldDeconstruction {
367 name: self.base.name.clone(),
368 path: "reinhardt.orm.models.BooleanField".to_string(),
369 args: vec![],
370 kwargs: self.base.get_kwargs(),
371 }
372 }
373
374 fn set_attributes_from_name(&mut self, name: &str) {
375 self.base.name = Some(name.to_string());
376 }
377
378 fn name(&self) -> Option<&str> {
379 self.base.name.as_deref()
380 }
381}
382
383#[derive(Debug, Clone)]
385pub struct CharField {
386 pub base: BaseField,
388 pub max_length: u64,
390}
391
392impl CharField {
393 pub fn new(max_length: u64) -> Self {
405 Self {
406 base: BaseField::new(),
407 max_length,
408 }
409 }
410 pub fn with_null_blank(max_length: u64) -> Self {
423 let mut base = BaseField::new();
424 base.null = true;
425 base.blank = true;
426 Self { base, max_length }
427 }
428 pub fn with_choices(max_length: u64, choices: Vec<(String, String)>) -> Self {
444 let mut base = BaseField::new();
445 base.choices = Some(choices);
446 Self { base, max_length }
447 }
448}
449
450impl Field for CharField {
451 fn deconstruct(&self) -> FieldDeconstruction {
452 let mut kwargs = self.base.get_kwargs();
453 kwargs.insert("max_length".to_string(), FieldKwarg::Uint(self.max_length));
454
455 FieldDeconstruction {
456 name: self.base.name.clone(),
457 path: "reinhardt.orm.models.CharField".to_string(),
458 args: vec![],
459 kwargs,
460 }
461 }
462
463 fn set_attributes_from_name(&mut self, name: &str) {
464 self.base.name = Some(name.to_string());
465 }
466
467 fn name(&self) -> Option<&str> {
468 self.base.name.as_deref()
469 }
470}
471
472#[derive(Debug, Clone)]
474pub struct IntegerField {
475 pub base: BaseField,
477}
478
479impl Default for IntegerField {
480 fn default() -> Self {
481 Self::new()
482 }
483}
484
485impl IntegerField {
486 pub fn new() -> Self {
497 Self {
498 base: BaseField::new(),
499 }
500 }
501 pub fn with_choices(choices: Vec<(String, String)>) -> Self {
515 let mut base = BaseField::new();
516 base.choices = Some(choices);
517 Self { base }
518 }
519 pub fn with_callable_choices(_callable_name: &str) -> Self {
530 Self::new()
534 }
535}
536
537impl Field for IntegerField {
538 fn deconstruct(&self) -> FieldDeconstruction {
539 FieldDeconstruction {
540 name: self.base.name.clone(),
541 path: "reinhardt.orm.models.IntegerField".to_string(),
542 args: vec![],
543 kwargs: self.base.get_kwargs(),
544 }
545 }
546
547 fn set_attributes_from_name(&mut self, name: &str) {
548 self.base.name = Some(name.to_string());
549 }
550
551 fn name(&self) -> Option<&str> {
552 self.base.name.as_deref()
553 }
554}
555
556#[derive(Debug, Clone)]
558pub struct DateField {
559 pub base: BaseField,
561 pub auto_now: bool,
563 pub auto_now_add: bool,
565}
566
567impl Default for DateField {
568 fn default() -> Self {
569 Self::new()
570 }
571}
572
573impl DateField {
574 pub fn new() -> Self {
586 Self {
587 base: BaseField::new(),
588 auto_now: false,
589 auto_now_add: false,
590 }
591 }
592 pub fn with_auto_now() -> Self {
604 Self {
605 base: BaseField::new(),
606 auto_now: true,
607 auto_now_add: false,
608 }
609 }
610}
611
612impl Field for DateField {
613 fn deconstruct(&self) -> FieldDeconstruction {
614 let mut kwargs = self.base.get_kwargs();
615 if self.auto_now {
616 kwargs.insert("auto_now".to_string(), FieldKwarg::Bool(true));
617 }
618 if self.auto_now_add {
619 kwargs.insert("auto_now_add".to_string(), FieldKwarg::Bool(true));
620 }
621
622 FieldDeconstruction {
623 name: self.base.name.clone(),
624 path: "reinhardt.orm.models.DateField".to_string(),
625 args: vec![],
626 kwargs,
627 }
628 }
629
630 fn set_attributes_from_name(&mut self, name: &str) {
631 self.base.name = Some(name.to_string());
632 }
633
634 fn name(&self) -> Option<&str> {
635 self.base.name.as_deref()
636 }
637}
638
639#[derive(Debug, Clone)]
641pub struct DateTimeField {
642 pub base: BaseField,
644 pub auto_now: bool,
646 pub auto_now_add: bool,
648}
649
650impl Default for DateTimeField {
651 fn default() -> Self {
652 Self::new()
653 }
654}
655
656impl DateTimeField {
657 pub fn new() -> Self {
669 Self {
670 base: BaseField::new(),
671 auto_now: false,
672 auto_now_add: false,
673 }
674 }
675 pub fn with_auto_now_add() -> Self {
687 Self {
688 base: BaseField::new(),
689 auto_now: false,
690 auto_now_add: true,
691 }
692 }
693 pub fn with_both() -> Self {
705 Self {
706 base: BaseField::new(),
707 auto_now: true,
708 auto_now_add: true,
709 }
710 }
711}
712
713impl Field for DateTimeField {
714 fn deconstruct(&self) -> FieldDeconstruction {
715 let mut kwargs = self.base.get_kwargs();
716 if self.auto_now {
717 kwargs.insert("auto_now".to_string(), FieldKwarg::Bool(true));
718 }
719 if self.auto_now_add {
720 kwargs.insert("auto_now_add".to_string(), FieldKwarg::Bool(true));
721 }
722
723 FieldDeconstruction {
724 name: self.base.name.clone(),
725 path: "reinhardt.orm.models.DateTimeField".to_string(),
726 args: vec![],
727 kwargs,
728 }
729 }
730
731 fn set_attributes_from_name(&mut self, name: &str) {
732 self.base.name = Some(name.to_string());
733 }
734
735 fn name(&self) -> Option<&str> {
736 self.base.name.as_deref()
737 }
738}
739
740#[derive(Debug, Clone)]
742pub struct DecimalField {
743 pub base: BaseField,
745 pub max_digits: u32,
747 pub decimal_places: u32,
749}
750
751impl DecimalField {
752 pub fn new(max_digits: u32, decimal_places: u32) -> Self {
765 Self {
766 base: BaseField::new(),
767 max_digits,
768 decimal_places,
769 }
770 }
771}
772
773impl Field for DecimalField {
774 fn deconstruct(&self) -> FieldDeconstruction {
775 let mut kwargs = self.base.get_kwargs();
776 kwargs.insert(
777 "max_digits".to_string(),
778 FieldKwarg::Uint(self.max_digits as u64),
779 );
780 kwargs.insert(
781 "decimal_places".to_string(),
782 FieldKwarg::Uint(self.decimal_places as u64),
783 );
784
785 FieldDeconstruction {
786 name: self.base.name.clone(),
787 path: "reinhardt.orm.models.DecimalField".to_string(),
788 args: vec![],
789 kwargs,
790 }
791 }
792
793 fn set_attributes_from_name(&mut self, name: &str) {
794 self.base.name = Some(name.to_string());
795 }
796
797 fn name(&self) -> Option<&str> {
798 self.base.name.as_deref()
799 }
800}
801
802#[derive(Debug, Clone)]
804pub struct EmailField {
805 pub base: BaseField,
807 pub max_length: u64,
809}
810
811impl Default for EmailField {
812 fn default() -> Self {
813 Self::new()
814 }
815}
816
817impl EmailField {
818 pub fn new() -> Self {
829 Self {
830 base: BaseField::new(),
831 max_length: 254, }
833 }
834 pub fn with_max_length(max_length: u64) -> Self {
845 Self {
846 base: BaseField::new(),
847 max_length,
848 }
849 }
850}
851
852impl Field for EmailField {
853 fn deconstruct(&self) -> FieldDeconstruction {
854 let mut kwargs = self.base.get_kwargs();
855 kwargs.insert("max_length".to_string(), FieldKwarg::Uint(self.max_length));
856
857 FieldDeconstruction {
858 name: self.base.name.clone(),
859 path: "reinhardt.orm.models.EmailField".to_string(),
860 args: vec![],
861 kwargs,
862 }
863 }
864
865 fn set_attributes_from_name(&mut self, name: &str) {
866 self.base.name = Some(name.to_string());
867 }
868
869 fn name(&self) -> Option<&str> {
870 self.base.name.as_deref()
871 }
872}
873
874#[derive(Debug, Clone)]
876pub struct FloatField {
877 pub base: BaseField,
879}
880
881impl Default for FloatField {
882 fn default() -> Self {
883 Self::new()
884 }
885}
886
887impl FloatField {
888 pub fn new() -> Self {
899 Self {
900 base: BaseField::new(),
901 }
902 }
903}
904
905impl Field for FloatField {
906 fn deconstruct(&self) -> FieldDeconstruction {
907 FieldDeconstruction {
908 name: self.base.name.clone(),
909 path: "reinhardt.orm.models.FloatField".to_string(),
910 args: vec![],
911 kwargs: self.base.get_kwargs(),
912 }
913 }
914
915 fn set_attributes_from_name(&mut self, name: &str) {
916 self.base.name = Some(name.to_string());
917 }
918
919 fn name(&self) -> Option<&str> {
920 self.base.name.as_deref()
921 }
922}
923
924#[derive(Debug, Clone)]
926pub struct TextField {
927 pub base: BaseField,
929}
930
931impl Default for TextField {
932 fn default() -> Self {
933 Self::new()
934 }
935}
936
937impl TextField {
938 pub fn new() -> Self {
950 Self {
951 base: BaseField::new(),
952 }
953 }
954}
955
956impl Field for TextField {
957 fn deconstruct(&self) -> FieldDeconstruction {
958 FieldDeconstruction {
959 name: self.base.name.clone(),
960 path: "reinhardt.orm.models.TextField".to_string(),
961 args: vec![],
962 kwargs: self.base.get_kwargs(),
963 }
964 }
965
966 fn set_attributes_from_name(&mut self, name: &str) {
967 self.base.name = Some(name.to_string());
968 }
969
970 fn name(&self) -> Option<&str> {
971 self.base.name.as_deref()
972 }
973}
974
975#[derive(Debug, Clone)]
977pub struct TimeField {
978 pub base: BaseField,
980 pub auto_now: bool,
982 pub auto_now_add: bool,
984}
985
986impl Default for TimeField {
987 fn default() -> Self {
988 Self::new()
989 }
990}
991
992impl TimeField {
993 pub fn new() -> Self {
1005 Self {
1006 base: BaseField::new(),
1007 auto_now: false,
1008 auto_now_add: false,
1009 }
1010 }
1011 pub fn with_auto_now() -> Self {
1013 Self {
1014 base: BaseField::new(),
1015 auto_now: true,
1016 auto_now_add: false,
1017 }
1018 }
1019 pub fn with_auto_now_add() -> Self {
1021 Self {
1022 base: BaseField::new(),
1023 auto_now: false,
1024 auto_now_add: true,
1025 }
1026 }
1027}
1028
1029impl Field for TimeField {
1030 fn deconstruct(&self) -> FieldDeconstruction {
1031 let mut kwargs = self.base.get_kwargs();
1032 if self.auto_now {
1033 kwargs.insert("auto_now".to_string(), FieldKwarg::Bool(true));
1034 }
1035 if self.auto_now_add {
1036 kwargs.insert("auto_now_add".to_string(), FieldKwarg::Bool(true));
1037 }
1038
1039 FieldDeconstruction {
1040 name: self.base.name.clone(),
1041 path: "reinhardt.orm.models.TimeField".to_string(),
1042 args: vec![],
1043 kwargs,
1044 }
1045 }
1046
1047 fn set_attributes_from_name(&mut self, name: &str) {
1048 self.base.name = Some(name.to_string());
1049 }
1050
1051 fn name(&self) -> Option<&str> {
1052 self.base.name.as_deref()
1053 }
1054}
1055
1056#[derive(Debug, Clone)]
1058pub struct URLField {
1059 pub base: BaseField,
1061 pub max_length: u64,
1063}
1064
1065impl Default for URLField {
1066 fn default() -> Self {
1067 Self::new()
1068 }
1069}
1070
1071impl URLField {
1072 pub fn new() -> Self {
1083 Self {
1084 base: BaseField::new(),
1085 max_length: 200, }
1087 }
1088 pub fn with_max_length(max_length: u64) -> Self {
1090 Self {
1091 base: BaseField::new(),
1092 max_length,
1093 }
1094 }
1095}
1096
1097impl Field for URLField {
1098 fn deconstruct(&self) -> FieldDeconstruction {
1099 let mut kwargs = self.base.get_kwargs();
1100 if self.max_length != 200 {
1102 kwargs.insert("max_length".to_string(), FieldKwarg::Uint(self.max_length));
1103 }
1104
1105 FieldDeconstruction {
1106 name: self.base.name.clone(),
1107 path: "reinhardt.orm.models.URLField".to_string(),
1108 args: vec![],
1109 kwargs,
1110 }
1111 }
1112
1113 fn set_attributes_from_name(&mut self, name: &str) {
1114 self.base.name = Some(name.to_string());
1115 }
1116
1117 fn name(&self) -> Option<&str> {
1118 self.base.name.as_deref()
1119 }
1120}
1121
1122#[derive(Debug, Clone)]
1124pub struct BinaryField {
1125 pub base: BaseField,
1127}
1128
1129impl Default for BinaryField {
1130 fn default() -> Self {
1131 Self::new()
1132 }
1133}
1134
1135impl BinaryField {
1136 pub fn new() -> Self {
1147 let mut base = BaseField::new();
1148 base.editable = false; Self { base }
1150 }
1151 pub fn with_editable() -> Self {
1162 let mut base = BaseField::new();
1163 base.editable = true;
1164 Self { base }
1165 }
1166}
1167
1168impl Field for BinaryField {
1169 fn deconstruct(&self) -> FieldDeconstruction {
1170 let mut kwargs = self.base.get_kwargs();
1171 kwargs.remove("editable");
1174 if self.base.editable {
1175 kwargs.insert("editable".to_string(), FieldKwarg::Bool(true));
1176 }
1177
1178 FieldDeconstruction {
1179 name: self.base.name.clone(),
1180 path: "reinhardt.orm.models.BinaryField".to_string(),
1181 args: vec![],
1182 kwargs,
1183 }
1184 }
1185
1186 fn set_attributes_from_name(&mut self, name: &str) {
1187 self.base.name = Some(name.to_string());
1188 }
1189
1190 fn name(&self) -> Option<&str> {
1191 self.base.name.as_deref()
1192 }
1193}
1194
1195#[derive(Debug, Clone)]
1197pub struct SlugField {
1198 pub base: BaseField,
1200 pub max_length: u64,
1202 pub db_index: bool,
1204}
1205
1206impl Default for SlugField {
1207 fn default() -> Self {
1208 Self::new()
1209 }
1210}
1211
1212impl SlugField {
1213 pub fn new() -> Self {
1225 Self {
1226 base: BaseField::new(),
1227 max_length: 50, db_index: true, }
1230 }
1231 pub fn with_options(max_length: u64, db_index: bool) -> Self {
1233 Self {
1234 base: BaseField::new(),
1235 max_length,
1236 db_index,
1237 }
1238 }
1239}
1240
1241impl Field for SlugField {
1242 fn deconstruct(&self) -> FieldDeconstruction {
1243 let mut kwargs = self.base.get_kwargs();
1244 if self.max_length != 50 {
1246 kwargs.insert("max_length".to_string(), FieldKwarg::Uint(self.max_length));
1247 }
1248 if !self.db_index {
1249 kwargs.insert("db_index".to_string(), FieldKwarg::Bool(false));
1250 }
1251
1252 FieldDeconstruction {
1253 name: self.base.name.clone(),
1254 path: "reinhardt.orm.models.SlugField".to_string(),
1255 args: vec![],
1256 kwargs,
1257 }
1258 }
1259
1260 fn set_attributes_from_name(&mut self, name: &str) {
1261 self.base.name = Some(name.to_string());
1262 }
1263
1264 fn name(&self) -> Option<&str> {
1265 self.base.name.as_deref()
1266 }
1267}
1268
1269#[derive(Debug, Clone)]
1271pub struct SmallIntegerField {
1272 pub base: BaseField,
1274}
1275
1276impl Default for SmallIntegerField {
1277 fn default() -> Self {
1278 Self::new()
1279 }
1280}
1281
1282impl SmallIntegerField {
1283 pub fn new() -> Self {
1294 Self {
1295 base: BaseField::new(),
1296 }
1297 }
1298}
1299
1300impl Field for SmallIntegerField {
1301 fn deconstruct(&self) -> FieldDeconstruction {
1302 FieldDeconstruction {
1303 name: self.base.name.clone(),
1304 path: "reinhardt.orm.models.SmallIntegerField".to_string(),
1305 args: vec![],
1306 kwargs: self.base.get_kwargs(),
1307 }
1308 }
1309
1310 fn set_attributes_from_name(&mut self, name: &str) {
1311 self.base.name = Some(name.to_string());
1312 }
1313
1314 fn name(&self) -> Option<&str> {
1315 self.base.name.as_deref()
1316 }
1317}
1318
1319#[derive(Debug, Clone)]
1321pub struct PositiveIntegerField {
1322 pub base: BaseField,
1324}
1325
1326impl Default for PositiveIntegerField {
1327 fn default() -> Self {
1328 Self::new()
1329 }
1330}
1331
1332impl PositiveIntegerField {
1333 pub fn new() -> Self {
1344 Self {
1345 base: BaseField::new(),
1346 }
1347 }
1348}
1349
1350impl Field for PositiveIntegerField {
1351 fn deconstruct(&self) -> FieldDeconstruction {
1352 FieldDeconstruction {
1353 name: self.base.name.clone(),
1354 path: "reinhardt.orm.models.PositiveIntegerField".to_string(),
1355 args: vec![],
1356 kwargs: self.base.get_kwargs(),
1357 }
1358 }
1359
1360 fn set_attributes_from_name(&mut self, name: &str) {
1361 self.base.name = Some(name.to_string());
1362 }
1363
1364 fn name(&self) -> Option<&str> {
1365 self.base.name.as_deref()
1366 }
1367}
1368
1369#[derive(Debug, Clone)]
1371pub struct PositiveSmallIntegerField {
1372 pub base: BaseField,
1374}
1375
1376impl Default for PositiveSmallIntegerField {
1377 fn default() -> Self {
1378 Self::new()
1379 }
1380}
1381
1382impl PositiveSmallIntegerField {
1383 pub fn new() -> Self {
1394 Self {
1395 base: BaseField::new(),
1396 }
1397 }
1398}
1399
1400impl Field for PositiveSmallIntegerField {
1401 fn deconstruct(&self) -> FieldDeconstruction {
1402 FieldDeconstruction {
1403 name: self.base.name.clone(),
1404 path: "reinhardt.orm.models.PositiveSmallIntegerField".to_string(),
1405 args: vec![],
1406 kwargs: self.base.get_kwargs(),
1407 }
1408 }
1409
1410 fn set_attributes_from_name(&mut self, name: &str) {
1411 self.base.name = Some(name.to_string());
1412 }
1413
1414 fn name(&self) -> Option<&str> {
1415 self.base.name.as_deref()
1416 }
1417}
1418
1419#[derive(Debug, Clone)]
1421pub struct PositiveBigIntegerField {
1422 pub base: BaseField,
1424}
1425
1426impl Default for PositiveBigIntegerField {
1427 fn default() -> Self {
1428 Self::new()
1429 }
1430}
1431
1432impl PositiveBigIntegerField {
1433 pub fn new() -> Self {
1444 Self {
1445 base: BaseField::new(),
1446 }
1447 }
1448}
1449
1450impl Field for PositiveBigIntegerField {
1451 fn deconstruct(&self) -> FieldDeconstruction {
1452 FieldDeconstruction {
1453 name: self.base.name.clone(),
1454 path: "reinhardt.orm.models.PositiveBigIntegerField".to_string(),
1455 args: vec![],
1456 kwargs: self.base.get_kwargs(),
1457 }
1458 }
1459
1460 fn set_attributes_from_name(&mut self, name: &str) {
1461 self.base.name = Some(name.to_string());
1462 }
1463
1464 fn name(&self) -> Option<&str> {
1465 self.base.name.as_deref()
1466 }
1467}
1468
1469#[derive(Debug, Clone)]
1471pub struct GenericIPAddressField {
1472 pub base: BaseField,
1474 pub protocol: String, pub unpack_ipv4: bool,
1478}
1479
1480impl Default for GenericIPAddressField {
1481 fn default() -> Self {
1482 Self::new()
1483 }
1484}
1485
1486impl GenericIPAddressField {
1487 pub fn new() -> Self {
1499 Self {
1500 base: BaseField::new(),
1501 protocol: "both".to_string(),
1502 unpack_ipv4: false,
1503 }
1504 }
1505 pub fn ipv4_only() -> Self {
1508 Self {
1509 base: BaseField::new(),
1510 protocol: "IPv4".to_string(),
1511 unpack_ipv4: false,
1512 }
1513 }
1514 pub fn ipv6_only() -> Self {
1517 Self {
1518 base: BaseField::new(),
1519 protocol: "IPv6".to_string(),
1520 unpack_ipv4: false,
1521 }
1522 }
1523}
1524
1525impl Field for GenericIPAddressField {
1526 fn deconstruct(&self) -> FieldDeconstruction {
1527 let mut kwargs = self.base.get_kwargs();
1528
1529 if self.protocol != "both" {
1531 kwargs.insert(
1532 "protocol".to_string(),
1533 FieldKwarg::String(self.protocol.clone()),
1534 );
1535 }
1536 if self.unpack_ipv4 {
1537 kwargs.insert("unpack_ipv4".to_string(), FieldKwarg::Bool(true));
1538 }
1539
1540 FieldDeconstruction {
1541 name: self.base.name.clone(),
1542 path: "reinhardt.orm.models.GenericIPAddressField".to_string(),
1543 args: vec![],
1544 kwargs,
1545 }
1546 }
1547
1548 fn set_attributes_from_name(&mut self, name: &str) {
1549 self.base.name = Some(name.to_string());
1550 }
1551
1552 fn name(&self) -> Option<&str> {
1553 self.base.name.as_deref()
1554 }
1555}
1556
1557#[derive(Debug, Clone)]
1559pub struct FilePathField {
1560 pub base: BaseField,
1562 pub path: String,
1564 pub match_pattern: Option<String>,
1566 pub recursive: bool,
1568 pub allow_files: bool,
1570 pub allow_folders: bool,
1572 pub max_length: u64,
1574}
1575
1576impl FilePathField {
1577 pub fn new(path: String) -> Self {
1592 Self {
1593 base: BaseField::new(),
1594 path,
1595 match_pattern: None,
1596 recursive: false,
1597 allow_files: true,
1598 allow_folders: false,
1599 max_length: 100,
1600 }
1601 }
1602}
1603
1604impl Field for FilePathField {
1605 fn deconstruct(&self) -> FieldDeconstruction {
1606 let mut kwargs = self.base.get_kwargs();
1607
1608 kwargs.insert("path".to_string(), FieldKwarg::String(self.path.clone()));
1609
1610 if let Some(ref pattern) = self.match_pattern {
1611 kwargs.insert("match".to_string(), FieldKwarg::String(pattern.clone()));
1612 }
1613 if self.recursive {
1614 kwargs.insert("recursive".to_string(), FieldKwarg::Bool(true));
1615 }
1616 if !self.allow_files {
1617 kwargs.insert("allow_files".to_string(), FieldKwarg::Bool(false));
1618 }
1619 if self.allow_folders {
1620 kwargs.insert("allow_folders".to_string(), FieldKwarg::Bool(true));
1621 }
1622 if self.max_length != 100 {
1623 kwargs.insert("max_length".to_string(), FieldKwarg::Uint(self.max_length));
1624 }
1625
1626 FieldDeconstruction {
1627 name: self.base.name.clone(),
1628 path: "reinhardt.orm.models.FilePathField".to_string(),
1629 args: vec![],
1630 kwargs,
1631 }
1632 }
1633
1634 fn set_attributes_from_name(&mut self, name: &str) {
1635 self.base.name = Some(name.to_string());
1636 }
1637
1638 fn name(&self) -> Option<&str> {
1639 self.base.name.as_deref()
1640 }
1641}
1642
1643#[derive(Debug, Clone)]
1645pub struct ForeignKey {
1646 pub base: BaseField,
1648 pub to: String, pub on_delete: String, pub related_name: Option<String>,
1654}
1655
1656impl ForeignKey {
1657 pub fn new(to: String, on_delete: String) -> Self {
1670 Self {
1671 base: BaseField::new(),
1672 to,
1673 on_delete,
1674 related_name: None,
1675 }
1676 }
1677}
1678
1679impl Field for ForeignKey {
1680 fn deconstruct(&self) -> FieldDeconstruction {
1681 let mut kwargs = self.base.get_kwargs();
1682
1683 kwargs.insert("to".to_string(), FieldKwarg::String(self.to.to_lowercase()));
1685 kwargs.insert(
1686 "on_delete".to_string(),
1687 FieldKwarg::String(self.on_delete.clone()),
1688 );
1689
1690 if let Some(ref name) = self.related_name {
1691 kwargs.insert("related_name".to_string(), FieldKwarg::String(name.clone()));
1692 }
1693
1694 FieldDeconstruction {
1695 name: self.base.name.clone(),
1696 path: "reinhardt.orm.models.ForeignKey".to_string(),
1697 args: vec![],
1698 kwargs,
1699 }
1700 }
1701
1702 fn set_attributes_from_name(&mut self, name: &str) {
1703 self.base.name = Some(name.to_string());
1704 }
1705
1706 fn name(&self) -> Option<&str> {
1707 self.base.name.as_deref()
1708 }
1709}
1710
1711#[derive(Debug, Clone)]
1713pub struct OneToOneField {
1714 pub base: BaseField,
1716 pub to: String,
1718 pub on_delete: String,
1720 pub related_name: Option<String>,
1722}
1723
1724impl OneToOneField {
1725 pub fn new(to: String, on_delete: String) -> Self {
1738 Self {
1739 base: BaseField::new(),
1740 to,
1741 on_delete,
1742 related_name: None,
1743 }
1744 }
1745}
1746
1747impl Field for OneToOneField {
1748 fn deconstruct(&self) -> FieldDeconstruction {
1749 let mut kwargs = self.base.get_kwargs();
1750
1751 kwargs.insert("to".to_string(), FieldKwarg::String(self.to.to_lowercase()));
1752 kwargs.insert(
1753 "on_delete".to_string(),
1754 FieldKwarg::String(self.on_delete.clone()),
1755 );
1756
1757 if let Some(ref name) = self.related_name {
1758 kwargs.insert("related_name".to_string(), FieldKwarg::String(name.clone()));
1759 }
1760
1761 FieldDeconstruction {
1762 name: self.base.name.clone(),
1763 path: "reinhardt.orm.models.OneToOneField".to_string(),
1764 args: vec![],
1765 kwargs,
1766 }
1767 }
1768
1769 fn set_attributes_from_name(&mut self, name: &str) {
1770 self.base.name = Some(name.to_string());
1771 }
1772
1773 fn name(&self) -> Option<&str> {
1774 self.base.name.as_deref()
1775 }
1776}
1777
1778#[derive(Debug, Clone)]
1780pub struct ManyToManyField {
1781 pub base: BaseField,
1783 pub to: String,
1785 pub related_name: Option<String>,
1787 pub through: Option<String>,
1789}
1790
1791impl ManyToManyField {
1792 pub fn new(to: String) -> Self {
1805 Self {
1806 base: BaseField::new(),
1807 to,
1808 related_name: None,
1809 through: None,
1810 }
1811 }
1812 pub fn with_related_name(to: String, related_name: String) -> Self {
1814 Self {
1815 base: BaseField::new(),
1816 to,
1817 related_name: Some(related_name),
1818 through: None,
1819 }
1820 }
1821}
1822
1823impl Field for ManyToManyField {
1824 fn deconstruct(&self) -> FieldDeconstruction {
1825 let mut kwargs = self.base.get_kwargs();
1826
1827 kwargs.insert("to".to_string(), FieldKwarg::String(self.to.to_lowercase()));
1828
1829 if let Some(ref name) = self.related_name {
1830 kwargs.insert("related_name".to_string(), FieldKwarg::String(name.clone()));
1831 }
1832
1833 if let Some(ref through) = self.through {
1834 kwargs.insert("through".to_string(), FieldKwarg::String(through.clone()));
1835 }
1836
1837 FieldDeconstruction {
1838 name: self.base.name.clone(),
1839 path: "reinhardt.orm.models.ManyToManyField".to_string(),
1840 args: vec![],
1841 kwargs,
1842 }
1843 }
1844
1845 fn set_attributes_from_name(&mut self, name: &str) {
1846 self.base.name = Some(name.to_string());
1847 }
1848
1849 fn name(&self) -> Option<&str> {
1850 self.base.name.as_deref()
1851 }
1852}
1853
1854#[cfg(test)]
1855mod tests {
1856 use super::*;
1857
1858 #[test]
1859 fn test_auto_field_deconstruct() {
1860 let mut field = AutoField::new();
1861 field.set_attributes_from_name("id");
1862 let dec = field.deconstruct();
1863
1864 assert_eq!(dec.name, Some("id".to_string()));
1865 assert_eq!(dec.path, "reinhardt.orm.models.AutoField");
1866 assert_eq!(dec.args.len(), 0);
1867 assert_eq!(dec.kwargs.get("primary_key"), Some(&FieldKwarg::Bool(true)));
1868 }
1869
1870 #[test]
1871 fn test_big_integer_field_deconstruct() {
1872 let field = BigIntegerField::new();
1873 let dec = field.deconstruct();
1874
1875 assert_eq!(dec.path, "reinhardt.orm.models.BigIntegerField");
1876 assert_eq!(dec.args.len(), 0);
1877 assert!(dec.kwargs.is_empty());
1878 }
1879
1880 #[test]
1881 fn test_boolean_field_deconstruct() {
1882 let field = BooleanField::new();
1883 let dec = field.deconstruct();
1884
1885 assert_eq!(dec.path, "reinhardt.orm.models.BooleanField");
1886 assert!(dec.kwargs.is_empty());
1887
1888 let field_with_default = BooleanField::with_default(true);
1889 let dec2 = field_with_default.deconstruct();
1890 assert_eq!(dec2.kwargs.get("default"), Some(&FieldKwarg::Bool(true)));
1891 }
1892
1893 #[test]
1894 fn test_char_field_deconstruct() {
1895 let field = CharField::new(65);
1896 let dec = field.deconstruct();
1897
1898 assert_eq!(dec.path, "reinhardt.orm.models.CharField");
1899 assert_eq!(dec.kwargs.get("max_length"), Some(&FieldKwarg::Uint(65)));
1900
1901 let field2 = CharField::with_null_blank(65);
1902 let dec2 = field2.deconstruct();
1903 assert_eq!(dec2.kwargs.get("null"), Some(&FieldKwarg::Bool(true)));
1904 assert_eq!(dec2.kwargs.get("blank"), Some(&FieldKwarg::Bool(true)));
1905 }
1906
1907 #[test]
1908 fn test_char_field_choices() {
1909 let choices = vec![
1910 ("A".to_string(), "One".to_string()),
1911 ("B".to_string(), "Two".to_string()),
1912 ];
1913 let field = CharField::with_choices(1, choices.clone());
1914 let dec = field.deconstruct();
1915
1916 assert_eq!(
1917 dec.kwargs.get("choices"),
1918 Some(&FieldKwarg::Choices(choices))
1919 );
1920 assert_eq!(dec.kwargs.get("max_length"), Some(&FieldKwarg::Uint(1)));
1921 }
1922
1923 #[test]
1924 fn test_date_field_deconstruct() {
1925 let field = DateField::new();
1926 let dec = field.deconstruct();
1927 assert_eq!(dec.path, "reinhardt.orm.models.DateField");
1928 assert!(dec.kwargs.is_empty());
1929
1930 let field2 = DateField::with_auto_now();
1931 let dec2 = field2.deconstruct();
1932 assert_eq!(dec2.kwargs.get("auto_now"), Some(&FieldKwarg::Bool(true)));
1933 }
1934
1935 #[test]
1936 fn test_datetime_field_deconstruct() {
1937 let field = DateTimeField::new();
1938 let dec = field.deconstruct();
1939 assert!(dec.kwargs.is_empty());
1940
1941 let field2 = DateTimeField::with_auto_now_add();
1942 let dec2 = field2.deconstruct();
1943 assert_eq!(
1944 dec2.kwargs.get("auto_now_add"),
1945 Some(&FieldKwarg::Bool(true))
1946 );
1947
1948 let field3 = DateTimeField::with_both();
1949 let dec3 = field3.deconstruct();
1950 assert_eq!(dec3.kwargs.get("auto_now"), Some(&FieldKwarg::Bool(true)));
1951 assert_eq!(
1952 dec3.kwargs.get("auto_now_add"),
1953 Some(&FieldKwarg::Bool(true))
1954 );
1955 }
1956
1957 #[test]
1958 fn test_decimal_field_deconstruct() {
1959 let field = DecimalField::new(5, 2);
1960 let dec = field.deconstruct();
1961
1962 assert_eq!(dec.path, "reinhardt.orm.models.DecimalField");
1963 assert_eq!(dec.kwargs.get("max_digits"), Some(&FieldKwarg::Uint(5)));
1964 assert_eq!(dec.kwargs.get("decimal_places"), Some(&FieldKwarg::Uint(2)));
1965 }
1966
1967 #[test]
1968 fn test_email_field_deconstruct() {
1969 let field = EmailField::new();
1970 let dec = field.deconstruct();
1971
1972 assert_eq!(dec.path, "reinhardt.orm.models.EmailField");
1973 assert_eq!(dec.kwargs.get("max_length"), Some(&FieldKwarg::Uint(254)));
1974
1975 let field2 = EmailField::with_max_length(255);
1976 let dec2 = field2.deconstruct();
1977 assert_eq!(dec2.kwargs.get("max_length"), Some(&FieldKwarg::Uint(255)));
1978 }
1979}