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