1pub mod mysql;
2pub mod postgres;
3pub mod sqlite;
4
5use std::collections::HashMap;
6
7use crate::cli::DatabaseKind;
8use crate::introspect::{ColumnInfo, SchemaInfo};
9
10#[derive(Debug, Clone)]
12pub struct RustType {
13 pub path: String,
14 pub needs_import: Option<String>,
15}
16
17impl RustType {
18 pub fn simple(path: &str) -> Self {
19 Self {
20 path: path.to_string(),
21 needs_import: None,
22 }
23 }
24
25 pub fn with_import(path: &str, import: &str) -> Self {
26 Self {
27 path: path.to_string(),
28 needs_import: Some(import.to_string()),
29 }
30 }
31
32 pub fn wrap_option(self) -> Self {
33 Self {
34 path: format!("Option<{}>", self.path),
35 needs_import: self.needs_import,
36 }
37 }
38
39 pub fn wrap_vec(self) -> Self {
40 Self {
41 path: format!("Vec<{}>", self.path),
42 needs_import: self.needs_import,
43 }
44 }
45}
46
47pub fn map_column(
48 col: &ColumnInfo,
49 db_kind: DatabaseKind,
50 schema_info: &SchemaInfo,
51 overrides: &HashMap<String, String>,
52) -> RustType {
53 if let Some(override_type) = overrides.get(&col.udt_name) {
55 let rt = RustType::simple(override_type);
56 return if col.is_nullable { rt.wrap_option() } else { rt };
57 }
58
59 let base = match db_kind {
60 DatabaseKind::Postgres => postgres::map_type(&col.udt_name, schema_info),
61 DatabaseKind::Mysql => mysql::map_type(&col.data_type, &col.udt_name),
62 DatabaseKind::Sqlite => sqlite::map_type(&col.udt_name),
63 };
64
65 if col.is_nullable {
66 base.wrap_option()
67 } else {
68 base
69 }
70}
71
72#[cfg(test)]
73mod tests {
74 use super::*;
75 use crate::introspect::SchemaInfo;
76 use std::collections::HashMap;
77
78 fn make_col(udt_name: &str, data_type: &str, nullable: bool) -> ColumnInfo {
79 ColumnInfo {
80 name: "test".to_string(),
81 data_type: data_type.to_string(),
82 udt_name: udt_name.to_string(),
83 is_nullable: nullable,
84 ordinal_position: 0,
85 schema_name: "public".to_string(),
86 }
87 }
88
89 #[test]
92 fn test_simple_creates_without_import() {
93 let rt = RustType::simple("i32");
94 assert_eq!(rt.path, "i32");
95 assert!(rt.needs_import.is_none());
96 }
97
98 #[test]
99 fn test_simple_path_correct() {
100 let rt = RustType::simple("String");
101 assert_eq!(rt.path, "String");
102 }
103
104 #[test]
105 fn test_simple_no_import() {
106 let rt = RustType::simple("bool");
107 assert_eq!(rt.needs_import, None);
108 }
109
110 #[test]
113 fn test_with_import_creates_with_import() {
114 let rt = RustType::with_import("Uuid", "use uuid::Uuid;");
115 assert_eq!(rt.path, "Uuid");
116 assert_eq!(rt.needs_import, Some("use uuid::Uuid;".to_string()));
117 }
118
119 #[test]
120 fn test_with_import_path_correct() {
121 let rt = RustType::with_import("DateTime<Utc>", "use chrono::{DateTime, Utc};");
122 assert_eq!(rt.path, "DateTime<Utc>");
123 }
124
125 #[test]
126 fn test_with_import_import_present() {
127 let rt = RustType::with_import("Value", "use serde_json::Value;");
128 assert!(rt.needs_import.is_some());
129 }
130
131 #[test]
134 fn test_wrap_option_wraps_path() {
135 let rt = RustType::simple("i32").wrap_option();
136 assert_eq!(rt.path, "Option<i32>");
137 }
138
139 #[test]
140 fn test_wrap_option_preserves_import() {
141 let rt = RustType::with_import("Uuid", "use uuid::Uuid;").wrap_option();
142 assert_eq!(rt.path, "Option<Uuid>");
143 assert_eq!(rt.needs_import, Some("use uuid::Uuid;".to_string()));
144 }
145
146 #[test]
147 fn test_wrap_option_double_wrap() {
148 let rt = RustType::simple("i32").wrap_option().wrap_option();
149 assert_eq!(rt.path, "Option<Option<i32>>");
150 }
151
152 #[test]
155 fn test_wrap_vec_wraps_path() {
156 let rt = RustType::simple("i32").wrap_vec();
157 assert_eq!(rt.path, "Vec<i32>");
158 }
159
160 #[test]
161 fn test_wrap_vec_preserves_import() {
162 let rt = RustType::with_import("Uuid", "use uuid::Uuid;").wrap_vec();
163 assert_eq!(rt.path, "Vec<Uuid>");
164 assert_eq!(rt.needs_import, Some("use uuid::Uuid;".to_string()));
165 }
166
167 #[test]
170 fn test_override_takes_precedence() {
171 let col = make_col("uuid", "uuid", false);
172 let schema = SchemaInfo::default();
173 let mut overrides = HashMap::new();
174 overrides.insert("uuid".to_string(), "MyUuid".to_string());
175 let rt = map_column(&col, DatabaseKind::Postgres, &schema, &overrides);
176 assert_eq!(rt.path, "MyUuid");
177 assert!(rt.needs_import.is_none());
178 }
179
180 #[test]
181 fn test_override_with_nullable() {
182 let col = make_col("uuid", "uuid", true);
183 let schema = SchemaInfo::default();
184 let mut overrides = HashMap::new();
185 overrides.insert("uuid".to_string(), "MyUuid".to_string());
186 let rt = map_column(&col, DatabaseKind::Postgres, &schema, &overrides);
187 assert_eq!(rt.path, "Option<MyUuid>");
188 }
189
190 #[test]
191 fn test_no_override_dispatches_postgres() {
192 let col = make_col("int4", "integer", false);
193 let schema = SchemaInfo::default();
194 let overrides = HashMap::new();
195 let rt = map_column(&col, DatabaseKind::Postgres, &schema, &overrides);
196 assert_eq!(rt.path, "i32");
197 }
198
199 #[test]
200 fn test_nullable_without_override() {
201 let col = make_col("int4", "integer", true);
202 let schema = SchemaInfo::default();
203 let overrides = HashMap::new();
204 let rt = map_column(&col, DatabaseKind::Postgres, &schema, &overrides);
205 assert_eq!(rt.path, "Option<i32>");
206 }
207}
208