elif-http-derive 0.2.11

Derive macros for elif-http declarative routing and controller system
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
449
450
451
452
453
454
455
456
457
458
//! Parameter and body validation macros
//!
//! Provides #[param] and #[body] macros for type-safe parameter handling.

use proc_macro::TokenStream;
use quote::quote;
use syn::{
    parse::Parse, parse::ParseStream, parse_macro_input, FnArg, Ident, ItemFn, Pat, PatIdent,
    Signature, Token, Type,
};

/// Parameter specification for type validation
#[derive(Debug, Clone)]
pub struct ParamSpec {
    pub name: Ident,
    pub param_type: ParamType,
}

/// Body parameter specification for injection
#[derive(Debug, Clone)]
pub struct BodySpec {
    pub name: Ident,
    pub body_type: BodyParamType,
}

/// Supported parameter types for validation
#[derive(Debug, Clone, PartialEq)]
pub enum ParamType {
    String,
    Int,
    UInt,
    Float,
    Bool,
    Uuid,
}

/// Supported body parameter types for injection
#[derive(Debug, Clone, PartialEq)]
pub enum BodyParamType {
    Custom(Box<Type>), // Custom deserializable type (boxed to reduce enum size)
    Form,              // HashMap<String, String> from form data
    Bytes,             // Vec<u8> for raw bytes
}

impl std::fmt::Display for ParamType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            ParamType::String => write!(f, "string"),
            ParamType::Int => write!(f, "int"),
            ParamType::UInt => write!(f, "uint"),
            ParamType::Float => write!(f, "float"),
            ParamType::Bool => write!(f, "bool"),
            ParamType::Uuid => write!(f, "uuid"),
        }
    }
}

impl Parse for ParamSpec {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let name: Ident = input.parse()?;
        input.parse::<Token![:]>()?;
        let type_ident: Ident = input.parse()?;
        let type_name = type_ident.to_string();

        let param_type = match type_name.as_str() {
            "string" => ParamType::String,
            "int" => ParamType::Int,
            "i32" => ParamType::Int,
            "i64" => ParamType::Int,
            "uint" => ParamType::UInt,
            "u32" => ParamType::UInt,
            "u64" => ParamType::UInt,
            "float" => ParamType::Float,
            "f32" => ParamType::Float,
            "f64" => ParamType::Float,
            "bool" => ParamType::Bool,
            "uuid" => ParamType::Uuid,
            _ => {
                return Err(syn::Error::new_spanned(
                    type_ident,
                    format!(
                        "Unsupported parameter type '{}'. Supported types: string, int, i32, i64, uint, u32, u64, float, f32, f64, bool, uuid. \
                        Hint: Use #[param({}: string)] for string parameters or #[param({}: u32)] for u32 parameters.", 
                        type_name, 
                        name, 
                        name
                    )
                ));
            }
        };

        Ok(ParamSpec { name, param_type })
    }
}

impl Parse for BodySpec {
    fn parse(input: ParseStream) -> syn::Result<Self> {
        let name: Ident = input.parse()?;
        input.parse::<Token![:]>()?;

        // Check for special types first (form, bytes)
        if input.peek(syn::Ident) {
            let lookahead = input.fork();
            if let Ok(type_ident) = lookahead.parse::<Ident>() {
                match type_ident.to_string().as_str() {
                    "form" => {
                        input.parse::<Ident>()?; // consume the 'form' ident
                        return Ok(BodySpec {
                            name,
                            body_type: BodyParamType::Form,
                        });
                    }
                    "bytes" => {
                        input.parse::<Ident>()?; // consume the 'bytes' ident
                        return Ok(BodySpec {
                            name,
                            body_type: BodyParamType::Bytes,
                        });
                    }
                    _ => {}
                }
            }
        }

        // Parse as custom type
        let custom_type: Type = input.parse()?;
        Ok(BodySpec {
            name,
            body_type: BodyParamType::Custom(Box::new(custom_type)),
        })
    }
}

