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
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
//! Post-processing filters for query results.
//!
//! DiscoveryQueryでは表現できない条件を後処理で適用する。
use glob::Pattern as GlobPattern;
use ryo_analysis::{AnalysisContext, DiscoveredSymbol, Pattern, Visibility as AnalysisVisibility};
use crate::converter::PostFilter;
use crate::schema::{GenericsMatch, ReceiverKind, Visibility as RyoQlVisibility};
/// 後処理フィルタプロセッサ
pub struct PostFilterProcessor<'a> {
ctx: &'a AnalysisContext,
}
impl<'a> PostFilterProcessor<'a> {
/// 新しいプロセッサを作成
pub fn new(ctx: &'a AnalysisContext) -> Self {
Self { ctx }
}
/// 後処理フィルタを適用
///
/// DetailStoreを使用してシンボル詳細を取得し、各フィルタを適用する。
pub fn apply(
&self,
results: Vec<DiscoveredSymbol>,
filters: &[PostFilter],
) -> Vec<DiscoveredSymbol> {
if filters.is_empty() {
return results;
}
let mut filtered = results;
for filter in filters {
match filter {
PostFilter::IsAsync(expected) => {
filtered = self.filter_by_async(filtered, *expected);
}
PostFilter::IsUnsafe(expected) => {
filtered = self.filter_by_unsafe(filtered, *expected);
}
PostFilter::Visibility(vis) => {
filtered = self.filter_by_visibility(filtered, vis);
}
PostFilter::Receiver(receiver) => {
filtered = self.filter_by_receiver(filtered, receiver);
}
PostFilter::Attributes(attrs) => {
filtered = self.filter_by_attributes(filtered, attrs);
}
PostFilter::Generics(generics) => {
filtered = self.filter_by_generics(filtered, generics);
}
PostFilter::PathInclude(pattern) => {
filtered = self.filter_by_path_include(filtered, pattern);
}
PostFilter::PathExclude(pattern) => {
filtered = self.filter_by_path_exclude(filtered, pattern);
}
PostFilter::PatternSearch(_) => {
// Phase 2で実装(Pattern Registry連携後)
}
PostFilter::OnEmpty => {
// Executor側で処理(try_recovery)
}
PostFilter::ReturnType(pattern) => {
filtered = self.filter_by_return_type(filtered, pattern);
}
PostFilter::ParamType(pattern) => {
filtered = self.filter_by_param_type(filtered, pattern);
}
PostFilter::FieldType(pattern) => {
filtered = self.filter_by_field_type(filtered, pattern);
}
PostFilter::Parent(pattern) => {
filtered = self.filter_by_parent(filtered, pattern);
}
PostFilter::SymbolId(ref sid_str) => {
filtered = self.filter_by_symbol_id(filtered, sid_str);
}
PostFilter::BodyMatch(ref body_match) => {
filtered = self.filter_by_body(filtered, body_match);
}
PostFilter::Relations(ref relations) => {
filtered = self.filter_by_relations(filtered, relations);
}
}
}
filtered
}
/// 戻り値型でフィルタ (DetailStore使用)
fn filter_by_return_type(
&self,
results: Vec<DiscoveredSymbol>,
pattern: &str,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
if let Some(detail) = self.ctx.detail_store.function(symbol.id) {
if let Some(ref ret_type) = detail.return_type {
return matches_pattern(ret_type, pattern);
}
}
false
})
.collect()
}
/// パラメータ型でフィルタ (DetailStore使用)
fn filter_by_param_type(
&self,
results: Vec<DiscoveredSymbol>,
pattern: &str,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
if let Some(detail) = self.ctx.detail_store.function(symbol.id) {
for param in &detail.params {
if matches_pattern(¶m.ty, pattern) {
return true;
}
}
}
false
})
.collect()
}
/// フィールド型でフィルタ (DetailStore使用)
fn filter_by_field_type(
&self,
results: Vec<DiscoveredSymbol>,
pattern: &str,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
// Struct fields
if let Some(detail) = self.ctx.detail_store.struct_(symbol.id) {
for field in &detail.fields {
if matches_pattern(&field.ty, pattern) {
return true;
}
}
}
// Enum variant fields
if let Some(detail) = self.ctx.detail_store.enum_(symbol.id) {
for variant in &detail.variants {
for field in &variant.fields {
if matches_pattern(&field.ty, pattern) {
return true;
}
}
}
}
false
})
.collect()
}
/// async関数でフィルタ
fn filter_by_async(
&self,
results: Vec<DiscoveredSymbol>,
expected: bool,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
if let Some(detail) = self.ctx.detail_store.function(symbol.id) {
detail.is_async == expected
} else {
// 関数でない場合はasync=falseとみなす
!expected
}
})
.collect()
}
/// unsafe関数でフィルタ
fn filter_by_unsafe(
&self,
results: Vec<DiscoveredSymbol>,
expected: bool,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
// 関数の場合
if let Some(detail) = self.ctx.detail_store.function(symbol.id) {
return detail.is_unsafe == expected;
}
// トレイトの場合
if let Some(detail) = self.ctx.detail_store.trait_(symbol.id) {
return detail.is_unsafe == expected;
}
// impl の場合
if let Some(detail) = self.ctx.detail_store.impl_(symbol.id) {
return detail.is_unsafe == expected;
}
// その他はunsafe=falseとみなす
!expected
})
.collect()
}
/// 可視性でフィルタ
fn filter_by_visibility(
&self,
results: Vec<DiscoveredSymbol>,
expected: &RyoQlVisibility,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
if let Some(vis) = &symbol.visibility {
matches_visibility(vis, expected)
} else if let Some(vis) = self.ctx.registry.visibility(symbol.id) {
matches_visibility(vis, expected)
} else {
// 可視性情報がない場合はPrivateとみなす
matches!(expected, RyoQlVisibility::Private)
}
})
.collect()
}
/// レシーバー種別でフィルタ
fn filter_by_receiver(
&self,
results: Vec<DiscoveredSymbol>,
expected: &ReceiverKind,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
if let Some(detail) = self.ctx.detail_store.function(symbol.id) {
let actual = get_receiver_kind(&detail.params);
actual == *expected
} else {
// 関数でない場合はNone(関連関数扱い)
*expected == ReceiverKind::None
}
})
.collect()
}
/// アトリビュートでフィルタ
///
/// シンボルに指定された全てのアトリビュートが含まれている場合にマッチ。
/// 例: `attributes: ["deprecated"]` → `#[deprecated]` を持つシンボルにマッチ
fn filter_by_attributes(
&self,
results: Vec<DiscoveredSymbol>,
expected_attrs: &[String],
) -> Vec<DiscoveredSymbol> {
if expected_attrs.is_empty() {
return results;
}
results
.into_iter()
.filter(|symbol| {
let actual_attrs = self.get_attrs(symbol.id);
// 全ての期待するアトリビュートが存在するか確認
expected_attrs.iter().all(|expected| {
actual_attrs.iter().any(|actual| {
// 完全一致またはパターンマッチ
actual == expected || matches_pattern(actual, expected)
})
})
})
.collect()
}
/// シンボルのアトリビュート情報を取得
fn get_attrs(&self, id: ryo_analysis::SymbolId) -> Vec<String> {
if let Some(detail) = self.ctx.detail_store.function(id) {
return detail.attrs.clone();
}
if let Some(detail) = self.ctx.detail_store.struct_(id) {
return detail.attrs.clone();
}
if let Some(detail) = self.ctx.detail_store.enum_(id) {
return detail.attrs.clone();
}
if let Some(detail) = self.ctx.detail_store.trait_(id) {
return detail.attrs.clone();
}
if let Some(detail) = self.ctx.detail_store.impl_(id) {
return detail.attrs.clone();
}
Vec::new()
}
/// ジェネリクス条件でフィルタ
fn filter_by_generics(
&self,
results: Vec<DiscoveredSymbol>,
expected: &GenericsMatch,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
let generics = self.get_generics(symbol.id);
matches_generics(&generics, expected)
})
.collect()
}
/// シンボルのジェネリクス情報を取得
fn get_generics(&self, id: ryo_analysis::SymbolId) -> Option<ryo_analysis::GenericInfo> {
if let Some(detail) = self.ctx.detail_store.function(id) {
return Some(detail.generics.clone());
}
if let Some(detail) = self.ctx.detail_store.struct_(id) {
return Some(detail.generics.clone());
}
if let Some(detail) = self.ctx.detail_store.enum_(id) {
return Some(detail.generics.clone());
}
if let Some(detail) = self.ctx.detail_store.trait_(id) {
return Some(detail.generics.clone());
}
if let Some(detail) = self.ctx.detail_store.impl_(id) {
return Some(detail.generics.clone());
}
None
}
/// パスincludeでフィルタ
fn filter_by_path_include(
&self,
results: Vec<DiscoveredSymbol>,
pattern: &str,
) -> Vec<DiscoveredSymbol> {
let Ok(glob) = GlobPattern::new(pattern) else {
return results; // パターン不正の場合はスキップ
};
results
.into_iter()
.filter(|symbol| {
if let Some(ref span) = symbol.span {
glob.matches(span.file.as_relative().to_string_lossy().as_ref())
} else {
false // spanがない場合は除外
}
})
.collect()
}
/// パスexcludeでフィルタ
fn filter_by_path_exclude(
&self,
results: Vec<DiscoveredSymbol>,
pattern: &str,
) -> Vec<DiscoveredSymbol> {
let Ok(glob) = GlobPattern::new(pattern) else {
return results; // パターン不正の場合はスキップ
};
results
.into_iter()
.filter(|symbol| {
if let Some(ref span) = symbol.span {
!glob.matches(span.file.as_relative().to_string_lossy().as_ref())
} else {
true // spanがない場合は保持
}
})
.collect()
}
/// 親シンボルでフィルタ(Variant, Field, Method用)
///
/// シンボルパスから親のパスを抽出し、パターンとマッチング。
/// 例: `ryo_analysis::Filter::Include` → 親は `Filter`
fn filter_by_parent(
&self,
results: Vec<DiscoveredSymbol>,
pattern: &Pattern,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
// シンボルパスから親の名前を取得
let path_str = symbol.path.to_string();
let parts: Vec<&str> = path_str.split("::").collect();
// 親は最後から2番目のセグメント
if parts.len() >= 2 {
let parent_name = parts[parts.len() - 2];
pattern.matches(parent_name)
} else {
// 親がない(トップレベル)場合は除外
false
}
})
.collect()
}
/// SymbolIdでフィルタ(直接lookup)
///
/// "165v1" または "SymbolId(165v1)" 形式の文字列を解析し、
/// 一致するシンボルのみを返す。
fn filter_by_symbol_id(
&self,
results: Vec<DiscoveredSymbol>,
sid_str: &str,
) -> Vec<DiscoveredSymbol> {
let Some(target_id) = ryo_analysis::SymbolId::parse(sid_str) else {
// パースできない場合は結果を空にする(無効なIDで全件返すのは誤り)
return vec![];
};
results
.into_iter()
.filter(|symbol| symbol.id == target_id)
.collect()
}
/// Body パターンマッチフィルタ
///
/// ASTRegistry から関数bodyを取得し、BodyScanner でパターンをチェック。
/// - `contains`: 各パターンに1つ以上マッチが必要
/// - `not_contains`: 各パターンにマッチが0件であること
/// - `all_of`: 全パターンに1つ以上マッチが必要
fn filter_by_body(
&self,
results: Vec<DiscoveredSymbol>,
body_match: &ryo_pattern::BodyMatch,
) -> Vec<DiscoveredSymbol> {
use ryo_pattern::BodyScanner;
use ryo_source::pure::PureItem;
results
.into_iter()
.filter(|symbol| {
// ASTRegistry から PureFn を取得
let Some(PureItem::Fn(fn_item)) = self.ctx.ast_registry.get(symbol.id) else {
// 関数以外はbodyがないため除外
return false;
};
// contains: 各パターンに1つ以上マッチが必要
if let Some(ref patterns) = body_match.contains {
for pattern in patterns {
let scanner = BodyScanner::new(pattern);
if scanner.scan_fn(fn_item).is_empty() {
return false;
}
}
}
// not_contains: 各パターンにマッチが0件であること
if let Some(ref patterns) = body_match.not_contains {
for pattern in patterns {
let scanner = BodyScanner::new(pattern);
if !scanner.scan_fn(fn_item).is_empty() {
return false;
}
}
}
// all_of: 全パターンに1つ以上マッチが必要(containsと同じロジック)
if let Some(ref patterns) = body_match.all_of {
for pattern in patterns {
let scanner = BodyScanner::new(pattern);
if scanner.scan_fn(fn_item).is_empty() {
return false;
}
}
}
true
})
.collect()
}
/// Relations フィルタ (any/all/none)
///
/// グラフAPIを使用して関係を照会し、ターゲット条件にマッチするか確認。
/// - `any`: 少なくとも1つの条件がマッチ
/// - `all`: 全条件がマッチ
/// - `none`: どの条件もマッチしない
fn filter_by_relations(
&self,
results: Vec<DiscoveredSymbol>,
relations: &ryo_pattern::Relations,
) -> Vec<DiscoveredSymbol> {
results
.into_iter()
.filter(|symbol| {
// any: 少なくとも1つの条件がマッチ
if let Some(ref conditions) = relations.any {
if !conditions
.iter()
.any(|rel| self.check_relation(symbol.id, rel))
{
return false;
}
}
// all: 全条件がマッチ
if let Some(ref conditions) = relations.all {
if !conditions
.iter()
.all(|rel| self.check_relation(symbol.id, rel))
{
return false;
}
}
// none: どの条件もマッチしない
if let Some(ref conditions) = relations.none {
if conditions
.iter()
.any(|rel| self.check_relation(symbol.id, rel))
{
return false;
}
}
true
})
.collect()
}
/// 単一の関係条件をチェック
fn check_relation(
&self,
source_id: ryo_analysis::SymbolId,
relation: &ryo_pattern::Relation,
) -> bool {
use ryo_pattern::RelationKind;
let related_ids: Vec<ryo_analysis::SymbolId> = match relation.kind {
RelationKind::Calls => self.ctx.code_graph.callees_of(source_id).collect(),
RelationKind::CalledBy => self.ctx.code_graph.callers_of(source_id).collect(),
RelationKind::TypeReferences => {
self.ctx.typeflow_graph.types_used_by(source_id).collect()
}
RelationKind::TypeReferencedBy => {
self.ctx.typeflow_graph.type_users(source_id).collect()
}
RelationKind::Implements => {
// outgoing Implements edges (Impl → Trait)
let mut result: Vec<ryo_analysis::SymbolId> = self
.ctx
.code_graph
.outgoing_edges(source_id)
.filter(|e| e.kind == ryo_analysis::CodeEdgeV2::Implements)
.map(|e| e.to)
.collect();
// For Struct/Enum: find Impl blocks that target this type,
// then follow their Implements edges to Traits
if result.is_empty() {
let source_kind = self.ctx.registry.kind(source_id);
if matches!(
source_kind,
Some(ryo_analysis::SymbolKind::Struct)
| Some(ryo_analysis::SymbolKind::Enum)
) {
if let Some(source_path) = self.ctx.registry.resolve(source_id) {
let source_name = source_path.name();
// Find Impl blocks whose self_ty matches this type
for impl_id in self
.ctx
.registry
.iter_by_kind(ryo_analysis::SymbolKind::Impl)
{
if let Some(impl_path) = self.ctx.registry.resolve(impl_id) {
if let Some(last_seg) = impl_path.segment_refs().last() {
if let Some(self_ty) = last_seg.impl_self_ty() {
// Strip generics: "Writer < '_ >" → "Writer"
let base =
self_ty.split('<').next().unwrap_or(self_ty).trim();
if base == source_name {
// Follow this Impl's Implements edges
for e in self.ctx.code_graph.outgoing_edges(impl_id)
{
if e.kind
== ryo_analysis::CodeEdgeV2::Implements
{
result.push(e.to);
}
}
}
}
}
}
}
}
}
}
result
}
RelationKind::ImplementedBy => {
// Impl blocks that implement this Trait
let impl_ids: Vec<ryo_analysis::SymbolId> =
self.ctx.code_graph.implementors_of(source_id).collect();
// Also resolve Impl → target Struct/Enum for broader matching
let mut result = impl_ids.clone();
for &impl_id in &impl_ids {
if let Some(impl_path) = self.ctx.registry.resolve(impl_id) {
if let Some(last_seg) = impl_path.segment_refs().last() {
if let Some(self_ty) = last_seg.impl_self_ty() {
let base = self_ty.split('<').next().unwrap_or(self_ty).trim();
// Find the Struct/Enum with this name
if let Some(struct_id) = self.ctx.registry.lookup_by_name(base) {
let kind = self.ctx.registry.kind(struct_id);
if matches!(
kind,
Some(ryo_analysis::SymbolKind::Struct)
| Some(ryo_analysis::SymbolKind::Enum)
) {
result.push(struct_id);
}
}
}
}
}
}
result
}
RelationKind::Contains => self.ctx.code_graph.children_of(source_id).collect(),
RelationKind::ContainedBy => self
.ctx
.code_graph
.parent_of(source_id)
.into_iter()
.collect(),
};
// ターゲット条件にマッチする関連シンボルが存在するか
related_ids
.iter()
.any(|&related_id| self.matches_target(related_id, &relation.target))
}
/// ターゲット条件にマッチするかチェック
fn matches_target(
&self,
id: ryo_analysis::SymbolId,
target: &ryo_pattern::RelationTarget,
) -> bool {
// kind フィルタ
if let Some(ref target_kind) = target.kind {
let actual_kind = self
.ctx
.registry
.kind(id)
.unwrap_or(ryo_analysis::SymbolKind::Other);
if !matches_target_kind(&actual_kind, target_kind) {
return false;
}
}
// name/pattern マッチ
if let Some(ref target_match) = target.r#match {
let Some(path) = self.ctx.registry.resolve(id) else {
return false;
};
let name = path.name();
if let Some(ref exact_name) = target_match.name {
if name != exact_name {
return false;
}
}
if let Some(ref pattern) = target_match.pattern {
if !matches_pattern(name, pattern) {
return false;
}
}
if let Some(ref regex_str) = target_match.regex {
if let Ok(re) = regex::Regex::new(regex_str) {
if !re.is_match(name) {
return false;
}
}
}
}
true
}
}
// =============================================================================
// Helper Functions
// =============================================================================
/// TargetKind → SymbolKind マッチング
fn matches_target_kind(
actual: &ryo_analysis::SymbolKind,
expected: &ryo_pattern::TargetKind,
) -> bool {
use ryo_analysis::SymbolKind;
use ryo_pattern::TargetKind;
matches!(
(actual, expected),
(SymbolKind::Function, TargetKind::Function)
| (SymbolKind::Method, TargetKind::Function) // Method も Function としてマッチ
| (SymbolKind::Struct, TargetKind::Struct)
| (SymbolKind::Enum, TargetKind::Enum)
| (SymbolKind::Trait, TargetKind::Trait)
| (SymbolKind::Impl, TargetKind::Impl)
| (SymbolKind::Mod, TargetKind::Mod)
| (SymbolKind::Const, TargetKind::Const)
| (SymbolKind::Static, TargetKind::Static)
| (SymbolKind::TypeAlias, TargetKind::TypeAlias)
)
}
/// パターンマッチング (glob-style)
fn matches_pattern(text: &str, pattern: &str) -> bool {
// Handle special patterns
if pattern == "*" {
return true;
}
// Handle *contains* pattern
if pattern.starts_with('*') && pattern.ends_with('*') && pattern.len() > 2 {
let inner = &pattern[1..pattern.len() - 1];
return text.contains(inner);
}
// Handle prefix* pattern
if pattern.ends_with('*') && !pattern.starts_with('*') {
let prefix = &pattern[..pattern.len() - 1];
return text.starts_with(prefix);
}
// Handle *suffix pattern
if pattern.starts_with('*') && !pattern.ends_with('*') {
let suffix = &pattern[1..];
return text.ends_with(suffix);
}
// Handle regex: pattern
if let Some(regex_str) = pattern.strip_prefix("regex:") {
if let Ok(re) = regex::Regex::new(regex_str) {
return re.is_match(text);
}
return false;
}
// Exact match
text == pattern
}
/// AnalysisVisibility から RyoQL Visibility へのマッチング
fn matches_visibility(actual: &AnalysisVisibility, expected: &RyoQlVisibility) -> bool {
match (actual, expected) {
(AnalysisVisibility::Public, RyoQlVisibility::Public) => true,
(AnalysisVisibility::Private, RyoQlVisibility::Private) => true,
(AnalysisVisibility::Crate, RyoQlVisibility::Crate) => true,
(AnalysisVisibility::Super, RyoQlVisibility::Super) => true,
(
AnalysisVisibility::Restricted(actual_path),
RyoQlVisibility::Restricted(expected_path),
) => actual_path.to_string() == *expected_path,
_ => false,
}
}
/// パラメータリストからレシーバー種別を判定
fn get_receiver_kind(params: &[ryo_analysis::ParamInfo]) -> ReceiverKind {
params.first().map_or(ReceiverKind::None, |first| {
if !first.is_self {
return ReceiverKind::None;
}
// self パラメータの型から判定
if first.ty.starts_with("&mut ") {
ReceiverKind::MutRef
} else if first.ty.starts_with('&') {
ReceiverKind::Ref
} else {
ReceiverKind::Owned
}
})
}
/// ジェネリクス条件のマッチング
fn matches_generics(actual: &Option<ryo_analysis::GenericInfo>, expected: &GenericsMatch) -> bool {
let Some(actual) = actual else {
// ジェネリクスがない場合、条件もなければマッチ
return expected.params.is_none()
&& expected.bounds.is_none()
&& expected.lifetimes.is_none();
};
// params チェック
if let Some(ref expected_params) = expected.params {
for expected_param in expected_params {
if !actual.type_params.contains(expected_param) {
return false;
}
}
}
// lifetimes チェック
if let Some(ref expected_lifetimes) = expected.lifetimes {
for expected_lt in expected_lifetimes {
if !actual.lifetimes.contains(expected_lt) {
return false;
}
}
}
// bounds は TypeFilter で既に処理されているためここではスキップ
// (GenericsMatch.bounds は None になっている)
true
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_matches_pattern_exact() {
assert!(matches_pattern("Result", "Result"));
assert!(!matches_pattern("Result", "Option"));
}
#[test]
fn test_matches_pattern_wildcard() {
assert!(matches_pattern("anything", "*"));
}
#[test]
fn test_matches_pattern_contains() {
assert!(matches_pattern("MyResult", "*Result*"));
assert!(matches_pattern("ResultWrapper", "*Result*"));
assert!(!matches_pattern("Option", "*Result*"));
}
#[test]
fn test_matches_pattern_prefix() {
assert!(matches_pattern("ResultType", "Result*"));
assert!(!matches_pattern("MyResult", "Result*"));
}
#[test]
fn test_matches_pattern_suffix() {
assert!(matches_pattern("MyResult", "*Result"));
assert!(!matches_pattern("ResultType", "*Result"));
}
}