use super::*;
use crate::TestError;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryParams {
selection: String,
where_: QueryParamsWhere,
where_many: QueryParamsWhereMany,
}
impl QueryParams {
pub fn new<S>(selection: S, where_: QueryParamsWhere, where_many: QueryParamsWhereMany) -> Self
where
S: Into<String>,
{
QueryParams {
selection: selection.into(),
where_,
where_many,
}
}
pub fn parse(&self, json: serde_json::Value, path: &[&str]) -> Result<String, TestError> {
let val = self.where_.parse(json, path)?;
Ok(val)
}
pub fn parse_extend(&self, json: serde_json::Value, path: &[&str], meta: &str) -> Result<String, TestError> {
let val = self.where_.parse_extend(json, path, meta)?;
Ok(val)
}
pub fn parse_many(&self, json: serde_json::Value, path: &[&str]) -> Result<Vec<String>, TestError> {
let val = self.where_many.parse(&json, path)?;
Ok(val)
}
pub fn parse_many_first(&self, json: serde_json::Value, path: &[&str]) -> Result<String, TestError> {
let val = self.where_many.parse(&json, path)?;
Ok(val.first().unwrap().to_owned())
}
pub fn parse_many_all(&self, json: serde_json::Value, path: &[&str]) -> Result<String, TestError> {
let val = self.where_many.parse(&json, path)?;
Ok(format!("{}{}{}", "[", val.join(", "), "]"))
}
pub fn selection(&self) -> &str {
self.selection.as_str()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryParamsWhere {
Identifier(String),
CompoundIdentifier(Vec<String>, String),
}
impl QueryParamsWhere {
pub fn identifier(field: impl Into<String>) -> Self {
Self::Identifier(field.into())
}
pub fn compound_identifier<V, F>(fields: V, arg_name: impl Into<String>) -> Self
where
F: Into<String>,
V: Into<Vec<F>>,
{
QueryParamsWhere::CompoundIdentifier(fields.into().into_iter().map(|f| f.into()).collect(), arg_name.into())
}
pub fn parse(&self, json: serde_json::Value, path: &[&str]) -> Result<String, TestError> {
match self {
QueryParamsWhere::Identifier(field) => parse_id(field, &json, path, ""),
QueryParamsWhere::CompoundIdentifier(fields, arg_name) => {
parse_compound_id(fields, arg_name, &json, path, "")
}
}
}
pub fn parse_extend(&self, json: serde_json::Value, path: &[&str], meta: &str) -> Result<String, TestError> {
match self {
QueryParamsWhere::Identifier(field) => parse_id(field, &json, path, meta),
QueryParamsWhere::CompoundIdentifier(fields, arg_name) => {
parse_compound_id(fields, arg_name, &json, path, meta)
}
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum QueryParamsWhereMany {
ManyIds(String),
ManyCompounds(Vec<String>, String),
}
impl QueryParamsWhereMany {
pub fn many_ids(field: impl Into<String>) -> Self {
Self::ManyIds(field.into())
}
pub fn many_compounds<V, F>(fields: V, arg_name: impl Into<String>) -> Self
where
F: Into<String>,
V: Into<Vec<F>>,
{
QueryParamsWhereMany::ManyCompounds(fields.into().into_iter().map(|f| f.into()).collect(), arg_name.into())
}
pub fn parse(&self, json: &serde_json::Value, path: &[&str]) -> Result<Vec<String>, TestError> {
match self {
QueryParamsWhereMany::ManyIds(field) => parse_many_ids(field, json, path),
QueryParamsWhereMany::ManyCompounds(fields, arg_name) => {
parse_many_compound_ids(fields, arg_name, json, path)
}
}
}
}