/// Validate that parameter specification matches function signature
pub fn validate_param_consistency(param_spec: &ParamSpec, sig: &Signature) -> Result<(), String> {
    let param_name = param_spec.name.to_string();

    // Find the parameter in the function signature
    let mut found_param = None;
    for input in &sig.inputs {
        if let FnArg::Typed(pat_type) = input {
            if let Pat::Ident(PatIdent { ident, .. }) = pat_type.pat.as_ref() {
                if *ident == param_name {
                    found_param = Some(&*pat_type.ty);
                    break;
                }
            }
        }
    }

    let param_type = match found_param {
        Some(ty) => ty,
        None => return Err(format!(
            "Parameter '{}' specified in #[param] not found in function signature. \
            Hint: Add '{}: SomeType' as a function parameter or remove the #[param({})] annotation.",
            param_name, param_name, param_name
        )),
    };

    // Validate type compatibility
    let type_str = quote! { #param_type }.to_string().replace(" ", "");
    let expected_types = get_compatible_rust_types(&param_spec.param_type);

    if !expected_types
        .iter()
        .any(|expected| type_str.contains(expected))
    {
        return Err(format!(
            "Parameter '{}' has type '{}' but expected one of: {}. \
            Hint: Change the function parameter to match #[param({}: {})] or change the #[param] declaration to match the function parameter.",
            param_name,
            type_str,
            expected_types.join(", "),
            param_name,
            get_recommended_param_type(&param_spec.param_type)
        ));
    }

    Ok(())
}

/// Get compatible Rust types for a parameter type
pub fn get_compatible_rust_types(param_type: &ParamType) -> Vec<&'static str> {
    match param_type {
        ParamType::String => vec!["String", "&str", "str"],
        ParamType::Int => vec!["i32", "i64", "isize"],
        ParamType::UInt => vec!["u32", "u64", "usize"],
        ParamType::Float => vec!["f32", "f64"],
        ParamType::Bool => vec!["bool"],
        ParamType::Uuid => vec!["Uuid", "uuid::Uuid"],
    }
}

/// Get the recommended parameter type string for error messages
pub fn get_recommended_param_type(param_type: &ParamType) -> &'static str {
    match param_type {
        ParamType::String => "string",
        ParamType::Int => "int",
        ParamType::UInt => "uint",
        ParamType::Float => "float",
        ParamType::Bool => "bool",
        ParamType::Uuid => "uuid",
    }
}


