@@ -11,7 +11,7 @@ struct PluginEntry {
11
11
enabled : bool ,
12
12
}
13
13
14
- /// Builds and customizes a plugin group. A plugin group is an ordered list of plugins that
14
+ /// Builds and customizes a plugin group. A plugin group is an ordered list of plugins
15
15
/// that can be enabled, disabled or reordered.
16
16
#[ derive( Default ) ]
17
17
pub struct PluginGroupBuilder {
@@ -20,24 +20,9 @@ pub struct PluginGroupBuilder {
20
20
}
21
21
22
22
impl PluginGroupBuilder {
23
- // Removes a previous ordering of a plugin that has just been added at `added_at` index
24
- fn remove_when_adding < T : Plugin > ( & mut self , added_at : usize ) {
25
- if let Some ( to_remove) = self
26
- . order
27
- . iter ( )
28
- . enumerate ( )
29
- . find ( |( i, ty) | * i != added_at && * * ty == TypeId :: of :: < T > ( ) )
30
- . map ( |( i, _) | i)
31
- {
32
- self . order . remove ( to_remove) ;
33
- }
34
- }
35
-
36
- /// Adds the plugin `plugin` at the end of this `PluginGroupBuilder`. If the plugin was
37
- /// already in the group, it is removed from its previous place.
38
- pub fn add < T : Plugin > ( & mut self , plugin : T ) -> & mut Self {
39
- let target_index = self . order . len ( ) ;
40
- self . order . push ( TypeId :: of :: < T > ( ) ) ;
23
+ // Insert the new plugin as enabled, and removes its previous ordering if it was
24
+ // already present
25
+ fn upsert_plugin_state < T : Plugin > ( & mut self , plugin : T , added_at_index : usize ) {
41
26
if let Some ( entry) = self . plugins . insert (
42
27
TypeId :: of :: < T > ( ) ,
43
28
PluginEntry {
@@ -51,9 +36,24 @@ impl PluginGroupBuilder {
51
36
entry. plugin. name( )
52
37
) ;
53
38
}
54
- self . remove_when_adding :: < T > ( target_index) ;
39
+ if let Some ( to_remove) = self
40
+ . order
41
+ . iter ( )
42
+ . enumerate ( )
43
+ . find ( |( i, ty) | * i != added_at_index && * * ty == TypeId :: of :: < T > ( ) )
44
+ . map ( |( i, _) | i)
45
+ {
46
+ self . order . remove ( to_remove) ;
47
+ }
55
48
}
49
+ }
56
50
51
+ /// Adds the plugin `plugin` at the end of this `PluginGroupBuilder`. If the plugin was
52
+ /// already in the group, it is removed from its previous place.
53
+ pub fn add < T : Plugin > ( & mut self , plugin : T ) -> & mut Self {
54
+ let target_index = self . order . len ( ) ;
55
+ self . order . push ( TypeId :: of :: < T > ( ) ) ;
56
+ self . upsert_plugin_state ( plugin, target_index) ;
57
57
self
58
58
}
59
59
@@ -74,21 +74,7 @@ impl PluginGroupBuilder {
74
74
)
75
75
} ) ;
76
76
self . order . insert ( target_index, TypeId :: of :: < T > ( ) ) ;
77
- if let Some ( entry) = self . plugins . insert (
78
- TypeId :: of :: < T > ( ) ,
79
- PluginEntry {
80
- plugin : Box :: new ( plugin) ,
81
- enabled : true ,
82
- } ,
83
- ) {
84
- if entry. enabled {
85
- warn ! (
86
- "You are replacing plugin '{}' that was not disabled." ,
87
- entry. plugin. name( )
88
- ) ;
89
- }
90
- self . remove_when_adding :: < T > ( target_index) ;
91
- }
77
+ self . upsert_plugin_state ( plugin, target_index) ;
92
78
self
93
79
}
94
80
@@ -110,21 +96,7 @@ impl PluginGroupBuilder {
110
96
} )
111
97
+ 1 ;
112
98
self . order . insert ( target_index, TypeId :: of :: < T > ( ) ) ;
113
- if let Some ( entry) = self . plugins . insert (
114
- TypeId :: of :: < T > ( ) ,
115
- PluginEntry {
116
- plugin : Box :: new ( plugin) ,
117
- enabled : true ,
118
- } ,
119
- ) {
120
- if entry. enabled {
121
- warn ! (
122
- "You are replacing plugin '{}' that was not disabled." ,
123
- entry. plugin. name( )
124
- ) ;
125
- }
126
- self . remove_when_adding :: < T > ( target_index) ;
127
- }
99
+ self . upsert_plugin_state ( plugin, target_index) ;
128
100
self
129
101
}
130
102
0 commit comments