grpctestify 1.6.1

gRPC testing utility written in 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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
//! Extended type system for plugin arguments and return values.
//!
//! Enables the optimizer to make smarter rewrites and the LSP to show
//! accurate type information in hover/completion.

use serde::{Deserialize, Serialize};

/// Extended type information for plugin return values and arguments.
/// More specific than `PluginReturnKind` — enables optimizer rewrites
/// and semantic validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum TypeInfo {
    // ─── Scalar types ───
    /// Boolean: true / false
    Bool,
    /// Non-negative integer (0, 1, 2, ...). Used by @len, @scope_message_count, etc.
    UInt,
    /// Any number (integer or float). Used by @elapsed_ms, etc.
    Number,
    /// String value. Used by @header, @trailer, @env, etc.
    String,

    // ─── Structured types ───
    /// JSON object/array. Used in REQUEST, RESPONSE, ERROR sections.
    Json,
    /// YAML document. Used in configuration sections (EXTRACT, OPTIONS).
    Yaml,

    // ─── Nullable variants ───
    /// Boolean or null (when lookup fails)
    BoolOrNull,
    /// String or null (when lookup fails)
    StringOrNull,
    /// JSON or null (when lookup fails)
    JsonOrNull,
    /// YAML or null (when lookup fails)
    YamlOrNull,

    // ─── Constrained strings ───
    /// String that matches UUID format
    Uuid,
    /// String that matches email format
    Email,
    /// String that matches URL format
    Url,
    /// String that matches IPv4/IPv6 format
    Ip,

    // ─── Wildcard ───
    /// JSON value of any type. Used by plugins that return the input as-is.
    Any,
}

impl TypeInfo {
    /// Check if this type can be used in a boolean context.
    pub fn is_truthy(&self) -> bool {
        matches!(self, TypeInfo::Bool | TypeInfo::BoolOrNull)
    }

    /// Check if this type can be compared with numbers.
    pub fn is_numeric(&self) -> bool {
        matches!(self, TypeInfo::UInt | TypeInfo::Number)
    }

    /// Check if this type can be compared with strings.
    pub fn is_stringy(&self) -> bool {
        matches!(
            self,
            TypeInfo::String
                | TypeInfo::StringOrNull
                | TypeInfo::Uuid
                | TypeInfo::Email
                | TypeInfo::Url
                | TypeInfo::Ip
        )
    }

    /// Check if this type can ever be null.
    pub fn is_nullable(&self) -> bool {
        matches!(
            self,
            TypeInfo::BoolOrNull
                | TypeInfo::StringOrNull
                | TypeInfo::JsonOrNull
                | TypeInfo::YamlOrNull
                | TypeInfo::Any
        )
    }

    /// Return the "base" type (strip nullability and constraints).
    pub fn base_type(&self) -> TypeInfo {
        match self {
            TypeInfo::BoolOrNull => TypeInfo::Bool,
            TypeInfo::StringOrNull => TypeInfo::String,
            TypeInfo::JsonOrNull => TypeInfo::Json,
            TypeInfo::YamlOrNull => TypeInfo::Yaml,
            TypeInfo::Uuid | TypeInfo::Email | TypeInfo::Url | TypeInfo::Ip => TypeInfo::String,
            other => *other,
        }
    }

