bsql-macros 0.9.0

Proc macros for bsql — compile-time safe SQL for Rust
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
//! Extended OID-to-Rust-type resolution with feature-gate awareness.
//!
//! The proc macro must decide which Rust type to use for a given PostgreSQL OID.
//! Base types (i32, String, etc.) are always available. Feature-gated types
//! (time::OffsetDateTime, uuid::Uuid, etc.) require the corresponding feature
//! flag to be enabled.
//!
//! This module lives in the proc macro crate because `cfg!(feature = "...")` is
//! evaluated at the proc macro's own compile time — which is controlled by the
//! feature chain: `bsql/time` -> `bsql-macros/time`.

/// Resolve a PostgreSQL OID to a Rust type string, considering enabled features.
///
/// Returns `Ok(type_string)` for recognized OIDs, or `Err(message)` if the OID
/// requires an unabled feature or is completely unsupported.
pub fn resolve_rust_type(oid: u32) -> Result<&'static str, String> {
    // Base types — always available
    if let Some(t) = bsql_core::types::rust_type_for_oid(oid) {
        return Ok(t);
    }

    match oid {
        // --- Timestamp with time zone (TIMESTAMPTZ) ---
        1184 => {
            if cfg!(feature = "time") {
                Ok("::time::OffsetDateTime")
            } else if cfg!(feature = "chrono") {
                Ok("::chrono::DateTime<::chrono::Utc>")
            } else {
                Err(feature_error("TIMESTAMPTZ", oid, &["time", "chrono"]))
            }
        }
        // --- Timestamp without time zone (TIMESTAMP) ---
        1114 => {
            if cfg!(feature = "time") {
                Ok("::time::PrimitiveDateTime")
            } else if cfg!(feature = "chrono") {
                Ok("::chrono::NaiveDateTime")
            } else {
                Err(feature_error("TIMESTAMP", oid, &["time", "chrono"]))
            }
        }
        // --- Date ---
        1082 => {
            if cfg!(feature = "time") {
                Ok("::time::Date")
            } else if cfg!(feature = "chrono") {
                Ok("::chrono::NaiveDate")
            } else {
                Err(feature_error("DATE", oid, &["time", "chrono"]))
            }
        }
        // --- Time (without time zone) ---
        1083 => {
            if cfg!(feature = "time") {
                Ok("::time::Time")
            } else if cfg!(feature = "chrono") {
                Ok("::chrono::NaiveTime")
            } else {
                Err(feature_error("TIME", oid, &["time", "chrono"]))
            }
        }
        // --- Time with time zone (TIMETZ) ---
        // 1266 is TIMETZ, rare but handle it
        1266 => Err(format!(
            "column type is TIMETZ (OID {oid}) which is not supported by bsql. \
                 Use TIME or TIMESTAMPTZ instead."
        )),
        // --- UUID ---
        2950 => {
            if cfg!(feature = "uuid") {
                Ok("::uuid::Uuid")
            } else {
                Err(feature_error("UUID", oid, &["uuid"]))
            }
        }
        // --- NUMERIC / DECIMAL ---
        1700 => {
            if cfg!(feature = "decimal") {
                Ok("::rust_decimal::Decimal")
            } else {
                Err(feature_error("NUMERIC", oid, &["decimal"]))
            }
        }
        // --- INTERVAL ---
        1186 => Err(format!(
            "column type is INTERVAL (OID {oid}) which is not yet supported by bsql. \
                 Cast to a supported type or track the bsql issue for INTERVAL support."
        )),
        // --- Timestamp/Date/Time arrays ---
        1115 => resolve_array("TIMESTAMP[]", 1114, oid),
        1185 => resolve_array("TIMESTAMPTZ[]", 1184, oid),
        1182 => resolve_array("DATE[]", 1082, oid),
        1183 => resolve_array("TIME[]", 1083, oid),
        2951 => resolve_array("UUID[]", 2950, oid),
        1231 => resolve_array("NUMERIC[]", 1700, oid),
        _ => {
            let name = bsql_core::types::pg_name_for_oid(oid).unwrap_or("unknown");
            Err(format!(
                "unsupported PostgreSQL type `{name}` (OID {oid}). \
                 Enable the appropriate feature flag in bsql or cast to a supported type."
            ))
        }
    }
}

