-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathselection.rs
More file actions
61 lines (54 loc) · 1.55 KB
/
selection.rs
File metadata and controls
61 lines (54 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use crate::{Ctx, ExtractIndex};
#[derive(Clone, Debug, PartialEq, serde::Serialize, serde::Deserialize, Hash, dyn_any::DynAny, Default)]
pub enum IndexOperationFilter {
Range(Vec<core::ops::RangeInclusive<usize>>),
#[default]
All,
}
impl IndexOperationFilter {
pub fn contains(&self, index: usize) -> bool {
match self {
Self::Range(range) => range.iter().any(|range| range.contains(&index)),
Self::All => true,
}
}
}
impl From<Vec<core::ops::RangeInclusive<usize>>> for IndexOperationFilter {
fn from(values: Vec<core::ops::RangeInclusive<usize>>) -> Self {
Self::Range(values)
}
}
impl From<core::ops::RangeInclusive<usize>> for IndexOperationFilter {
fn from(value: core::ops::RangeInclusive<usize>) -> Self {
Self::Range(vec![value])
}
}
impl core::fmt::Display for IndexOperationFilter {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::All => {
write!(f, "*")?;
}
Self::Range(range) => {
let mut started = false;
for value in range {
if started {
write!(f, ", ")?;
}
started = true;
if value.start() == value.end() {
write!(f, "{}", value.start())?;
} else {
write!(f, "{}..={}", value.start(), value.end())?;
}
}
}
}
Ok(())
}
}
#[node_macro::node(category("Filtering"), path(graphene_core::vector))]
async fn evaluate_index_operation_filter(ctx: impl Ctx + ExtractIndex, filter: IndexOperationFilter) -> bool {
let index = ctx.try_index().and_then(|indexes| indexes.last().copied()).unwrap_or_default();
filter.contains(index)
}