1
1
use crate :: intern:: { self , InternedStr } ;
2
2
use crate :: state:: {
3
- format_location,
4
3
store:: { self , Id , SpanId , Store } ,
5
4
Attribute , Field , Metadata , Visibility ,
6
5
} ;
@@ -14,6 +13,8 @@ use std::{
14
13
time:: { Duration , SystemTime } ,
15
14
} ;
16
15
16
+ use super :: Location ;
17
+
17
18
#[ derive( Default , Debug ) ]
18
19
pub ( crate ) struct ResourcesState {
19
20
resources : Store < Resource > ,
@@ -29,11 +30,15 @@ pub(crate) enum TypeVisibility {
29
30
#[ derive( Debug , Copy , Clone ) ]
30
31
#[ repr( usize ) ]
31
32
pub ( crate ) enum SortBy {
32
- Rid = 0 ,
33
- Kind = 1 ,
34
- ConcreteType = 2 ,
35
- Target = 3 ,
36
- Total = 4 ,
33
+ Id = 0 ,
34
+ ParentId = 1 ,
35
+ Kind = 2 ,
36
+ Total = 3 ,
37
+ Target = 4 ,
38
+ ConcreteType = 5 ,
39
+ Visibility = 6 ,
40
+ Location = 7 ,
41
+ Attributes = 8 ,
37
42
}
38
43
39
44
#[ derive( Debug ) ]
@@ -55,7 +60,7 @@ pub(crate) struct Resource {
55
60
stats : ResourceStats ,
56
61
target : InternedStr ,
57
62
concrete_type : InternedStr ,
58
- location : String ,
63
+ location : Location ,
59
64
visibility : TypeVisibility ,
60
65
}
61
66
@@ -71,27 +76,50 @@ struct ResourceStats {
71
76
72
77
impl Default for SortBy {
73
78
fn default ( ) -> Self {
74
- Self :: Rid
79
+ Self :: Id
75
80
}
76
81
}
77
82
78
83
impl SortBy {
79
84
pub fn sort ( & self , now : SystemTime , resources : & mut [ ResourceRef ] ) {
80
85
match self {
81
- Self :: Rid => {
86
+ Self :: Id => {
82
87
resources. sort_unstable_by_key ( |resource| resource. upgrade ( ) . map ( |r| r. borrow ( ) . id ) )
83
88
}
89
+ Self :: ParentId => resources. sort_unstable_by_key ( |resource| {
90
+ resource. upgrade ( ) . map ( |r| r. borrow ( ) . parent_id . clone ( ) )
91
+ } ) ,
84
92
Self :: Kind => resources. sort_unstable_by_key ( |resource| {
85
93
resource. upgrade ( ) . map ( |r| r. borrow ( ) . kind . clone ( ) )
86
94
} ) ,
95
+ Self :: Total => resources
96
+ . sort_unstable_by_key ( |resource| resource. upgrade ( ) . map ( |r| r. borrow ( ) . total ( now) ) ) ,
97
+ Self :: Target => resources. sort_unstable_by_key ( |resource| {
98
+ resource. upgrade ( ) . map ( |r| r. borrow ( ) . target . clone ( ) )
99
+ } ) ,
87
100
Self :: ConcreteType => resources. sort_unstable_by_key ( |resource| {
88
101
resource. upgrade ( ) . map ( |r| r. borrow ( ) . concrete_type . clone ( ) )
89
102
} ) ,
90
- Self :: Target => resources. sort_unstable_by_key ( |resource| {
91
- resource. upgrade ( ) . map ( |r| r. borrow ( ) . target . clone ( ) )
103
+ Self :: Visibility => resources
104
+ . sort_unstable_by_key ( |resource| resource. upgrade ( ) . map ( |r| r. borrow ( ) . visibility ) ) ,
105
+ Self :: Location => resources. sort_unstable_by_key ( |resource| {
106
+ resource. upgrade ( ) . map ( |r| r. borrow ( ) . location . clone ( ) )
107
+ } ) ,
108
+ Self :: Attributes => resources. sort_unstable_by_key ( |resource| {
109
+ resource. upgrade ( ) . map ( |r| {
110
+ // FIXME - we are taking only the first span as sorting key here,
111
+ // and we are sorting each attribute as a String.
112
+ // Instead, attributes should probably be parsed and sorted according to their actual values,
113
+ // not just as strings.
114
+ r. borrow ( )
115
+ . formatted_attributes ( )
116
+ . iter ( )
117
+ . flatten ( )
118
+ . next ( )
119
+ . cloned ( )
120
+ . map ( |s| s. content )
121
+ } )
92
122
} ) ,
93
- Self :: Total => resources
94
- . sort_unstable_by_key ( |resource| resource. upgrade ( ) . map ( |r| r. borrow ( ) . total ( now) ) ) ,
95
123
}
96
124
}
97
125
}
@@ -100,11 +128,15 @@ impl TryFrom<usize> for SortBy {
100
128
type Error = ( ) ;
101
129
fn try_from ( idx : usize ) -> Result < Self , Self :: Error > {
102
130
match idx {
103
- idx if idx == Self :: Rid as usize => Ok ( Self :: Rid ) ,
131
+ idx if idx == Self :: Id as usize => Ok ( Self :: Id ) ,
132
+ idx if idx == Self :: ParentId as usize => Ok ( Self :: ParentId ) ,
104
133
idx if idx == Self :: Kind as usize => Ok ( Self :: Kind ) ,
105
- idx if idx == Self :: ConcreteType as usize => Ok ( Self :: ConcreteType ) ,
106
- idx if idx == Self :: Target as usize => Ok ( Self :: Target ) ,
107
134
idx if idx == Self :: Total as usize => Ok ( Self :: Total ) ,
135
+ idx if idx == Self :: Target as usize => Ok ( Self :: Target ) ,
136
+ idx if idx == Self :: ConcreteType as usize => Ok ( Self :: ConcreteType ) ,
137
+ idx if idx == Self :: Visibility as usize => Ok ( Self :: Visibility ) ,
138
+ idx if idx == Self :: Location as usize => Ok ( Self :: Location ) ,
139
+ idx if idx == Self :: Attributes as usize => Ok ( Self :: Attributes ) ,
108
140
_ => Err ( ( ) ) ,
109
141
}
110
142
}
@@ -202,7 +234,7 @@ impl ResourcesState {
202
234
. unwrap_or_else ( || "n/a" . to_string ( ) ) ,
203
235
) ;
204
236
205
- let location = format_location ( resource. location ) ;
237
+ let location = resource. location . map ( |l| l . into ( ) ) . unwrap_or_default ( ) ;
206
238
let visibility = if resource. is_internal {
207
239
TypeVisibility :: Internal
208
240
} else {
@@ -309,8 +341,8 @@ impl Resource {
309
341
self . stats . total . is_some ( )
310
342
}
311
343
312
- pub ( crate ) fn location ( & self ) -> & str {
313
- & self . location
344
+ pub ( crate ) fn location ( & self ) -> String {
345
+ self . location . to_string ( )
314
346
}
315
347
}
316
348
0 commit comments