/// Resolve an array type by delegating to the element type.
fn resolve_array(pg_name: &str, element_oid: u32, array_oid: u32) -> Result<&'static str, String> {
    match element_oid {
        1184 => {
            if cfg!(feature = "time") {
                Ok("Vec<::time::OffsetDateTime>")
            } else if cfg!(feature = "chrono") {
                Ok("Vec<::chrono::DateTime<::chrono::Utc>>")
            } else {
                Err(feature_error(pg_name, array_oid, &["time", "chrono"]))
            }
        }
        1114 => {
            if cfg!(feature = "time") {
                Ok("Vec<::time::PrimitiveDateTime>")
            } else if cfg!(feature = "chrono") {
                Ok("Vec<::chrono::NaiveDateTime>")
            } else {
                Err(feature_error(pg_name, array_oid, &["time", "chrono"]))
            }
        }
        1082 => {
            if cfg!(feature = "time") {
                Ok("Vec<::time::Date>")
            } else if cfg!(feature = "chrono") {
                Ok("Vec<::chrono::NaiveDate>")
            } else {
                Err(feature_error(pg_name, array_oid, &["time", "chrono"]))
            }
        }
        1083 => {
            if cfg!(feature = "time") {
                Ok("Vec<::time::Time>")
            } else if cfg!(feature = "chrono") {
                Ok("Vec<::chrono::NaiveTime>")
            } else {
                Err(feature_error(pg_name, array_oid, &["time", "chrono"]))
            }
        }
        2950 => {
            if cfg!(feature = "uuid") {
                Ok("Vec<::uuid::Uuid>")
            } else {
                Err(feature_error(pg_name, array_oid, &["uuid"]))
            }
        }
        1700 => {
            if cfg!(feature = "decimal") {
                Ok("Vec<::rust_decimal::Decimal>")
            } else {
                Err(feature_error(pg_name, array_oid, &["decimal"]))
            }
        }
        _ => Err(format!(
            "unsupported PostgreSQL array type `{pg_name}` (OID {array_oid})."
        )),
    }
}

/// Check whether a user-declared Rust parameter type is compatible with a PG OID.
///
/// Extends `bsql_core::types::is_param_compatible` with feature-gated types.
pub fn is_param_compatible_extended(rust_type: &str, pg_oid: u32) -> bool {
    // Base types first
    if bsql_core::types::is_param_compatible(rust_type, pg_oid) {
        return true;
    }

    match (rust_type, pg_oid) {
        // --- time crate types ---
        ("::time::OffsetDateTime" | "time::OffsetDateTime" | "OffsetDateTime", 1184) => {
            cfg!(feature = "time")
        }
        ("::time::PrimitiveDateTime" | "time::PrimitiveDateTime" | "PrimitiveDateTime", 1114) => {
            cfg!(feature = "time")
        }
        ("::time::Date" | "time::Date", 1082) => cfg!(feature = "time"),
        ("::time::Time" | "time::Time", 1083) => cfg!(feature = "time"),

        // --- chrono types ---
        (
            "::chrono::DateTime<::chrono::Utc>"
            | "chrono::DateTime<chrono::Utc>"
            | "chrono::DateTime<Utc>",
            1184,
        ) => {
            cfg!(feature = "chrono")
        }
        ("::chrono::NaiveDateTime" | "chrono::NaiveDateTime" | "NaiveDateTime", 1114) => {
            cfg!(feature = "chrono")
        }
        ("::chrono::NaiveDate" | "chrono::NaiveDate" | "NaiveDate", 1082) => {
            cfg!(feature = "chrono")
        }
        ("::chrono::NaiveTime" | "chrono::NaiveTime" | "NaiveTime", 1083) => {
            cfg!(feature = "chrono")
        }

        // --- uuid ---
        ("::uuid::Uuid" | "uuid::Uuid" | "Uuid", 2950) => cfg!(feature = "uuid"),

        // --- rust_decimal ---
        ("::rust_decimal::Decimal" | "rust_decimal::Decimal" | "Decimal", 1700) => {
            cfg!(feature = "decimal")
        }

        _ => false,
    }
}

