Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit aaf8c52

Browse files
oppiliappanHendrik van Antwerpen
authored andcommitted
move Filter trait into serde module
1 parent 2ec09d2 commit aaf8c52

File tree

2 files changed

+111
-92
lines changed

2 files changed

+111
-92
lines changed

stack-graphs/src/json.rs

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -35,71 +35,16 @@ use crate::partial::PartialScopedSymbol;
3535
use crate::partial::PartialSymbolStack;
3636
use crate::partial::ScopeStackVariable;
3737
use crate::partial::SymbolStackVariable;
38+
pub use crate::serde::{Filter, NoFilter};
3839
use crate::stitching::Database;
3940

4041
#[derive(Debug, Error)]
4142
#[error(transparent)]
4243
pub struct JsonError(#[from] serde_json::error::Error);
4344

44-
//-----------------------------------------------------------------------------
45-
// Filter
46-
47-
pub trait Filter {
48-
/// Return whether elements for the given file must be included.
49-
fn include_file(&self, graph: &StackGraph, file: &Handle<File>) -> bool;
50-
51-
/// Return whether the given node must be included.
52-
/// Nodes of excluded files are always excluded.
53-
fn include_node(&self, graph: &StackGraph, node: &Handle<Node>) -> bool;
54-
55-
/// Return whether the given edge must be included.
56-
/// Edges via excluded nodes are always excluded.
57-
fn include_edge(&self, graph: &StackGraph, source: &Handle<Node>, sink: &Handle<Node>) -> bool;
58-
59-
/// Return whether the given path must be included.
60-
/// Paths via excluded nodes or edges are always excluded.
61-
fn include_partial_path(
62-
&self,
63-
graph: &StackGraph,
64-
paths: &PartialPaths,
65-
path: &PartialPath,
66-
) -> bool;
67-
}
68-
69-
impl<F> Filter for F
70-
where
71-
F: Fn(&StackGraph, &Handle<File>) -> bool,
72-
{
73-
fn include_file(&self, graph: &StackGraph, file: &Handle<File>) -> bool {
74-
self(graph, file)
75-
}
76-
77-
fn include_node(&self, _graph: &StackGraph, _node: &Handle<Node>) -> bool {
78-
true
79-
}
80-
81-
fn include_edge(
82-
&self,
83-
_graph: &StackGraph,
84-
_source: &Handle<Node>,
85-
_sink: &Handle<Node>,
86-
) -> bool {
87-
true
88-
}
89-
90-
fn include_partial_path(
91-
&self,
92-
_graph: &StackGraph,
93-
_paths: &PartialPaths,
94-
_path: &PartialPath,
95-
) -> bool {
96-
true
97-
}
98-
}
99-
10045
/// Filter implementation that enforces all implications of another filter.
10146
/// For example, that nodes frome excluded files are not included, etc.
102-
struct ImplicationFilter<'a>(&'a dyn Filter);
47+
pub(crate) struct ImplicationFilter<'a>(pub &'a dyn Filter);
10348

10449
impl Filter for ImplicationFilter<'_> {
10550
fn include_file(&self, graph: &StackGraph, file: &Handle<File>) -> bool {
@@ -144,37 +89,6 @@ impl Filter for ImplicationFilter<'_> {
14489
}
14590
}
14691

147-
// Filter implementation that includes everything.
148-
pub struct NoFilter;
149-
150-
impl Filter for NoFilter {
151-
fn include_file(&self, _graph: &StackGraph, _file: &Handle<File>) -> bool {
152-
true
153-
}
154-
155-
fn include_node(&self, _graph: &StackGraph, _node: &Handle<Node>) -> bool {
156-
true
157-
}
158-
159-
fn include_edge(
160-
&self,
161-
_graph: &StackGraph,
162-
_source: &Handle<Node>,
163-
_sink: &Handle<Node>,
164-
) -> bool {
165-
true
166-
}
167-
168-
fn include_partial_path(
169-
&self,
170-
_graph: &StackGraph,
171-
_paths: &PartialPaths,
172-
_path: &PartialPath,
173-
) -> bool {
174-
true
175-
}
176-
}
177-
17892
//-----------------------------------------------------------------------------
17993
// InStackGraph
18094

stack-graphs/src/serde.rs

Lines changed: 109 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,106 @@
1-
use crate::{
2-
arena::Handle,
3-
json::{Filter, NoFilter},
4-
};
1+
use crate::arena::Handle;
2+
pub use filter::{Filter, NoFilter};
53

