Search criteria implementation, fixes #3

This commit is contained in:
Michael Zhang 2021-10-28 22:37:26 -05:00
parent 50bd0a83d4
commit 1ab69aadb9
5 changed files with 43 additions and 11 deletions

7
Cargo.lock generated
View file

@ -862,6 +862,12 @@ dependencies = [
"cfg-if",
]
[[package]]
name = "maplit"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d"
[[package]]
name = "matches"
version = "0.1.9"
@ -1047,6 +1053,7 @@ dependencies = [
"format-bytes",
"futures",
"log",
"maplit",
"nom",
"panorama-proto-common",
"stderrlog",

View file

@ -1,8 +1,6 @@
panorama
========
[![](https://tokei.rs/b1/github/iptq/panorama?category=code)](https://github.com/XAMPPRocky/tokei)
Panorama is a terminal Personal Information Manager (PIM).
Status: **still writing the basics**

View file

@ -42,3 +42,6 @@ panorama-proto-common = { path = "../proto-common" }
# for fuzzing
arbitrary = { version = "1.0.2", optional = true, features = ["derive"] }
[dev-dependencies]
maplit = "1.0.2"

View file

@ -15,6 +15,9 @@ extern crate log;
#[macro_use]
extern crate panorama_proto_common;
#[cfg(test)]
#[macro_use]
extern crate maplit;
pub mod client;
// pub mod events;
pub mod proto;

View file

@ -1,4 +1,8 @@
use std::{collections::HashSet, io::{self, Write}, ops::{Bound, RangeBounds}};
use std::{
collections::HashSet,
io::{self, Write},
ops::{Bound, RangeBounds},
};
use format_bytes::DisplayBytes;
use panorama_proto_common::{quote_string, Bytes};
@ -175,15 +179,15 @@ impl DisplayBytes for FetchItems {
#[derive(Clone, Debug)]
pub enum FetchAttr {}
// TODO: not the most efficient representation but I'll optimize if this ever actually becomes a
// problem
// TODO: not the most efficient representation but I'll optimize if this ever
// actually becomes a problem
#[derive(Clone, Debug)]
pub struct SearchCriteria(HashSet<(Bound<u32>, Bound<u32>)>);
pub struct SearchCriteria(Vec<(Bound<u32>, Bound<u32>)>);
impl SearchCriteria {
pub fn all() -> Self {
let mut set = HashSet::new();
set.insert((Bound::Unbounded, Bound::Unbounded));
let mut set = Vec::new();
set.push((Bound::Unbounded, Bound::Unbounded));
SearchCriteria(set)
}
@ -196,6 +200,12 @@ impl SearchCriteria {
false
}
pub fn with_range(&mut self, range: impl RangeBounds<u32>) -> &mut Self {
let range = (range.start_bound().cloned(), range.end_bound().cloned());
self.0.push(range);
self
}
}
impl DisplayBytes for SearchCriteria {
@ -209,7 +219,7 @@ impl DisplayBytes for SearchCriteria {
match range.0 {
Bound::Excluded(n) => write_bytes!(w, b"{}", &(n + 1))?,
Bound::Included(n) => write_bytes!(w, b"{}", &n)?,
Bound::Unbounded => write_bytes!(w, b"*")?,
Bound::Unbounded => write_bytes!(w, b"1")?,
}
write_bytes!(w, b":")?;
@ -232,6 +242,17 @@ mod tests {
#[test]
fn display_search_criteria() {
// TODO: is there a trivial case?
assert_eq!(format_bytes!(b"{}", SearchCriteria::all()), b"*:*");
assert_eq!(format_bytes!(b"{}", SearchCriteria::all()), b"1:*");
assert_eq!(
format_bytes!(
b"{}",
SearchCriteria(vec![
(Bound::Unbounded, Bound::Included(4)),
(Bound::Excluded(5), Bound::Unbounded),
])
),
b"1:4,6:*"
);
}
}