rustf 1.0.0

A simple MVC web framework inspired by Total.js
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! Type registry for database type mappings
//!
//! This module provides a centralized registry of type mappings between
//! Rust types, SQL types, and database-specific types.

use super::value::SqlValue;
use std::collections::HashMap;

/// Rust type identifier
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum RustType {
    Bool,
    I8,
    I16,
    I32,
    I64,
    U8,
    U16,
    U32,
    U64,
    F32,
    F64,
    String,
    Vec(Box<RustType>),
    Option(Box<RustType>),
    Json,
    Uuid,
    DateTime,
    Date,
    Time,
    Decimal,
}

/// SQL standard type
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum SqlType {
    // Boolean
    Boolean,

    // Numeric
    TinyInt,
    SmallInt,
    Integer,
    BigInt,
    Real,
    DoublePrecision,
    Decimal(Option<u8>, Option<u8>), // precision, scale
    Numeric(Option<u8>, Option<u8>),

    // Character
    Char(Option<u32>),
    VarChar(Option<u32>),
    Text,

    // Binary
    Binary(Option<u32>),
    VarBinary(Option<u32>),
    Blob,

    // Temporal
    Date,
    Time,
    Timestamp,
    DateTime,

    // Other
    Json,
    Uuid,
    Enum(Vec<String>),
}

/// Database-specific type information
#[derive(Debug, Clone)]
pub struct DatabaseTypeInfo {
    pub postgres_type: String,
    pub mysql_type: String,
    pub sqlite_type: String,
    pub sql_standard: SqlType,
}

/// Type mapping information
#[derive(Debug, Clone)]
pub struct TypeMapping {
    pub rust_type: RustType,
    pub sql_type: SqlType,
    pub database_info: DatabaseTypeInfo,
    pub nullable: bool,
    pub default_value: Option<SqlValue>,
}

/// Central type registry
pub struct TypeRegistry {
    mappings: HashMap<RustType, TypeMapping>,
    sql_to_rust: HashMap<SqlType, RustType>,
    postgres_types: HashMap<String, RustType>,
    mysql_types: HashMap<String, RustType>,
    sqlite_types: HashMap<String, RustType>,
}

impl TypeRegistry {
    /// Create a new type registry with default mappings
    pub fn new() -> Self {
        let mut registry = TypeRegistry {
            mappings: HashMap::new(),
            sql_to_rust: HashMap::new(),
            postgres_types: HashMap::new(),
            mysql_types: HashMap::new(),
            sqlite_types: HashMap::new(),
        };

        registry.initialize_default_mappings();
        registry
    }

