Skip to content

Commit 555617a

Browse files
authored
feat: Add map function to TextRangePropertyValue<Option<T>> (#632)
1 parent 06fc934 commit 555617a

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

consumer/src/text.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -586,6 +586,18 @@ pub enum RangePropertyValue<T: alloc::fmt::Debug + PartialEq> {
586586
Mixed,
587587
}
588588

589+
impl<T: alloc::fmt::Debug + PartialEq> RangePropertyValue<Option<T>> {
590+
pub fn map<U: alloc::fmt::Debug + PartialEq>(
591+
self,
592+
f: impl FnOnce(T) -> U,
593+
) -> RangePropertyValue<Option<U>> {
594+
match self {
595+
Self::Single(value) => RangePropertyValue::Single(value.map(f)),
596+
Self::Mixed => RangePropertyValue::Mixed,
597+
}
598+
}
599+
}
600+
589601
#[derive(Clone, Copy)]
590602
pub struct Range<'a> {
591603
pub(crate) node: Node<'a>,
@@ -2568,4 +2580,21 @@ mod tests {
25682580
let node = state.node_by_id(NodeId(1)).unwrap();
25692581
let _ = node.text_selection().unwrap();
25702582
}
2583+
2584+
#[test]
2585+
fn range_property_value_map() {
2586+
use super::RangePropertyValue;
2587+
assert_eq!(
2588+
RangePropertyValue::Single(Some(0)).map(|x| x + 1),
2589+
RangePropertyValue::Single(Some(1))
2590+
);
2591+
assert_eq!(
2592+
RangePropertyValue::<Option<usize>>::Single(None).map(|x| x + 1),
2593+
RangePropertyValue::Single(None)
2594+
);
2595+
assert_eq!(
2596+
RangePropertyValue::<Option<usize>>::Mixed.map(|x| x + 1),
2597+
RangePropertyValue::Mixed
2598+
);
2599+
}
25712600
}

0 commit comments

Comments
 (0)