Skip to content

Commit 025d1ed

Browse files
committed
feat: Add JsonTypeSet::len & JsonTypeSet::remove
Signed-off-by: Dmitry Dygalo <dmitry.dygalo@workato.com>
1 parent 345bb71 commit 025d1ed

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- `JsonTypeSet::len()` and `JsonTypeSet::remove()` helpers for managing type sets.
8+
59
## [0.37.1] - 2025-11-19
610

711
### Fixed

crates/jsonschema/src/types.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,25 @@ impl JsonTypeSet {
117117
self.0 |= ty as u8;
118118
self
119119
}
120+
/// Remove a type from this set and return the modified set.
121+
#[inline]
122+
#[must_use]
123+
pub const fn remove(mut self, ty: JsonType) -> Self {
124+
self.0 &= !(ty as u8);
125+
self
126+
}
127+
/// Return the number of types in this set.
128+
#[inline]
129+
#[must_use]
130+
pub const fn len(self) -> usize {
131+
self.0.count_ones() as usize
132+
}
133+
/// Return `true` if the set contains no types.
134+
#[inline]
135+
#[must_use]
136+
pub const fn is_empty(self) -> bool {
137+
self.0 == 0
138+
}
120139
/// Check if this set includes the specified type.
121140
#[inline]
122141
#[must_use]
@@ -328,6 +347,29 @@ mod tests {
328347
set.contains_value_type(value)
329348
}
330349

350+
#[test]
351+
fn test_remove_types() {
352+
let set = JsonTypeSet::all().remove(JsonType::Number);
353+
assert!(!set.contains(JsonType::Number));
354+
assert!(set.contains(JsonType::Integer));
355+
assert_eq!(set.len(), 6);
356+
357+
let empty = JsonTypeSet::empty();
358+
assert_eq!(empty.remove(JsonType::Boolean), empty);
359+
}
360+
361+
#[test]
362+
fn test_len() {
363+
let empty = JsonTypeSet::empty();
364+
assert!(empty.is_empty());
365+
assert_eq!(empty.len(), 0);
366+
367+
let with_string = empty.insert(JsonType::String);
368+
assert!(!with_string.is_empty());
369+
assert_eq!(with_string.len(), 1);
370+
assert_eq!(JsonTypeSet::all().len(), 7);
371+
}
372+
331373
#[test]
332374
fn test_debug_format() {
333375
assert_eq!(format!("{:?}", JsonTypeSet::default()), "()");

0 commit comments

Comments
 (0)