/// Build a clear error message for a type that requires an unabled feature.
fn feature_error(pg_type: &str, oid: u32, features: &[&str]) -> String {
    let features_str = features
        .iter()
        .map(|f| format!("\"{f}\""))
        .collect::<Vec<_>>()
        .join(" or ");
    format!("column type is {pg_type} (OID {oid}) — enable feature {features_str} in bsql")
}

/// Returns true if the Rust type is a known scalar/array type that is
/// provably incompatible with PG enum parameters.
///
/// Used by `validate.rs` to reject obviously wrong types for PG enum columns
/// (e.g. `$status: i32` on an enum column) while still allowing unknown types
/// that might be `#[bsql::pg_enum]` user enums.
pub fn is_known_non_enum_type(rust_type: &str) -> bool {
    matches!(
        rust_type,
        "bool"
            | "i16"
            | "i32"
            | "i64"
            | "f32"
            | "f64"
            | "u32"
            | "Vec<u8>"
            | "&[u8]"
            | "Vec<bool>"
            | "Vec<i16>"
            | "Vec<i32>"
            | "Vec<i64>"
            | "Vec<f32>"
            | "Vec<f64>"
            | "Vec<String>"
            | "&[bool]"
            | "&[i16]"
            | "&[i32]"
            | "&[i64]"
            | "&[f32]"
            | "&[f64]"
            | "&[&str]"
            | "&[String]"
    ) || rust_type.starts_with("::time::")
        || rust_type.starts_with("time::")
        || rust_type.starts_with("::chrono::")
        || rust_type.starts_with("chrono::")
        || rust_type.starts_with("::uuid::")
        || rust_type.starts_with("uuid::")
        || rust_type.starts_with("::rust_decimal::")
        || rust_type.starts_with("rust_decimal::")
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn base_types_resolve() {
        assert_eq!(resolve_rust_type(23).unwrap(), "i32");
        assert_eq!(resolve_rust_type(25).unwrap(), "String");
        assert_eq!(resolve_rust_type(16).unwrap(), "bool");
    }

    #[test]
    fn unknown_oid_errors() {
        assert!(resolve_rust_type(99999).is_err());
    }

    // Feature-dependent tests: these pass based on which features are active
    // during `cargo test` of the proc macro crate.

    #[cfg(feature = "time")]
    #[test]
    fn timestamptz_resolves_to_time() {
        assert_eq!(resolve_rust_type(1184).unwrap(), "::time::OffsetDateTime");
        assert_eq!(
            resolve_rust_type(1114).unwrap(),
            "::time::PrimitiveDateTime"
        );
        assert_eq!(resolve_rust_type(1082).unwrap(), "::time::Date");
        assert_eq!(resolve_rust_type(1083).unwrap(), "::time::Time");
    }

    #[cfg(feature = "uuid")]
    #[test]
    fn uuid_resolves() {
        assert_eq!(resolve_rust_type(2950).unwrap(), "::uuid::Uuid");
    }

    #[cfg(not(any(feature = "time", feature = "chrono")))]
    #[test]
    fn timestamptz_errors_without_feature() {
        let err = resolve_rust_type(1184).unwrap_err();
        assert!(err.contains("TIMESTAMPTZ"), "unexpected error: {err}");
        assert!(err.contains("time"), "should suggest time feature: {err}");
    }

    #[cfg(not(feature = "uuid"))]
    #[test]
    fn uuid_errors_without_feature() {
        let err = resolve_rust_type(2950).unwrap_err();
        assert!(err.contains("UUID"), "unexpected error: {err}");
        assert!(err.contains("uuid"), "should suggest uuid feature: {err}");
    }

    #[test]
    fn base_param_compat_still_works() {
        assert!(is_param_compatible_extended("i32", 23));
        assert!(is_param_compatible_extended("&str", 25));
        assert!(!is_param_compatible_extended("i32", 25));
    }

    #[cfg(feature = "uuid")]
    #[test]
    fn uuid_param_compat() {
        assert!(is_param_compatible_extended("::uuid::Uuid", 2950));
        assert!(is_param_compatible_extended("uuid::Uuid", 2950));
        assert!(is_param_compatible_extended("Uuid", 2950));
    }

    #[cfg(feature = "time")]
    #[test]
    fn time_param_compat() {
        assert!(is_param_compatible_extended("::time::OffsetDateTime", 1184));
        assert!(is_param_compatible_extended("time::OffsetDateTime", 1184));
        assert!(is_param_compatible_extended("OffsetDateTime", 1184));
        assert!(is_param_compatible_extended("::time::Date", 1082));
    }

    #[cfg(all(feature = "chrono", not(feature = "time")))]
    #[test]
    fn timestamptz_resolves_to_chrono() {
        assert_eq!(
            resolve_rust_type(1184).unwrap(),
            "::chrono::DateTime<::chrono::Utc>"
        );
        assert_eq!(resolve_rust_type(1114).unwrap(), "::chrono::NaiveDateTime");
        assert_eq!(resolve_rust_type(1082).unwrap(), "::chrono::NaiveDate");
        assert_eq!(resolve_rust_type(1083).unwrap(), "::chrono::NaiveTime");
    }

    #[cfg(feature = "chrono")]
    #[test]
    fn chrono_param_compat() {
        assert!(is_param_compatible_extended("::chrono::NaiveDate", 1082));
        assert!(is_param_compatible_extended("chrono::NaiveDate", 1082));
        assert!(is_param_compatible_extended("NaiveDate", 1082));
        assert!(is_param_compatible_extended(
            "::chrono::NaiveDateTime",
            1114
        ));
    }

    #[cfg(feature = "decimal")]
    #[test]
    fn numeric_resolves_to_decimal() {
        assert_eq!(resolve_rust_type(1700).unwrap(), "::rust_decimal::Decimal");
    }

    #[cfg(feature = "decimal")]
    #[test]
    fn decimal_param_compat() {
        assert!(is_param_compatible_extended(
            "::rust_decimal::Decimal",
            1700
        ));
        assert!(is_param_compatible_extended("rust_decimal::Decimal", 1700));
        assert!(is_param_compatible_extended("Decimal", 1700));
    }

    #[cfg(not(feature = "decimal"))]
    #[test]
    fn numeric_errors_without_feature() {
        let err = resolve_rust_type(1700).unwrap_err();
        assert!(err.contains("NUMERIC"), "unexpected error: {err}");
        assert!(
            err.contains("decimal"),
            "should suggest decimal feature: {err}"
        );
    }

    // --- is_known_non_enum_type ---

    #[test]
    fn known_non_enum_scalars() {
        assert!(is_known_non_enum_type("bool"));
        assert!(is_known_non_enum_type("i32"));
        assert!(is_known_non_enum_type("i64"));
        assert!(is_known_non_enum_type("f64"));
        assert!(is_known_non_enum_type("u32"));
    }

    #[test]
    fn known_non_enum_arrays() {
        assert!(is_known_non_enum_type("Vec<u8>"));
        assert!(is_known_non_enum_type("Vec<i32>"));
        assert!(is_known_non_enum_type("&[i64]"));
        assert!(is_known_non_enum_type("&[&str]"));
    }

    #[test]
    fn known_non_enum_crate_types() {
        assert!(is_known_non_enum_type("::time::OffsetDateTime"));
        assert!(is_known_non_enum_type("time::Date"));
        assert!(is_known_non_enum_type("::chrono::NaiveDate"));
        assert!(is_known_non_enum_type("chrono::NaiveDateTime"));
        assert!(is_known_non_enum_type("::uuid::Uuid"));
        assert!(is_known_non_enum_type("uuid::Uuid"));
        assert!(is_known_non_enum_type("::rust_decimal::Decimal"));
        assert!(is_known_non_enum_type("rust_decimal::Decimal"));
    }

    #[test]
    fn str_and_string_not_known_non_enum() {
        assert!(!is_known_non_enum_type("&str"));
        assert!(!is_known_non_enum_type("String"));
    }

    #[test]
    fn unknown_custom_type_not_known_non_enum() {
        assert!(!is_known_non_enum_type("TicketStatus"));
        assert!(!is_known_non_enum_type("MyEnum"));
    }
}