Skip to main content

link_cli/triplets/
restriction.rs

1use super::Triplet;
2
3/// A restriction of [`Triplets::each_triplet`](super::Triplets::each_triplet): `None` matches any value,
4/// `Some(0)` an empty `from` or `to`.
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
6pub struct TripletQuery {
7    pub index: Option<u32>,
8    pub type_id: Option<u32>,
9    pub from: Option<u32>,
10    pub to: Option<u32>,
11}
12
13impl TripletQuery {
14    /// Every triplet.
15    pub fn any() -> Self {
16        Self::default()
17    }
18
19    pub fn new(
20        index: Option<u32>,
21        type_id: Option<u32>,
22        from: Option<u32>,
23        to: Option<u32>,
24    ) -> Self {
25        Self {
26            index,
27            type_id,
28            from,
29            to,
30        }
31    }
32
33    pub fn by_index(index: u32) -> Self {
34        Self {
35            index: Some(index),
36            ..Self::default()
37        }
38    }
39
40    pub fn by_type(type_id: u32) -> Self {
41        Self {
42            type_id: Some(type_id),
43            ..Self::default()
44        }
45    }
46
47    pub fn by_from(from: u32) -> Self {
48        Self {
49            from: Some(from),
50            ..Self::default()
51        }
52    }
53
54    pub fn by_to(to: u32) -> Self {
55        Self {
56            to: Some(to),
57            ..Self::default()
58        }
59    }
60
61    /// Whether `triplet` satisfies every part of the restriction.
62    pub fn matches(&self, triplet: &Triplet) -> bool {
63        self.index.is_none_or(|index| index == triplet.index)
64            && self
65                .type_id
66                .is_none_or(|type_id| type_id == triplet.type_id)
67            && self.from.is_none_or(|from| from == triplet.from)
68            && self.to.is_none_or(|to| to == triplet.to)
69    }
70}