Skip to content

Commit

Permalink
Add query
Browse files Browse the repository at this point in the history
  • Loading branch information
tinrab committed Nov 15, 2023
1 parent ccce51f commit 22bfa04
Show file tree
Hide file tree
Showing 26 changed files with 1,244 additions and 49 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni"
version = "0.1.4"
version = "0.1.5"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utility Library for Rust"
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -30,9 +30,9 @@ proto = ["prost", "dep:bomboni_proto"]
request = ["dep:bomboni_request"]

[dependencies]
bomboni_common = { path = "bomboni_common", version = "0.1.4" }
bomboni_derive = { path = "bomboni_derive", version = "0.1.4" }
bomboni_common = { path = "bomboni_common", version = "0.1.5" }
bomboni_derive = { path = "bomboni_derive", version = "0.1.5" }

bomboni_prost = { path = "bomboni_prost", version = "0.1.4", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.4", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.4", optional = true }
bomboni_prost = { path = "bomboni_prost", version = "0.1.5", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.5", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.5", optional = true }
2 changes: 1 addition & 1 deletion bomboni_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_common"
version = "0.1.4"
version = "0.1.5"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Common things for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
2 changes: 1 addition & 1 deletion bomboni_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_derive"
version = "0.1.4"
version = "0.1.5"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Provides derive implementations for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
4 changes: 2 additions & 2 deletions bomboni_prost/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_prost"
version = "0.1.4"
version = "0.1.5"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with prost. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -14,7 +14,7 @@ name = "bomboni_prost"
path = "src/lib.rs"

[dependencies]
itertools = "0.11.0"
itertools = "0.12.0"
convert_case = "0.6.0"
prost = "0.12.1"
prost-types = "0.12.1"
Expand Down
6 changes: 3 additions & 3 deletions bomboni_proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_proto"
version = "0.1.4"
version = "0.1.5"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with Protobuf/gRPC. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -18,7 +18,7 @@ testing = []

[dependencies]
thiserror = "1.0.50"
itertools = "0.11.0"
itertools = "0.12.0"
chrono = { version = "0.4.31", features = ["serde"] }
prost = "0.12.1"
serde = { version = "1.0.192", features = ["derive"] }
Expand All @@ -28,5 +28,5 @@ pot = "3.0.0"
serde_json = "1.0.108"

[build-dependencies]
bomboni_prost = { path = "../bomboni_prost", version = "0.1.4" }
bomboni_prost = { path = "../bomboni_prost", version = "0.1.5" }
prost-build = "0.12.1"
2 changes: 1 addition & 1 deletion bomboni_proto/src/protobuf/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use thiserror::Error;

use crate::google::protobuf::Duration;

#[derive(Error, Debug)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum DurationError {
#[error("duration is out of range")]
OutOfRange,
Expand Down
2 changes: 1 addition & 1 deletion bomboni_proto/src/protobuf/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use thiserror::Error;

use crate::google::protobuf::Timestamp;

#[derive(Error, Debug)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum TimestampError {
#[error("invalid nanoseconds")]
InvalidNanoseconds,
Expand Down
13 changes: 9 additions & 4 deletions bomboni_request/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_request"
version = "0.1.4"
version = "0.1.5"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with API requests. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -17,10 +17,15 @@ path = "src/lib.rs"
testing = []

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.4" }
bomboni_derive = { path = "../bomboni_derive", version = "0.1.4" }
bomboni_common = { path = "../bomboni_common", version = "0.1.5" }
bomboni_derive = { path = "../bomboni_derive", version = "0.1.5" }
thiserror = "1.0.50"
itertools = "0.11.0"
itertools = "0.12.0"
chrono = "0.4.31"
pest = "2.7.5"
pest_derive = "2.7.5"
base64ct = { version = "1.6.0", features = ["alloc"] }
aes-gcm = { version = "0.10.3", features = ["alloc"] }
blake2 = "0.10.6"
rsa = "0.9.3"
rand = "0.8.5"
2 changes: 1 addition & 1 deletion bomboni_request/src/filter/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::schema::ValueType;

