Skip to content

Commit 5de0f07

Browse files
committed
Rename _unchecked to _raw/_raw_dotted and reword docs accordingly
1 parent 6ef866a commit 5de0f07

File tree

1 file changed

+34
-32
lines changed

1 file changed

+34
-32
lines changed

src/shared.rs

Lines changed: 34 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
1717
/// Constructs a modifier set from a string, where modifiers are separated by
1818
/// the character `.`.
1919
///
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+
);
2530
Self(s)
2631
}
2732

@@ -40,12 +45,11 @@ impl<S: Deref<Target = str>> ModifierSet<S> {
4045
ModifierSet(&self.0)
4146
}
4247

43-
/// Inserts a modifier into the set, without checking that it is a valid modifier.
48+
/// Inserts a new modifier into the set.
4449
///
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)
4953
where
5054
S: for<'a> std::ops::AddAssign<&'a str>,
5155
{
@@ -161,53 +165,51 @@ mod tests {
161165
#[test]
162166
fn iter_count() {
163167
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);
167171
}
168172

169173
#[test]
170174
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")));
179181
}
180182

181183
#[test]
182184
fn best_match() {
183185
// 1. more modifiers in common with self
184186
assert_eq!(
185-
ModifierSet::new_unchecked("a.b").best_match_in(
187+
ModifierSet::from_raw_dotted("a.b").best_match_in(
186188
[
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),
189191
]
190192
.into_iter()
191193
),
192194
Some(2)
193195
);
194196
// 2. fewer modifiers in general
195197
assert_eq!(
196-
ModifierSet::new_unchecked("a").best_match_in(
198+
ModifierSet::from_raw_dotted("a").best_match_in(
197199
[
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),
200202
]
201203
.into_iter()
202204
),
203205
Some(1)
204206
);
205207
// the first rule takes priority over the second
206208
assert_eq!(
207-
ModifierSet::new_unchecked("a.b").best_match_in(
209+
ModifierSet::from_raw_dotted("a.b").best_match_in(
208210
[
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),
211213
]
212214
.into_iter()
213215
),
@@ -217,8 +219,8 @@ mod tests {
217219
assert_eq!(
218220
ModifierSet::default().best_match_in(
219221
[
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)
222224
]
223225
.into_iter()
224226
),

0 commit comments

Comments
 (0)