@@ -40,24 +40,9 @@ impl PluginGroupBuilder {
40
40
}
41
41
}
42
42
43
- // Removes a previous ordering of a plugin that has just been added at `added_at` index
44
- fn remove_when_adding < T : Plugin > ( & mut self , added_at : usize ) {
45
- if let Some ( to_remove) = self
46
- . order
47
- . iter ( )
48
- . enumerate ( )
49
- . find ( |( i, ty) | * i != added_at && * * ty == TypeId :: of :: < T > ( ) )
50
- . map ( |( i, _) | i)
51
- {
52
- self . order . remove ( to_remove) ;
53
- }
54
- }
55
-
56
- /// Adds the plugin [`Plugin`] at the end of this [`PluginGroupBuilder`]. If the plugin was
57
- /// already in the group, it is removed from its previous place.
58
- pub fn add < T : Plugin > ( & mut self , plugin : T ) -> & mut Self {
59
- let target_index = self . order . len ( ) ;
60
- self . order . push ( TypeId :: of :: < T > ( ) ) ;
43
+ // Insert the new plugin as enabled, and removes its previous ordering if it was
44
+ // already present
45
+ fn upsert_plugin_state < T : Plugin > ( & mut self , plugin : T , added_at_index : usize ) {
61
46
if let Some ( entry) = self . plugins . insert (
62
47
TypeId :: of :: < T > ( ) ,
63
48
PluginEntry {
@@ -71,9 +56,24 @@ impl PluginGroupBuilder {
71
56
entry. plugin. name( )
72
57
) ;
73
58
}
74
- self . remove_when_adding :: < T > ( target_index) ;
59
+ if let Some ( to_remove) = self
60
+ . order
61
+ . iter ( )
62
+ . enumerate ( )
63
+ . find ( |( i, ty) | * i != added_at_index && * * ty == TypeId :: of :: < T > ( ) )
64
+ . map ( |( i, _) | i)
65
+ {
66
+ self . order . remove ( to_remove) ;
67
+ }
75
68
}
69
+ }
76
70
71
+ /// Adds the plugin [`Plugin`] at the end of this [`PluginGroupBuilder`]. If the plugin was
72
+ /// already in the group, it is removed from its previous place.
73
+ pub fn add < T : Plugin > ( & mut self , plugin : T ) -> & mut Self {
74
+ let target_index = self . order . len ( ) ;
75
+ self . order . push ( TypeId :: of :: < T > ( ) ) ;
76
+ self . upsert_plugin_state ( plugin, target_index) ;
77
77
self
78
78
}
79
79
@@ -83,21 +83,7 @@ impl PluginGroupBuilder {
83
83
pub fn add_before < Target : Plugin , T : Plugin > ( & mut self , plugin : T ) -> & mut Self {
84
84
let target_index = self . index_of :: < Target > ( ) ;
85
85
self . order . insert ( target_index, TypeId :: of :: < T > ( ) ) ;
86
- if let Some ( entry) = self . plugins . insert (
87
- TypeId :: of :: < T > ( ) ,
88
- PluginEntry {
89
- plugin : Box :: new ( plugin) ,
90
- enabled : true ,
91
- } ,
92
- ) {
93
- if entry. enabled {
94
- warn ! (
95
- "You are replacing plugin '{}' that was not disabled." ,
96
- entry. plugin. name( )
97
- ) ;
98
- }
99
- self . remove_when_adding :: < T > ( target_index) ;
100
- }
86
+ self . upsert_plugin_state ( plugin, target_index) ;
101
87
self
102
88
}
103
89
@@ -107,21 +93,7 @@ impl PluginGroupBuilder {
107
93
pub fn add_after < Target : Plugin , T : Plugin > ( & mut self , plugin : T ) -> & mut Self {
108
94
let target_index = self . index_of :: < Target > ( ) + 1 ;
109
95
self . order . insert ( target_index, TypeId :: of :: < T > ( ) ) ;
110
- if let Some ( entry) = self . plugins . insert (
111
- TypeId :: of :: < T > ( ) ,
112
- PluginEntry {
113
- plugin : Box :: new ( plugin) ,
114
- enabled : true ,
115
- } ,
116
- ) {
117
- if entry. enabled {
118
- warn ! (
119
- "You are replacing plugin '{}' that was not disabled." ,
120
- entry. plugin. name( )
121
- ) ;
122
- }
123
- self . remove_when_adding :: < T > ( target_index) ;
124
- }
96
+ self . upsert_plugin_state ( plugin, target_index) ;
125
97
self
126
98
}
127
99
0 commit comments