@@ -17,11 +17,16 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
17
17
/// Constructs a modifier set from a string, where modifiers are separated by
18
18
/// the character `.`.
19
19
///
20
- /// It is not unsafe to use this function incorrectly, but it can produce
21
- /// unexpected results down the line. Correct usage should ensure that `s`
22
- /// does not contain any empty modifiers (i.e. the sequence `..`) and that
23
- /// no modifier occurs twice.
24
- pub fn new_unchecked ( s : S ) -> Self {
20
+ /// `s` should not contain any empty modifiers (i.e. it shouldn't contain the
21
+ /// sequence `..`) and no modifier should occur twice. Otherwise, unexpected
22
+ /// errors can occur.
23
+ pub fn from_raw_dotted ( s : S ) -> Self {
24
+ // checking the other requirement too feels like it would be a bit too
25
+ // expensive, even for debug mode.
26
+ debug_assert ! (
27
+ !s. contains( ".." ) ,
28
+ "ModifierSet::from_dotted called with string containing empty modifier"
29
+ ) ;
25
30
Self ( s)
26
31
}
27
32
@@ -40,12 +45,11 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
40
45
ModifierSet ( & self . 0 )
41
46
}
42
47
43
- /// Inserts a modifier into the set, without checking that it is a valid modifier .
48
+ /// Inserts a new modifier into the set.
44
49
///
45
- /// It is not unsafe to use this method incorrectly, but that can produce
46
- /// unexpected results down the line. Correct usage should ensure that
47
- /// `modifier` is not empty and doesn't contain the character `.`.
48
- pub fn insert_unchecked ( & mut self , m : & str )
50
+ /// `m` should not be empty, contain the character `.`,
51
+ /// or already be in the set. Otherwise, unexpected errors can occur.
52
+ pub fn insert_raw ( & mut self , m : & str )
49
53
where
50
54
S : for < ' a > std:: ops:: AddAssign < & ' a str > ,
51
55
{
@@ -161,53 +165,51 @@ mod tests {
161
165
#[ test]
162
166
fn iter_count ( ) {
163
167
assert_eq ! ( ModifierSet :: default ( ) . iter( ) . count( ) , 0 ) ;
164
- assert_eq ! ( ModifierSet :: new_unchecked ( "a" ) . iter( ) . count( ) , 1 ) ;
165
- assert_eq ! ( ModifierSet :: new_unchecked ( "a.b" ) . iter( ) . count( ) , 2 ) ;
166
- assert_eq ! ( ModifierSet :: new_unchecked ( "a.b.c" ) . iter( ) . count( ) , 3 ) ;
168
+ assert_eq ! ( ModifierSet :: from_raw_dotted ( "a" ) . iter( ) . count( ) , 1 ) ;
169
+ assert_eq ! ( ModifierSet :: from_raw_dotted ( "a.b" ) . iter( ) . count( ) , 2 ) ;
170
+ assert_eq ! ( ModifierSet :: from_raw_dotted ( "a.b.c" ) . iter( ) . count( ) , 3 ) ;
167
171
}
168
172
169
173
#[ test]
170
174
fn subset ( ) {
171
- assert ! (
172
- ModifierSet :: new_unchecked( "a" ) . is_subset( ModifierSet :: new_unchecked( "a.b" ) )
173
- ) ;
174
- assert ! (
175
- ModifierSet :: new_unchecked( "a" ) . is_subset( ModifierSet :: new_unchecked( "b.a" ) )
176
- ) ;
177
- assert ! ( ModifierSet :: new_unchecked( "a.b" )
178
- . is_subset( ModifierSet :: new_unchecked( "b.c.a" ) ) ) ;
175
+ assert ! ( ModifierSet :: from_raw_dotted( "a" )
176
+ . is_subset( ModifierSet :: from_raw_dotted( "a.b" ) ) ) ;
177
+ assert ! ( ModifierSet :: from_raw_dotted( "a" )
178
+ . is_subset( ModifierSet :: from_raw_dotted( "b.a" ) ) ) ;
179
+ assert ! ( ModifierSet :: from_raw_dotted( "a.b" )
180
+ . is_subset( ModifierSet :: from_raw_dotted( "b.c.a" ) ) ) ;
179
181
}
180
182
181
183
#[ test]
182
184
fn best_match ( ) {
183
185
// 1. more modifiers in common with self
184
186
assert_eq ! (
185
- ModifierSet :: new_unchecked ( "a.b" ) . best_match_in(
187
+ ModifierSet :: from_raw_dotted ( "a.b" ) . best_match_in(
186
188
[
187
- ( ModifierSet :: new_unchecked ( "a.c" ) , 1 ) ,
188
- ( ModifierSet :: new_unchecked ( "a.b" ) , 2 ) ,
189
+ ( ModifierSet :: from_raw_dotted ( "a.c" ) , 1 ) ,
190
+ ( ModifierSet :: from_raw_dotted ( "a.b" ) , 2 ) ,
189
191
]
190
192
. into_iter( )
191
193
) ,
192
194
Some ( 2 )
193
195
) ;
194
196
// 2. fewer modifiers in general
195
197
assert_eq ! (
196
- ModifierSet :: new_unchecked ( "a" ) . best_match_in(
198
+ ModifierSet :: from_raw_dotted ( "a" ) . best_match_in(
197
199
[
198
- ( ModifierSet :: new_unchecked ( "a" ) , 1 ) ,
199
- ( ModifierSet :: new_unchecked ( "a.b" ) , 2 ) ,
200
+ ( ModifierSet :: from_raw_dotted ( "a" ) , 1 ) ,
201
+ ( ModifierSet :: from_raw_dotted ( "a.b" ) , 2 ) ,
200
202
]
201
203
. into_iter( )
202
204
) ,
203
205
Some ( 1 )
204
206
) ;
205
207
// the first rule takes priority over the second
206
208
assert_eq ! (
207
- ModifierSet :: new_unchecked ( "a.b" ) . best_match_in(
209
+ ModifierSet :: from_raw_dotted ( "a.b" ) . best_match_in(
208
210
[
209
- ( ModifierSet :: new_unchecked ( "a" ) , 1 ) ,
210
- ( ModifierSet :: new_unchecked ( "a.b" ) , 2 ) ,
211
+ ( ModifierSet :: from_raw_dotted ( "a" ) , 1 ) ,
212
+ ( ModifierSet :: from_raw_dotted ( "a.b" ) , 2 ) ,
211
213
]
212
214
. into_iter( )
213
215
) ,
@@ -217,8 +219,8 @@ mod tests {
217
219
assert_eq ! (
218
220
ModifierSet :: default ( ) . best_match_in(
219
221
[
220
- ( ModifierSet :: new_unchecked ( "a" ) , 1 ) ,
221
- ( ModifierSet :: new_unchecked ( "b" ) , 2 )
222
+ ( ModifierSet :: from_raw_dotted ( "a" ) , 1 ) ,
223
+ ( ModifierSet :: from_raw_dotted ( "b" ) , 2 )
222
224
]
223
225
. into_iter( )
224
226
) ,
0 commit comments