/// Validate that function signature is compatible with the specified body type
/// NOTE: This function is deprecated - use validate_body_param_consistency for new parameter injection system
#[allow(dead_code)]
pub fn validate_body_compatibility(body_type: &Type, sig: &Signature) -> Result<(), String> {
    let body_type_str = quote! { #body_type }.to_string().replace(" ", "");

    // Look for a parameter that could accept the body
    let mut found_body_param = false;
    for input in &sig.inputs {
        if let FnArg::Typed(pat_type) = input {
            let param_type_str = quote! { #pat_type.ty }.to_string().replace(" ", "");

            // Check if this parameter could accept the body type
            if param_type_str.contains("ElifJson")
                || param_type_str.contains("Json")
                || param_type_str.contains(&body_type_str)
            {
                found_body_param = true;

                // Validate that it's properly wrapped
                if param_type_str.contains("ElifJson") {
                    // Check if the inner type matches
                    if !param_type_str.contains(&body_type_str) {
                        return Err(format!(
                            "ElifJson parameter type doesn't match expected body type '{}'",
                            body_type_str
                        ));
                    }
                }
                break;
            }
        }
    }

    if !found_body_param {
        return Err(format!(
            "No compatible body parameter found for type '{}'. Expected parameter like ElifJson<{}>",
            body_type_str, body_type_str
        ));
    }

    Ok(())
}

/// Validate that body parameter specification matches function signature
pub fn validate_body_param_consistency(
    body_spec: &BodySpec,
    sig: &Signature,
) -> Result<(), String> {
    let param_name = body_spec.name.to_string();

    // Find the parameter in the function signature
    let mut found_param = None;
    for input in &sig.inputs {
        if let FnArg::Typed(pat_type) = input {
            if let Pat::Ident(PatIdent { ident, .. }) = pat_type.pat.as_ref() {
                if *ident == param_name {
                    found_param = Some(&*pat_type.ty);
                    break;
                }
            }
        }
    }

    let param_type = match found_param {
        Some(ty) => ty,
        None => {
            return Err(format!(
                "Body parameter '{}' not found in function signature",
                param_name
            ))
        }
    };

    // Validate type compatibility based on body type
    let type_str = quote! { #param_type }.to_string().replace(" ", "");

    match &body_spec.body_type {
        BodyParamType::Custom(expected_type) => {
            let expected_type_str = quote! { #expected_type }.to_string().replace(" ", "");
            // For custom types, the function parameter should match exactly
            if type_str != expected_type_str {
                return Err(format!(
                    "Body parameter '{}' has type '{}' but expected '{}'",
                    param_name, type_str, expected_type_str
                ));
            }
        }
        BodyParamType::Form => {
            // Form data should be HashMap<String, String>
            if !type_str.contains("HashMap") || !type_str.contains("String") {
                return Err(format!(
                    "Body parameter '{}' has type '{}' but expected 'HashMap<String, String>' for form data",
                    param_name, type_str
                ));
            }
        }
        BodyParamType::Bytes => {
            // Bytes should be Vec<u8>
            if !type_str.contains("Vec") || !type_str.contains("u8") {
                return Err(format!(
                    "Body parameter '{}' has type '{}' but expected 'Vec<u8>' for bytes data",
                    param_name, type_str
                ));
            }
        }
    }

    Ok(())
}

/// Route parameter specification macro
/// Validates that the parameter type matches what's expected from the route path
pub fn param_impl(args: TokenStream, input: TokenStream) -> TokenStream {
    let input_fn = parse_macro_input!(input as ItemFn);

    // Parse parameter specification from args
    let param_spec = if !args.is_empty() {
        match syn::parse::<ParamSpec>(args) {
            Ok(spec) => Some(spec),
            Err(err) => {
                return syn::Error::new(
                    err.span(),
                    format!("Invalid param specification: {}. Hint: Use #[param(id: u32)] or #[param(name: string)]", err)
                )
                .to_compile_error()
                .into();
            }
        }
    } else {
        None
    };

    // Validate that function signature matches param specification
    if let Some(spec) = &param_spec {
        if let Err(msg) = validate_param_consistency(spec, &input_fn.sig) {
            return syn::Error::new_spanned(
                &input_fn.sig,
                format!("Parameter type mismatch: {}", msg),
            )
            .to_compile_error()
            .into();
        }
    }

    // The #[param] macro only provides metadata - actual injection is handled by HTTP method macros
    let expanded = quote! {
        #input_fn
    };

    TokenStream::from(expanded)
}

/// Request body specification macro
/// Validates that the handler function can accept the specified body type
pub fn body_impl(args: TokenStream, input: TokenStream) -> TokenStream {
    let input_fn = parse_macro_input!(input as ItemFn);

    // Parse body parameter specification from args
    let body_spec = if !args.is_empty() {
        match syn::parse::<BodySpec>(args) {
            Ok(spec) => Some(spec),
            Err(err) => {
                return syn::Error::new(
                    err.span(),
                    format!("Invalid body specification: {}. Hint: Use #[body(param_name: Type)] for parameter injection or #[body(user_data: CreateUserRequest)]", err)
                )
                .to_compile_error()
                .into();
            }
        }
    } else {
        return syn::Error::new(
            proc_macro2::Span::call_site(),
            "Body specification required. Hint: Use #[body(param_name: Type)] to specify the body parameter."
        )
        .to_compile_error()
        .into();
    };

    // Validate that function signature matches body specification
    if let Some(spec) = &body_spec {
        if let Err(msg) = validate_body_param_consistency(spec, &input_fn.sig) {
            return syn::Error::new_spanned(
                &input_fn.sig,
                format!("Body parameter mismatch: {}. Hint: Ensure function parameter name and type match #[body] declaration.", msg)
            )
            .to_compile_error()
            .into();
        }
    }

    let expanded = quote! {
        #input_fn
    };

    TokenStream::from(expanded)
}

/// Request injection macro for automatic ElifRequest parameter injection
/// Marks methods that should receive an ElifRequest parameter automatically
pub fn request_impl(args: TokenStream, input: TokenStream) -> TokenStream {
    let input_fn = parse_macro_input!(input as ItemFn);

    // Parse optional parameter name from args (for future extensibility)
    let req_param_name = if !args.is_empty() {
        match syn::parse::<Ident>(args) {
            Ok(name) => name.to_string(),
            Err(_) => {
                return syn::Error::new(
                    proc_macro2::Span::call_site(),
                    "Invalid request parameter name. Hint: Use #[request] or #[request(req)] for custom naming."
                )
                .to_compile_error()
                .into();
            }
        }
    } else {
        "req".to_string()
    };

    // Check if the function already has a parameter that conflicts with the request name
    for input in &input_fn.sig.inputs {
        if let FnArg::Typed(pat_type) = input {
            if let Pat::Ident(PatIdent { ident, .. }) = pat_type.pat.as_ref() {
                if *ident == req_param_name {
                    // A parameter with the same name exists. Check if it's a conflict.
                    let param_type_str = quote! { #pat_type.ty }.to_string();
                    if !param_type_str.contains("ElifRequest") {
                        // It's a conflict if the type is not ElifRequest.
                        return syn::Error::new_spanned(
                            &input_fn.sig,
                            format!("Function already has a parameter named '{}' which conflicts with #[request]. Either rename the parameter or change its type to ElifRequest.", req_param_name)
                        )
                        .to_compile_error()
                        .into();
                    }
                    // If it is an ElifRequest, it's not a conflict. We can stop checking.
                    break;
                }
            }
        }
    }

    // The actual signature modification will be handled by the HTTP method macros
    // This macro validates the function and marks it for request parameter injection
    let expanded = quote! {
        #input_fn
    };

    TokenStream::from(expanded)
}