use super::parser::Rule;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum FilterError {
#[error("failed to parse filter from `{start}` to `{end}`")]
Parse { start: usize, end: usize },
Expand Down
40 changes: 40 additions & 0 deletions bomboni_request/src/filter/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
//! # Filter
//!
//! Utility for specifying filters on queries, as described in Google AIP standard [1].
//!
//! [1]: https://google.aip.dev/160
use std::fmt;
use std::fmt::{Display, Formatter, Write};
use std::ops::Deref;
Expand Down Expand Up @@ -381,6 +387,17 @@ impl Filter {
Filter::Value(value) => value.value_type(),
}
}

pub fn is_valid(&self, schema: &Schema) -> bool {
// TODO: verify if this is fine
self.get_result_value_type(schema).is_some()
}
}

impl Default for Filter {
fn default() -> Self {
Filter::Conjunction(Vec::new())
}
}

impl Display for Filter {
Expand Down Expand Up @@ -443,6 +460,29 @@ mod tests {

use super::*;

#[test]
fn validate_schema() {
let schema = UserItem::get_schema();
macro_rules! check {
(@valid $filter:expr) => {
assert!(check!($filter));
};
(@invalid $filter:expr) => {
assert!(!check!($filter));
};
($filter:expr) => {
Filter::parse($filter).unwrap().is_valid(&schema)
};
}

check!(@valid "42");
check!(@valid "false");

check!(@invalid "a");
check!(@invalid "a.b");
check!(@invalid "f()");
}

#[test]
fn it_works() {
Filter::parse(r#" "#).unwrap();
Expand Down
2 changes: 1 addition & 1 deletion bomboni_request/src/ordering/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use thiserror::Error;

#[derive(Error, Debug, Clone, PartialEq, Eq)]
#[derive(Error, Debug, Clone, PartialEq)]
pub enum OrderingError {
#[error("duplicate ordering field `{0}`")]
DuplicateField(String),
Expand Down
17 changes: 16 additions & 1 deletion bomboni_request/src/ordering/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ use std::{

use itertools::Itertools;

use crate::schema::Schema;

use self::error::{OrderingError, OrderingResult};

use super::schema::SchemaMapped;

pub mod error;

#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Ordering {
pub terms: Vec<OrderingTerm>,
}
Expand Down Expand Up @@ -91,6 +93,19 @@ impl Ordering {
}
Some(cmp::Ordering::Equal)
}

pub fn is_valid(&self, schema: &Schema) -> bool {
for term in self.terms.iter() {
if let Some(field) = schema.get_field(&term.name) {
if !field.ordered {
return false;
}
} else {
return false;
}
}
true
}
}

impl Display for Ordering {
Expand Down
1 change: 0 additions & 1 deletion bomboni_request/src/query.rs

This file was deleted.

41 changes: 41 additions & 0 deletions bomboni_request/src/query/error.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use thiserror::Error;

use crate::{filter::error::FilterError, ordering::error::OrderingError};

#[derive(Error, Debug, Clone, PartialEq)]
pub enum QueryError {
#[error("filter error: {0}")]
FilterError(FilterError),
#[error("filter is too long")]
FilterTooLong,
#[error("filter schema mismatch")]
FilterSchemaMismatch,
#[error("ordering error: {0}")]
OrderingError(OrderingError),
#[error("ordering is too long")]
OrderingTooLong,
#[error("ordering schema mismatch")]
OrderingSchemaMismatch,
#[error("query is too long")]
QueryTooLong,
#[error("page token is invalid")]
InvalidPageToken,
#[error("page token could not be built")]
PageTokenFailure,
#[error("page size specified is invalid")]
InvalidPageSize,
}

pub type QueryResult<T> = Result<T, QueryError>;

impl From<FilterError> for QueryError {
fn from(err: FilterError) -> Self {
QueryError::FilterError(err)
}
}

impl From<OrderingError> for QueryError {
fn from(err: OrderingError) -> Self {
QueryError::OrderingError(err)
}
}
Loading

0 comments on commit 22bfa04

Please sign in to comment.