    /// Initialize default type mappings
    fn initialize_default_mappings(&mut self) {
        // Boolean
        self.register_type(
            RustType::Bool,
            SqlType::Boolean,
            DatabaseTypeInfo {
                postgres_type: "BOOLEAN".to_string(),
                mysql_type: "BOOLEAN".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::Boolean,
            },
        );

        // Small integers
        self.register_type(
            RustType::I8,
            SqlType::TinyInt,
            DatabaseTypeInfo {
                postgres_type: "SMALLINT".to_string(), // PostgreSQL doesn't have TINYINT
                mysql_type: "TINYINT".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::TinyInt,
            },
        );

        self.register_type(
            RustType::I16,
            SqlType::SmallInt,
            DatabaseTypeInfo {
                postgres_type: "SMALLINT".to_string(),
                mysql_type: "SMALLINT".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::SmallInt,
            },
        );

        // Standard integers
        self.register_type(
            RustType::I32,
            SqlType::Integer,
            DatabaseTypeInfo {
                postgres_type: "INTEGER".to_string(),
                mysql_type: "INT".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::Integer,
            },
        );

        self.register_type(
            RustType::I64,
            SqlType::BigInt,
            DatabaseTypeInfo {
                postgres_type: "BIGINT".to_string(),
                mysql_type: "BIGINT".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::BigInt,
            },
        );

        // Unsigned integers
        self.register_type(
            RustType::U8,
            SqlType::TinyInt,
            DatabaseTypeInfo {
                postgres_type: "SMALLINT".to_string(),
                mysql_type: "TINYINT UNSIGNED".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::TinyInt,
            },
        );

        self.register_type(
            RustType::U16,
            SqlType::SmallInt,
            DatabaseTypeInfo {
                postgres_type: "INTEGER".to_string(),
                mysql_type: "SMALLINT UNSIGNED".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::SmallInt,
            },
        );

        self.register_type(
            RustType::U32,
            SqlType::Integer,
            DatabaseTypeInfo {
                postgres_type: "BIGINT".to_string(),
                mysql_type: "INT UNSIGNED".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::Integer,
            },
        );

        self.register_type(
            RustType::U64,
            SqlType::BigInt,
            DatabaseTypeInfo {
                postgres_type: "NUMERIC(20,0)".to_string(),
                mysql_type: "BIGINT UNSIGNED".to_string(),
                sqlite_type: "INTEGER".to_string(),
                sql_standard: SqlType::BigInt,
            },
        );

        // Floating point
        self.register_type(
            RustType::F32,
            SqlType::Real,
            DatabaseTypeInfo {
                postgres_type: "REAL".to_string(),
                mysql_type: "FLOAT".to_string(),
                sqlite_type: "REAL".to_string(),
                sql_standard: SqlType::Real,
            },
        );

        self.register_type(
            RustType::F64,
            SqlType::DoublePrecision,
            DatabaseTypeInfo {
                postgres_type: "DOUBLE PRECISION".to_string(),
                mysql_type: "DOUBLE".to_string(),
                sqlite_type: "REAL".to_string(),
                sql_standard: SqlType::DoublePrecision,
            },
        );

        // String
        self.register_type(
            RustType::String,
            SqlType::Text,
            DatabaseTypeInfo {
                postgres_type: "TEXT".to_string(),
                mysql_type: "TEXT".to_string(),
                sqlite_type: "TEXT".to_string(),
                sql_standard: SqlType::Text,
            },
        );

        // JSON
        self.register_type(
            RustType::Json,
            SqlType::Json,
            DatabaseTypeInfo {
                postgres_type: "JSONB".to_string(),
                mysql_type: "JSON".to_string(),
                sqlite_type: "TEXT".to_string(),
                sql_standard: SqlType::Json,
            },
        );

        // UUID
        self.register_type(
            RustType::Uuid,
            SqlType::Uuid,
            DatabaseTypeInfo {
                postgres_type: "UUID".to_string(),
                mysql_type: "CHAR(36)".to_string(),
                sqlite_type: "TEXT".to_string(),
                sql_standard: SqlType::Uuid,
            },
        );

        // Temporal types
        self.register_type(
            RustType::DateTime,
            SqlType::Timestamp,
            DatabaseTypeInfo {
                postgres_type: "TIMESTAMP WITH TIME ZONE".to_string(),
                mysql_type: "DATETIME".to_string(),
                sqlite_type: "TEXT".to_string(),
                sql_standard: SqlType::Timestamp,
            },
        );

        self.register_type(
            RustType::Date,
            SqlType::Date,
            DatabaseTypeInfo {
                postgres_type: "DATE".to_string(),
                mysql_type: "DATE".to_string(),
                sqlite_type: "TEXT".to_string(),
                sql_standard: SqlType::Date,
            },
        );

        self.register_type(
            RustType::Time,
            SqlType::Time,
            DatabaseTypeInfo {
                postgres_type: "TIME".to_string(),
                mysql_type: "TIME".to_string(),
                sqlite_type: "TEXT".to_string(),
                sql_standard: SqlType::Time,
            },
        );

        // Decimal
        self.register_type(
            RustType::Decimal,
            SqlType::Decimal(None, None),
            DatabaseTypeInfo {
                postgres_type: "NUMERIC".to_string(),
                mysql_type: "DECIMAL".to_string(),
                sqlite_type: "TEXT".to_string(),
                sql_standard: SqlType::Decimal(None, None),
            },
        );
    }