64
use serde::{Deserialize, Serialize};
75
use thiserror::Error;
86

7+
mod filter {
8+
use crate::{
9+
arena::Handle,
10+
graph::{File, Node, StackGraph},
11+
partial::{PartialPath, PartialPaths},
12+
};
13+
14+
pub trait Filter {
15+
/// Return whether elements for the given file must be included.
16+
fn include_file(&self, graph: &StackGraph, file: &Handle<File>) -> bool;
17+
18+
/// Return whether the given node must be included.
19+
/// Nodes of excluded files are always excluded.
20+
fn include_node(&self, graph: &StackGraph, node: &Handle<Node>) -> bool;
21+
22+
/// Return whether the given edge must be included.
23+
/// Edges via excluded nodes are always excluded.
24+
fn include_edge(
25+
&self,
26+
graph: &StackGraph,
27+
source: &Handle<Node>,
28+
sink: &Handle<Node>,
29+
) -> bool;
30+
31+
/// Return whether the given path must be included.
32+
/// Paths via excluded nodes or edges are always excluded.
33+
fn include_partial_path(
34+
&self,
35+
graph: &StackGraph,
36+
paths: &PartialPaths,
37+
path: &PartialPath,
38+
) -> bool;
39+
}
40+
41+
impl<F> Filter for F
42+
where
43+
F: Fn(&StackGraph, &Handle<File>) -> bool,
44+
{
45+
fn include_file(&self, graph: &StackGraph, file: &Handle<File>) -> bool {
46+
self(graph, file)
47+
}
48+
49+
fn include_node(&self, _graph: &StackGraph, _node: &Handle<Node>) -> bool {
50+
true
51+
}
52+
53+
fn include_edge(
54+
&self,
55+
_graph: &StackGraph,
56+
_source: &Handle<Node>,
57+
_sink: &Handle<Node>,
58+
) -> bool {
59+
true
60+
}
61+
62+
fn include_partial_path(
63+
&self,
64+
_graph: &StackGraph,
65+
_paths: &PartialPaths,
66+
_path: &PartialPath,
67+
) -> bool {
68+
true
69+
}
70+
}
71+
72+
// Filter implementation that includes everything.
73+
pub struct NoFilter;
74+
75+
impl Filter for NoFilter {
76+
fn include_file(&self, _graph: &StackGraph, _file: &Handle<File>) -> bool {
77+
true
78+
}
79+
80+
fn include_node(&self, _graph: &StackGraph, _node: &Handle<Node>) -> bool {
81+
true
82+
}
83+
84+
fn include_edge(
85+
&self,
86+
_graph: &StackGraph,
87+
_source: &Handle<Node>,
88+
_sink: &Handle<Node>,
89+
) -> bool {
90+
true
91+
}
92+
93+
fn include_partial_path(
94+
&self,
95+
_graph: &StackGraph,
96+
_paths: &PartialPaths,
97+
_path: &PartialPath,
98+
) -> bool {
99+
true
100+
}
101+
}
102+
}
103+
9104
#[derive(Serialize, Deserialize, PartialEq, Eq, Debug, Default, Clone)]
10105
pub struct StackGraph {
11106
files: Files,
@@ -497,6 +592,7 @@ mod test {
497592
use super::*;
498593

499594
#[test]
595+
#[cfg(feature = "json")]
500596
fn serde_json_stack_graph() {
501597
let expected = StackGraph {
502598
files: Files {
@@ -601,6 +697,7 @@ mod test {
601697
}
602698

603699
#[test]
700+
#[cfg(feature = "json")]
604701
fn reconstruct() {
605702
let json_data = serde_json::json!(
606703
{
@@ -721,6 +818,14 @@ mod test {
721818

722819
assert_eq!(sg.iter_nodes().count(), 3);
723820
assert_eq!(sg.iter_files().count(), 1);
821+
822+
// the scope node should contain debug and source info
823+
let handle = sg
824+
.iter_nodes()
825+
.find(|handle| matches!(sg[*handle], crate::graph::Node::Scope(..)))
826+
.unwrap();
827+
assert!(sg.source_info(handle).is_some());
828+
assert!(sg.debug_info(handle).is_some());
724829
}
725830

726831
#[test]

0 commit comments

Comments
 (0)