sqry-core 11.0.3

Core library for sqry - semantic code search engine
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
//! Main [`QueryBuilder`] implementation

use std::sync::Arc;

use super::{BuildError, ConditionBuilder, RegexBuilder};
use crate::query::registry::FieldRegistry;
use crate::query::types::{
    Expr, FieldType, Operator, Query as QueryAST, RegexFlags, RegexValue, Span, Value,
};

/// Builder for constructing type-safe queries.
///
/// # Example
///
/// ```ignore
/// use sqry_core::query::builder::QueryBuilder;
///
/// let query = QueryBuilder::kind("function")
///     .and(QueryBuilder::lang("rust"))
///     .and_not(QueryBuilder::name_matches("test.*"))
///     .build()?;
/// ```
#[derive(Clone, Debug)]
#[must_use = "QueryBuilder does nothing until .build() is called"]
pub struct QueryBuilder {
    /// The expression being built
    expr: BuilderExpr,
    /// Accumulated validation errors (lazy validation)
    errors: Vec<BuildError>,
}

/// Internal expression representation during building
#[derive(Clone, Debug)]
enum BuilderExpr {
    /// Single condition
    Condition(ConditionBuilder),
    /// AND of multiple expressions
    And(Vec<QueryBuilder>),
    /// OR of multiple expressions
    Or(Vec<QueryBuilder>),
    /// Negation of expression
    Not(Box<QueryBuilder>),
    /// Empty builder (for chaining from `new()`)
    Empty,
}

// ============================================================================
// Constructor Methods
// ============================================================================

impl QueryBuilder {
    /// Create empty builder for chaining
    pub fn new() -> Self {
        Self {
            expr: BuilderExpr::Empty,
            errors: Vec::new(),
        }
    }

    // ========================================================================
    // Node Identity Fields
    // ========================================================================