    /// Human-readable display name for LSP hover / explain / inspect.
    pub fn display_name(&self) -> &'static str {
        match self {
            TypeInfo::Bool => "bool",
            TypeInfo::UInt => "non-negative integer",
            TypeInfo::Number => "number",
            TypeInfo::String => "string",
            TypeInfo::Json => "json",
            TypeInfo::Yaml => "yaml",
            TypeInfo::Any => "any",
            TypeInfo::BoolOrNull => "bool | null",
            TypeInfo::StringOrNull => "string | null",
            TypeInfo::JsonOrNull => "json | null",
            TypeInfo::YamlOrNull => "yaml | null",
            TypeInfo::Uuid => "uuid (string)",
            TypeInfo::Email => "email (string)",
            TypeInfo::Url => "url (string)",
            TypeInfo::Ip => "ip address (string)",
        }
    }

    /// Check if a comparison operator makes sense for this type.
    /// Returns `(true, None)` if the operator is valid,
    /// or `(false, Some(reason))` with a human-readable explanation.
    pub fn supports_operator(&self, op: &str) -> (bool, Option<&'static str>) {
        match op {
            "==" | "!=" => (true, None), // All types support equality
            ">" | "<" | ">=" | "<=" => {
                if self.is_numeric() {
                    (true, None)
                } else if matches!(self, TypeInfo::Bool | TypeInfo::BoolOrNull) {
                    (
                        false,
                        Some("boolean values cannot be compared with <, >, <=, >="),
                    )
                } else if self.is_stringy() {
                    (
                        false,
                        Some(
                            "use 'contains', 'startsWith', or 'endsWith' for string comparison, not <, >, <=, >=",
                        ),
                    )
                } else {
                    (
                        false,
                        Some("this type does not support ordering comparisons"),
                    )
                }
            }
            "contains" | "startsWith" | "endsWith" | "matches" => {
                if self.is_stringy() {
                    (true, None)
                } else if self.is_numeric() {
                    (
                        false,
                        Some(
                            "numeric values do not support string operators — use == or != instead",
                        ),
                    )
                } else if matches!(self, TypeInfo::Bool | TypeInfo::BoolOrNull) {
                    (
                        false,
                        Some("boolean values do not support string operators"),
                    )
                } else {
                    (false, Some("this type does not support string operators"))
                }
            }
            _ => (false, Some("unknown operator")),
        }
    }
}

/// Type information for a single plugin argument.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArgTypeInfo {
    /// Expected type of this argument.
    pub expected: TypeInfo,
    /// Is this argument required? If false, it has a default value.
    pub required: bool,
    /// Default value expression (for optional args).
    pub default: Option<&'static str>,
}

/// Extended plugin signature with full type information.
#[derive(Debug, Clone)]
pub struct TypedPluginSignature {
    /// Return type of the plugin.
    pub return_type: TypeInfo,
    /// Type information for each argument.
    pub arg_types: &'static [ArgTypeInfo],
    /// Plugin purity (same as PluginSignature).
    pub purity: crate::plugins::PluginPurity,
    /// Is the plugin deterministic? (same as PluginSignature).
    pub deterministic: bool,
    /// Is the plugin idempotent? (same as PluginSignature).
    pub idempotent: bool,
    /// Is it safe for the optimizer to rewrite expressions using this plugin?
    pub safe_for_rewrite: bool,
}

impl TypedPluginSignature {
    /// Check if the plugin can be called with the given number of arguments.
    pub fn valid_arg_count(&self, count: usize) -> bool {
        let required = self.arg_types.iter().filter(|a| a.required).count();
        let total = self.arg_types.len();
        count >= required && count <= total
    }

    /// Get expected type for argument at given position.
    pub fn arg_type(&self, idx: usize) -> Option<TypeInfo> {
        self.arg_types.get(idx).map(|a| a.expected)
    }
}