    /// Register a type mapping
    fn register_type(
        &mut self,
        rust_type: RustType,
        sql_type: SqlType,
        database_info: DatabaseTypeInfo,
    ) {
        // Register in Rust->SQL mapping
        self.mappings.insert(
            rust_type.clone(),
            TypeMapping {
                rust_type: rust_type.clone(),
                sql_type: sql_type.clone(),
                database_info: database_info.clone(),
                nullable: false,
                default_value: None,
            },
        );

        // Register in SQL->Rust mapping
        self.sql_to_rust.insert(sql_type, rust_type.clone());

        // Register database-specific mappings
        self.postgres_types.insert(
            database_info.postgres_type.to_uppercase(),
            rust_type.clone(),
        );
        self.mysql_types
            .insert(database_info.mysql_type.to_uppercase(), rust_type.clone());
        self.sqlite_types
            .insert(database_info.sqlite_type.to_uppercase(), rust_type.clone());
    }

    /// Get type mapping for a Rust type
    pub fn get_mapping(&self, rust_type: &RustType) -> Option<&TypeMapping> {
        self.mappings.get(rust_type)
    }

    /// Get Rust type from SQL type
    pub fn rust_type_from_sql(&self, sql_type: &SqlType) -> Option<&RustType> {
        self.sql_to_rust.get(sql_type)
    }

    /// Get Rust type from PostgreSQL type name
    pub fn rust_type_from_postgres(&self, type_name: &str) -> Option<&RustType> {
        let normalized = type_name.to_uppercase();
        self.postgres_types.get(&normalized).or_else(|| {
            // Handle common PostgreSQL type aliases
            match normalized.as_str() {
                "BOOL" => self.postgres_types.get("BOOLEAN"),
                "INT2" => self.postgres_types.get("SMALLINT"),
                "INT4" => self.postgres_types.get("INTEGER"),
                "INT8" => self.postgres_types.get("BIGINT"),
                "FLOAT4" => self.postgres_types.get("REAL"),
                "FLOAT8" => self.postgres_types.get("DOUBLE PRECISION"),
                "VARCHAR" | "CHAR" | "BPCHAR" | "NAME" => self.postgres_types.get("TEXT"),
                "TIMESTAMPTZ" => self.postgres_types.get("TIMESTAMP WITH TIME ZONE"),
                _ => None,
            }
        })
    }

    /// Get Rust type from MySQL type name
    pub fn rust_type_from_mysql(&self, type_name: &str) -> Option<&RustType> {
        let normalized = type_name.to_uppercase();
        self.mysql_types.get(&normalized).or_else(|| {
            // Handle common MySQL type variations
            match normalized.as_str() {
                "INTEGER" => self.mysql_types.get("INT"),
                "BOOL" => self.mysql_types.get("BOOLEAN"),
                "MEDIUMINT" => self.mysql_types.get("INT"),
                "DOUBLE PRECISION" | "REAL" => self.mysql_types.get("DOUBLE"),
                "VARCHAR" | "CHAR" | "TINYTEXT" | "MEDIUMTEXT" | "LONGTEXT" => {
                    self.mysql_types.get("TEXT")
                }
                "TIMESTAMP" => self.mysql_types.get("DATETIME"),
                _ => None,
            }
        })
    }

    /// Get Rust type from SQLite type name
    pub fn rust_type_from_sqlite(&self, type_name: &str) -> Option<&RustType> {
        let normalized = type_name.to_uppercase();
        // SQLite has type affinity, so we need to be more flexible
        if normalized.contains("INT") {
            self.sqlite_types.get("INTEGER")
        } else if normalized.contains("CHAR")
            || normalized.contains("CLOB")
            || normalized.contains("TEXT")
        {
            self.sqlite_types.get("TEXT")
        } else if normalized.contains("BLOB") {
            None // We'll handle this specially
        } else if normalized.contains("REAL")
            || normalized.contains("FLOA")
            || normalized.contains("DOUB")
        {
            self.sqlite_types.get("REAL")
        } else {
            self.sqlite_types.get(&normalized)
        }
    }

    /// Check if a type should be nullable
    pub fn is_nullable(&self, rust_type: &RustType) -> bool {
        matches!(rust_type, RustType::Option(_))
    }

    /// Get the inner type of an Option
    pub fn unwrap_option<'a>(&self, rust_type: &'a RustType) -> &'a RustType {
        match rust_type {
            RustType::Option(inner) => inner,
            _ => rust_type,
        }
    }
}

impl Default for TypeRegistry {
    fn default() -> Self {
        Self::new()
    }
}