    /// Filter by symbol kind (function, method, class, etc.)
    pub fn kind(value: impl Into<String>) -> Self {
        Self::condition("kind", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by multiple symbol kinds (OR)
    pub fn kind_any(values: &[&str]) -> Self {
        Self::any(values.iter().map(|v| Self::kind(*v)).collect())
    }

    /// Filter by symbol name (exact match)
    pub fn name(value: impl Into<String>) -> Self {
        Self::condition("name", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by symbol name (regex match with default flags)
    pub fn name_matches(pattern: impl Into<String>) -> Self {
        let regex = RegexValue {
            pattern: pattern.into(),
            flags: RegexFlags::default(),
        };
        Self::condition("name", Operator::Regex, Value::Regex(regex))
    }

    /// Filter by symbol name (regex match with custom flags via closure)
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Case-insensitive name matching
    /// QueryBuilder::name_matches_with("test.*", |rb| rb.case_insensitive())
    /// ```
    pub fn name_matches_with<F>(pattern: impl Into<String>, configure: F) -> Self
    where
        F: FnOnce(RegexBuilder) -> RegexBuilder,
    {
        let builder = RegexBuilder::new(pattern);
        let configured = configure(builder);
        Self::condition(
            "name",
            Operator::Regex,
            Value::Regex(configured.into_regex_value()),
        )
    }

    /// Filter by programming language
    pub fn lang(value: impl Into<String>) -> Self {
        Self::condition("lang", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by programming language (alias for lang)
    pub fn language(value: impl Into<String>) -> Self {
        Self::lang(value)
    }

    // ========================================================================
    // Location Fields
    // ========================================================================

    /// Filter by file path (exact or glob match)
    pub fn path(value: impl Into<String>) -> Self {
        Self::condition("path", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by file path (alias for path)
    pub fn file(value: impl Into<String>) -> Self {
        Self::path(value)
    }

    /// Filter by file path (regex match with default flags)
    pub fn path_matches(pattern: impl Into<String>) -> Self {
        let regex = RegexValue {
            pattern: pattern.into(),
            flags: RegexFlags::default(),
        };
        Self::condition("path", Operator::Regex, Value::Regex(regex))
    }

    /// Filter by file path (regex match with custom flags via closure)
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Case-insensitive path matching
    /// QueryBuilder::path_matches_with(".*test.*", |rb| rb.case_insensitive())
    /// ```
    pub fn path_matches_with<F>(pattern: impl Into<String>, configure: F) -> Self
    where
        F: FnOnce(RegexBuilder) -> RegexBuilder,
    {
        let builder = RegexBuilder::new(pattern);
        let configured = configure(builder);
        Self::condition(
            "path",
            Operator::Regex,
            Value::Regex(configured.into_regex_value()),
        )
    }

    /// Filter by repository
    pub fn repo(value: impl Into<String>) -> Self {
        Self::condition("repo", Operator::Equal, Value::String(value.into()))
    }

    // ========================================================================
    // Hierarchy Fields
    // ========================================================================

    /// Filter by parent symbol
    pub fn parent(value: impl Into<String>) -> Self {
        Self::condition("parent", Operator::Equal, Value::String(value.into()))
    }

    // ========================================================================
    // Content Fields
    // ========================================================================

    /// Filter by text content (regex only, default flags)
    pub fn text_matches(pattern: impl Into<String>) -> Self {
        let regex = RegexValue {
            pattern: pattern.into(),
            flags: RegexFlags::default(),
        };
        Self::condition("text", Operator::Regex, Value::Regex(regex))
    }

    /// Filter by text content (regex with custom flags via closure)
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Multi-line text matching
    /// QueryBuilder::text_matches_with("^pub fn.*$", |rb| rb.multiline())
    /// ```
    pub fn text_matches_with<F>(pattern: impl Into<String>, configure: F) -> Self
    where
        F: FnOnce(RegexBuilder) -> RegexBuilder,
    {
        let builder = RegexBuilder::new(pattern);
        let configured = configure(builder);
        Self::condition(
            "text",
            Operator::Regex,
            Value::Regex(configured.into_regex_value()),
        )
    }

    // ========================================================================
    // Relation Predicates
    // ========================================================================

    /// Filter symbols that call the specified symbol
    pub fn callers(symbol: impl Into<String>) -> Self {
        Self::condition("callers", Operator::Equal, Value::String(symbol.into()))
    }

    /// Filter symbols called by the specified symbol
    pub fn callees(symbol: impl Into<String>) -> Self {
        Self::condition("callees", Operator::Equal, Value::String(symbol.into()))
    }

    /// Filter symbols that import the specified module
    pub fn imports(module: impl Into<String>) -> Self {
        Self::condition("imports", Operator::Equal, Value::String(module.into()))
    }

    /// Filter symbols that export something
    pub fn exports(value: impl Into<String>) -> Self {
        Self::condition("exports", Operator::Equal, Value::String(value.into()))
    }

    /// Filter symbols with the specified return type
    pub fn returns(type_name: impl Into<String>) -> Self {
        Self::condition("returns", Operator::Equal, Value::String(type_name.into()))
    }

    /// Filter symbols that reference the specified symbol
    pub fn references(symbol: impl Into<String>) -> Self {
        Self::condition("references", Operator::Equal, Value::String(symbol.into()))
    }

    // ========================================================================
    // Scope Predicates (P2-34)
    // ========================================================================

    /// Filter by scope (file, module, class, function, block)
    ///
    /// This targets the core `scope` field (enum type).
    pub fn scope(value: impl Into<String>) -> Self {
        Self::condition("scope", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by scope type (module, function, class, struct, method, block, etc.)
    ///
    /// This targets the `scope.type` compound field for nested scope filtering.
    pub fn scope_type(value: impl Into<String>) -> Self {
        Self::condition("scope.type", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by scope name
    pub fn scope_name(value: impl Into<String>) -> Self {
        Self::condition("scope.name", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by scope parent
    pub fn scope_parent(value: impl Into<String>) -> Self {
        Self::condition("scope.parent", Operator::Equal, Value::String(value.into()))
    }

    /// Filter by scope ancestor (transitive parent)
    pub fn scope_ancestor(value: impl Into<String>) -> Self {
        Self::condition(
            "scope.ancestor",
            Operator::Equal,
            Value::String(value.into()),
        )
    }

    // ========================================================================
    // Generic Field Access (for plugin fields)
    // ========================================================================

    /// Access any field by name with a value
    pub fn field(name: impl Into<String>, value: impl Into<Value>) -> Self {
        Self::condition_value(name.into(), Operator::Equal, value.into())
    }

    /// Access any field by name with regex match (default flags)
    pub fn field_matches(name: impl Into<String>, pattern: impl Into<String>) -> Self {
        let regex = RegexValue {
            pattern: pattern.into(),
            flags: RegexFlags::default(),
        };
        Self::condition_value(name.into(), Operator::Regex, Value::Regex(regex))
    }

    /// Access any field by name with regex match (custom flags via closure)
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Case-insensitive field matching
    /// QueryBuilder::field_matches_with("custom_field", "pattern.*", |rb| rb.case_insensitive())
    /// ```
    pub fn field_matches_with<F>(
        name: impl Into<String>,
        pattern: impl Into<String>,
        configure: F,
    ) -> Self
    where
        F: FnOnce(RegexBuilder) -> RegexBuilder,
    {
        let builder = RegexBuilder::new(pattern);
        let configured = configure(builder);
        Self::condition_value(
            name.into(),
            Operator::Regex,
            Value::Regex(configured.into_regex_value()),
        )
    }

    /// Numeric comparison: field > value
    pub fn field_gt(name: impl Into<String>, value: i64) -> Self {
        Self::condition_value(name.into(), Operator::Greater, Value::Number(value))
    }

    /// Numeric comparison: field >= value
    pub fn field_gte(name: impl Into<String>, value: i64) -> Self {
        Self::condition_value(name.into(), Operator::GreaterEq, Value::Number(value))
    }

    /// Numeric comparison: field < value
    pub fn field_lt(name: impl Into<String>, value: i64) -> Self {
        Self::condition_value(name.into(), Operator::Less, Value::Number(value))
    }

    /// Numeric comparison: field <= value
    pub fn field_lte(name: impl Into<String>, value: i64) -> Self {
        Self::condition_value(name.into(), Operator::LessEq, Value::Number(value))
    }

    // ========================================================================
    // Private Helpers
    // ========================================================================

    /// Create a condition with a static field name (used by core field methods)
    fn condition(field: &'static str, operator: Operator, value: Value) -> Self {
        Self {
            expr: BuilderExpr::Condition(ConditionBuilder::new_static(field, operator, value)),
            errors: Vec::new(),
        }
    }

    /// Create a condition with a dynamic field name (used by generic field methods)
    fn condition_value(field: String, operator: Operator, value: Value) -> Self {
        Self {
            expr: BuilderExpr::Condition(ConditionBuilder::new(field, operator, value)),
            errors: Vec::new(),
        }
    }
}

// ============================================================================
// Boolean Combinators
// ============================================================================

impl QueryBuilder {
    /// Static constructor: AND of multiple conditions
    pub fn all(conditions: Vec<QueryBuilder>) -> Self {
        let errors = conditions.iter().flat_map(|c| c.errors.clone()).collect();
        Self {
            expr: BuilderExpr::And(conditions),
            errors,
        }
    }

    /// Static constructor: OR of multiple conditions
    pub fn any(conditions: Vec<QueryBuilder>) -> Self {
        let errors = conditions.iter().flat_map(|c| c.errors.clone()).collect();
        Self {
            expr: BuilderExpr::Or(conditions),
            errors,
        }
    }

    /// Chainable: combine with AND
    pub fn and(self, other: QueryBuilder) -> Self {
        // Merge errors from both operands
        let mut errors = self.errors;
        errors.extend(other.errors.clone());

        match self.expr {
            BuilderExpr::Empty => Self {
                expr: other.expr,
                errors,
            },
            BuilderExpr::And(mut exprs) => {
                exprs.push(other);
                Self {
                    expr: BuilderExpr::And(exprs),
                    errors,
                }
            }
            _ => Self {
                expr: BuilderExpr::And(vec![
                    Self {
                        expr: self.expr,
                        errors: Vec::new(),
                    },
                    other,
                ]),
                errors,
            },
        }
    }

    /// Chainable: combine with OR
    pub fn or(self, other: QueryBuilder) -> Self {
        // Merge errors from both operands
        let mut errors = self.errors;
        errors.extend(other.errors.clone());

        match self.expr {
            BuilderExpr::Empty => Self {
                expr: other.expr,
                errors,
            },
            BuilderExpr::Or(mut exprs) => {
                exprs.push(other);
                Self {
                    expr: BuilderExpr::Or(exprs),
                    errors,
                }
            }
            _ => Self {
                expr: BuilderExpr::Or(vec![
                    Self {
                        expr: self.expr,
                        errors: Vec::new(),
                    },
                    other,
                ]),
                errors,
            },
        }
    }

    /// Chainable: combine with AND NOT
    pub fn and_not(self, other: QueryBuilder) -> Self {
        self.and(Self::negate(other))
    }

    /// Static constructor: negate expression
    ///
    /// Named `negate` to avoid confusion with `std::ops::Not::not`.
    /// Use this to create `NOT <expr>` conditions.
    pub fn negate(builder: QueryBuilder) -> Self {
        let errors = builder.errors.clone();
        Self {
            expr: BuilderExpr::Not(Box::new(builder)),
            errors,
        }
    }
}

// ============================================================================
// Build Methods
// ============================================================================

impl QueryBuilder {
    /// Build the query with default field registry validation
    ///
    /// # Errors
    ///
    /// Returns `BuildError` if:
    /// - Unknown field names are used
    /// - Operators are incompatible with field types
    /// - Value types don't match field types
    /// - Enum values are invalid
    /// - Regex patterns are syntactically invalid
    /// - The query is empty (no conditions)
    pub fn build(self) -> Result<Arc<QueryAST>, BuildError> {
        let registry = FieldRegistry::with_core_fields();
        self.build_with_registry(&registry)
    }

    /// Build with custom field registry (for plugin fields)
    ///
    /// This allows validation against a registry that includes plugin-specific
    /// fields in addition to core fields.
    ///
    /// # Errors
    ///
    /// Same as `build()`.
    pub fn build_with_registry(
        self,
        registry: &FieldRegistry,
    ) -> Result<Arc<QueryAST>, BuildError> {
        // Report any accumulated errors
        if !self.errors.is_empty() {
            return Err(BuildError::Multiple(self.errors));
        }

        // Convert builder expression to AST
        let expr = self.into_expr(registry)?;

        Ok(Arc::new(QueryAST {
            root: expr,
            span: Span::synthetic(),
        }))
    }

    fn into_expr(self, registry: &FieldRegistry) -> Result<Expr, BuildError> {
        match self.expr {
            BuilderExpr::Empty => Err(BuildError::EmptyQuery),
            BuilderExpr::Condition(ref cond) => {
                // Validate field, operator, value, and enum constraints
                Self::validate_condition(cond, registry)?;
                // Clone the condition to allow consumption by into_condition
                Ok(Expr::Condition(cond.clone().into_condition(registry)))
            }
            BuilderExpr::And(exprs) => {
                let children: Result<Vec<_>, _> =
                    exprs.into_iter().map(|e| e.into_expr(registry)).collect();
                Ok(Expr::And(children?))
            }
            BuilderExpr::Or(exprs) => {
                let children: Result<Vec<_>, _> =
                    exprs.into_iter().map(|e| e.into_expr(registry)).collect();
                Ok(Expr::Or(children?))
            }
            BuilderExpr::Not(inner) => Ok(Expr::Not(Box::new(inner.into_expr(registry)?))),
        }
    }

    fn validate_condition(
        cond: &ConditionBuilder,
        registry: &FieldRegistry,
    ) -> Result<(), BuildError> {
        // Get field descriptor (resolves aliases)
        let descriptor = registry
            .get(cond.field())
            .ok_or_else(|| BuildError::UnknownField {
                field: cond.field().to_string(),
                available: registry.field_names().join(", "),
            })?;

        // Check operator is valid for field type
        if !descriptor.supports_operator(cond.operator()) {
            return Err(BuildError::InvalidOperator {
                field: cond.field().to_string(),
                operator: cond.operator().clone(),
                field_type: format!("{:?}", descriptor.field_type),
            });
        }

        // Check value type matches field type
        Self::validate_value_type(cond.field(), &descriptor.field_type, cond.value())?;

        // Validate regex patterns early
        // This catches invalid patterns from convenience methods like name_matches()
        Self::validate_regex_pattern(cond.value())?;

        // Check enum constraints for applicable fields
        Self::validate_enum_value(cond.field(), cond.value(), &descriptor.field_type)?;

        Ok(())
    }

    fn validate_regex_pattern(value: &Value) -> Result<(), BuildError> {
        if let Value::Regex(regex_value) = value {
            // Check if pattern contains lookaround assertions (FT-C.1: Support lookaround)
            // Aligned with validator.rs behavior to accept the same patterns.
            let has_lookaround = regex_value.pattern.contains("(?=")
                || regex_value.pattern.contains("(?!")
                || regex_value.pattern.contains("(?<=")
                || regex_value.pattern.contains("(?<!");

            if has_lookaround {
                // Use fancy-regex for lookaround support
                fancy_regex::Regex::new(&regex_value.pattern).map_err(|e| {
                    BuildError::InvalidFancyRegex {
                        pattern: regex_value.pattern.clone(),
                        error: e.to_string(),
                    }
                })?;
            } else {
                // Use standard regex for performance (validate with flags applied)
                let mut builder = regex::RegexBuilder::new(&regex_value.pattern);
                builder.case_insensitive(regex_value.flags.case_insensitive);
                builder.multi_line(regex_value.flags.multiline);
                builder.dot_matches_new_line(regex_value.flags.dot_all);
                builder.build()?;
            }
        }
        Ok(())
    }

    fn validate_enum_value(
        field: &str,
        value: &Value,
        field_type: &FieldType,
    ) -> Result<(), BuildError> {
        // Extract enum values from the field type
        // This ensures validation stays in sync with FieldRegistry::core_fields()
        if let (FieldType::Enum(valid), Value::String(s)) = (field_type, value)
            && !valid.contains(&s.as_str())
        {
            return Err(BuildError::InvalidEnumValue {
                field: field.to_string(),
                value: s.clone(),
                valid: valid.join(", "),
            });
        }

        Ok(())
    }

    fn validate_value_type(
        field: &str,
        field_type: &FieldType,
        value: &Value,
    ) -> Result<(), BuildError> {
        // Match value types to field types, aligned with validator.rs behavior.
        // Regex values are valid for String, Path, and Enum fields (e.g., kind~=/function|method/).
        let is_valid = matches!(
            (field_type, value),
            (
                FieldType::String | FieldType::Path | FieldType::Enum(_),
                Value::String(_) | Value::Regex(_)
            ) | (FieldType::Number, Value::Number(_))
                | (FieldType::Bool, Value::Boolean(_)) // enum regex: kind~=/function|method/
        );

        if !is_valid {
            return Err(BuildError::ValueTypeMismatch {
                field: field.to_string(),
                expected: format!("{field_type:?}"),
                actual: value.type_name().to_string(),
            });
        }

        Ok(())
    }
}

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

// ============================================================================
// Conversion from Value types for generic field() method
// ============================================================================

impl From<&str> for Value {
    fn from(s: &str) -> Self {
        Value::String(s.to_string())
    }
}

impl From<String> for Value {
    fn from(s: String) -> Self {
        Value::String(s)
    }
}

impl From<i64> for Value {
    fn from(n: i64) -> Self {
        Value::Number(n)
    }
}

impl From<bool> for Value {
    fn from(b: bool) -> Self {
        Value::Boolean(b)
    }
}