/// Build typed signatures for all known plugins.
/// This is the single source of truth for plugin types.
pub fn typed_plugin_signatures() -> std::collections::HashMap<String, TypedPluginSignature> {
    use crate::plugins::PluginPurity;
    let mut map = std::collections::HashMap::new();

    macro_rules! plugin_sig {
        ($name:expr, $return_type:expr, purity: $purity:expr, deterministic: $det:expr, idempotent: $idem:expr, rewrite: $rewrite:expr, args: [$($arg:expr),* $(,)?]) => {
            map.insert(
                $name.to_string(),
                TypedPluginSignature {
                    return_type: $return_type,
                    arg_types: &[$($arg),*],
                    purity: $purity,
                    deterministic: $det,
                    idempotent: $idem,
                    safe_for_rewrite: $rewrite,
                },
            );
        };
    }

    // ─── Validation plugins (return bool) ───
    plugin_sig!("@uuid", TypeInfo::Bool,
        purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    plugin_sig!("@email", TypeInfo::Bool,
        purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    plugin_sig!("@ip", TypeInfo::Bool,
        purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    plugin_sig!("@url", TypeInfo::Bool,
        purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    plugin_sig!("@timestamp", TypeInfo::Bool,
        purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::Any, required: true, default: None }]);

    plugin_sig!("@regex", TypeInfo::Bool,
    purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: false,
    args: [
        ArgTypeInfo { expected: TypeInfo::String, required: true, default: None },
        ArgTypeInfo { expected: TypeInfo::String, required: true, default: None },
    ]);

    // ─── Length / emptiness plugins ───
    plugin_sig!("@len", TypeInfo::UInt,
        purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::Any, required: true, default: None }]);

    plugin_sig!("@empty", TypeInfo::Bool,
        purity: PluginPurity::Pure, deterministic: true, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::Any, required: true, default: None }]);

    // ─── Header / trailer plugins ───
    plugin_sig!("@header", TypeInfo::StringOrNull,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: false,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    plugin_sig!("@has_header", TypeInfo::Bool,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    plugin_sig!("@trailer", TypeInfo::StringOrNull,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: false,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    plugin_sig!("@has_trailer", TypeInfo::Bool,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: true,
        args: [ArgTypeInfo { expected: TypeInfo::String, required: true, default: None }]);

    // ─── Environment plugin ───
    plugin_sig!("@env", TypeInfo::StringOrNull,
    purity: PluginPurity::Impure, deterministic: false, idempotent: false, rewrite: false,
    args: [
        ArgTypeInfo { expected: TypeInfo::String, required: true, default: None },
        ArgTypeInfo { expected: TypeInfo::String, required: false, default: None },
    ]);

    // ─── Timing plugins ───
    plugin_sig!("@elapsed_ms", TypeInfo::UInt,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: false,
        args: []);

    plugin_sig!("@total_elapsed_ms", TypeInfo::UInt,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: false,
        args: []);

    plugin_sig!("@scope_message_count", TypeInfo::UInt,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: false,
        args: []);

    plugin_sig!("@scope_index", TypeInfo::UInt,
        purity: PluginPurity::ContextDependent, deterministic: false, idempotent: true, rewrite: false,
        args: []);

    map
}

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

    #[test]
    fn test_type_info_is_truthy() {
        assert!(TypeInfo::Bool.is_truthy());
        assert!(TypeInfo::BoolOrNull.is_truthy());
        assert!(!TypeInfo::String.is_truthy());
        assert!(!TypeInfo::UInt.is_truthy());
    }

    #[test]
    fn test_type_info_is_numeric() {
        assert!(TypeInfo::UInt.is_numeric());
        assert!(TypeInfo::Number.is_numeric());
        assert!(!TypeInfo::String.is_numeric());
    }

    #[test]
    fn test_type_info_is_stringy() {
        assert!(TypeInfo::String.is_stringy());
        assert!(TypeInfo::StringOrNull.is_stringy());
        assert!(TypeInfo::Uuid.is_stringy());
        assert!(TypeInfo::Email.is_stringy());
        assert!(TypeInfo::Url.is_stringy());
        assert!(TypeInfo::Ip.is_stringy());
        assert!(!TypeInfo::Bool.is_stringy());
    }

    #[test]
    fn test_type_info_is_nullable() {
        assert!(TypeInfo::BoolOrNull.is_nullable());
        assert!(TypeInfo::StringOrNull.is_nullable());
        assert!(TypeInfo::JsonOrNull.is_nullable());
        assert!(TypeInfo::YamlOrNull.is_nullable());
        assert!(TypeInfo::Any.is_nullable());
        assert!(!TypeInfo::Bool.is_nullable());
    }

    #[test]
    fn test_type_info_base_type() {
        assert_eq!(TypeInfo::BoolOrNull.base_type(), TypeInfo::Bool);
        assert_eq!(TypeInfo::StringOrNull.base_type(), TypeInfo::String);
        assert_eq!(TypeInfo::Uuid.base_type(), TypeInfo::String);
        assert_eq!(TypeInfo::Email.base_type(), TypeInfo::String);
        assert_eq!(TypeInfo::Url.base_type(), TypeInfo::String);
        assert_eq!(TypeInfo::Ip.base_type(), TypeInfo::String);
        assert_eq!(TypeInfo::Bool.base_type(), TypeInfo::Bool);
    }

    #[test]
    fn test_type_info_display_name() {
        assert_eq!(TypeInfo::Bool.display_name(), "bool");
        assert_eq!(TypeInfo::UInt.display_name(), "non-negative integer");
        assert_eq!(TypeInfo::Number.display_name(), "number");
        assert_eq!(TypeInfo::String.display_name(), "string");
        assert_eq!(TypeInfo::Json.display_name(), "json");
        assert_eq!(TypeInfo::Yaml.display_name(), "yaml");
        assert_eq!(TypeInfo::Any.display_name(), "any");
        assert_eq!(TypeInfo::BoolOrNull.display_name(), "bool | null");
        assert_eq!(TypeInfo::Uuid.display_name(), "uuid (string)");
        assert_eq!(TypeInfo::Email.display_name(), "email (string)");
        assert_eq!(TypeInfo::Url.display_name(), "url (string)");
        assert_eq!(TypeInfo::Ip.display_name(), "ip address (string)");
    }

    #[test]
    fn test_type_info_supports_operator() {
        let (valid, _) = TypeInfo::Bool.supports_operator("==");
        assert!(valid);

        let (valid, reason) = TypeInfo::Bool.supports_operator("<");
        assert!(!valid);
        assert!(reason.is_some());

        let (valid, reason) = TypeInfo::String.supports_operator("<");
        assert!(!valid);
        assert!(reason.is_some());

        let (valid, _) = TypeInfo::String.supports_operator("contains");
        assert!(valid);

        let (valid, _) = TypeInfo::Number.supports_operator("contains");
        assert!(!valid);

        let (valid, _) = TypeInfo::Uuid.supports_operator("==");
        assert!(valid);

        let (valid, reason) = TypeInfo::Uuid.supports_operator(">");
        assert!(!valid);
        assert!(reason.is_some());

        let (valid, _reason) = TypeInfo::Uuid.supports_operator("contains");
        assert!(valid);

        let (valid, _) = TypeInfo::Any.supports_operator("==");
        assert!(valid);
    }

    #[test]
    fn test_typed_plugin_signature_valid_arg_count() {
        let sig = TypedPluginSignature {
            return_type: TypeInfo::Bool,
            arg_types: &[
                ArgTypeInfo {
                    expected: TypeInfo::String,
                    required: true,
                    default: None,
                },
                ArgTypeInfo {
                    expected: TypeInfo::String,
                    required: false,
                    default: Some("default"),
                },
            ],
            purity: crate::plugins::PluginPurity::Pure,
            deterministic: true,
            idempotent: true,
            safe_for_rewrite: true,
        };

        assert!(sig.valid_arg_count(1));
        assert!(sig.valid_arg_count(2));
        assert!(!sig.valid_arg_count(0));
        assert!(!sig.valid_arg_count(3));
    }

    #[test]
    fn test_typed_plugin_signature_count() {
        let sig = TypedPluginSignature {
            return_type: TypeInfo::Bool,
            arg_types: &[
                ArgTypeInfo {
                    expected: TypeInfo::String,
                    required: true,
                    default: None,
                },
                ArgTypeInfo {
                    expected: TypeInfo::String,
                    required: false,
                    default: Some("default"),
                },
            ],
            purity: crate::plugins::PluginPurity::Pure,
            deterministic: true,
            idempotent: true,
            safe_for_rewrite: true,
        };

        assert_eq!(sig.arg_type(0), Some(TypeInfo::String));
        assert_eq!(sig.arg_type(1), Some(TypeInfo::String));
        assert_eq!(sig.arg_type(2), None);
    }

    #[test]
    fn test_typed_plugin_signatures() {
        let sigs = typed_plugin_signatures();
        assert!(sigs.contains_key("@uuid"));
        assert!(sigs.contains_key("@email"));
        assert!(sigs.contains_key("@ip"));
        assert!(sigs.contains_key("@url"));
        assert!(sigs.contains_key("@len"));
        assert!(sigs.contains_key("@empty"));
        assert!(sigs.contains_key("@header"));
        assert!(sigs.contains_key("@trailer"));
        assert!(sigs.contains_key("@env"));
        assert!(sigs.contains_key("@elapsed_ms"));
    }
}