mirror of
https://github.com/tribufu/rust-gamedig
synced 2026-05-06 15:27:28 +00:00
[Service] Add merge methods on SearchFilters
This commit is contained in:
parent
fc52f3fe91
commit
a17f5ad4d2
2 changed files with 76 additions and 0 deletions
|
|
@ -6,6 +6,9 @@ Protocols:
|
|||
- Valve:
|
||||
1. Added standard and serde derives to `GatheringSettings`.
|
||||
|
||||
Services:
|
||||
1. Added methods to merge two search filters together.
|
||||
|
||||
### Breaking:
|
||||
Nothing, yet...
|
||||
|
||||
|
|
|
|||
|
|
@ -154,6 +154,28 @@ impl<'a> SearchFilters<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn merge_all(self, others: SearchFilters<'a>) -> Self {
|
||||
let mut updated = self.merge_normals(&others);
|
||||
updated = updated.merge_nors(&others);
|
||||
updated = updated.merge_nands(&others);
|
||||
|
||||
updated
|
||||
}
|
||||
|
||||
pub fn merge_normals(self, others: &SearchFilters<'a>) -> Self {
|
||||
let mut updated_fitler = self.filters;
|
||||
|
||||
for (filter_key, filter_value) in &others.filters {
|
||||
updated_fitler.insert(*filter_key, *filter_value);
|
||||
}
|
||||
|
||||
Self {
|
||||
filters: updated_fitler,
|
||||
nand_filters: self.nand_filters,
|
||||
nor_filters: self.nor_filters,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert(self, filter: Filter<'a>) -> Self {
|
||||
let mut updated_fitler = self.filters;
|
||||
updated_fitler.insert(std::mem::discriminant(&filter), filter);
|
||||
|
|
@ -165,6 +187,20 @@ impl<'a> SearchFilters<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn merge_nands(self, others: &SearchFilters<'a>) -> Self {
|
||||
let mut updated_fitler = self.nand_filters;
|
||||
|
||||
for (filter_key, filter_value) in &others.nand_filters {
|
||||
updated_fitler.insert(*filter_key, *filter_value);
|
||||
}
|
||||
|
||||
Self {
|
||||
filters: self.filters,
|
||||
nand_filters: updated_fitler,
|
||||
nor_filters: self.nor_filters,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_nand(self, filter: Filter<'a>) -> Self {
|
||||
let mut updated_fitler = self.nor_filters;
|
||||
updated_fitler.insert(std::mem::discriminant(&filter), filter);
|
||||
|
|
@ -176,6 +212,20 @@ impl<'a> SearchFilters<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn merge_nors(self, others: &SearchFilters<'a>) -> Self {
|
||||
let mut updated_fitler = self.nor_filters;
|
||||
|
||||
for (filter_key, filter_value) in &others.nor_filters {
|
||||
updated_fitler.insert(*filter_key, *filter_value);
|
||||
}
|
||||
|
||||
Self {
|
||||
filters: self.filters,
|
||||
nand_filters: self.nand_filters,
|
||||
nor_filters: updated_fitler,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert_nor(self, filter: Filter<'a>) -> Self {
|
||||
let mut updated_fitler = self.nand_filters;
|
||||
updated_fitler.insert(std::mem::discriminant(&filter), filter);
|
||||
|
|
@ -230,3 +280,26 @@ pub enum Region {
|
|||
Africa = 0x07,
|
||||
Others = 0xFF,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use crate::valve_master_server::{Filter, SearchFilters};
|
||||
|
||||
#[test]
|
||||
fn merge_normals_test() {
|
||||
let filters_a = SearchFilters::new()
|
||||
.insert(Filter::IsSecured(true));
|
||||
|
||||
let filters_b = SearchFilters::new()
|
||||
.insert(Filter::CanBeFull(true))
|
||||
.insert(Filter::IsSecured(false));
|
||||
|
||||
let combined = filters_a.merge_all(filters_b);
|
||||
|
||||
let composed = SearchFilters::new()
|
||||
.insert(Filter::IsSecured(false))
|
||||
.insert(Filter::CanBeFull(true));
|
||||
|
||||
assert_eq!(combined, composed)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue