From 7d0b23e95512330879f433d019c6c38cffd3739d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Ml=C3=A1dek?= Date: Tue, 21 Dec 2021 12:01:40 +0100 Subject: [PATCH] impl fromstr for query --- src/database/lang.rs | 9 +++++++++ src/routes.rs | 3 +-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/database/lang.rs b/src/database/lang.rs index 709ad28..637ed44 100644 --- a/src/database/lang.rs +++ b/src/database/lang.rs @@ -234,6 +234,15 @@ impl TryFrom<&lexpr::Value> for Query { } } +impl FromStr for Query { + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + let sexp = lexpr::from_str(s)?; + Query::try_from(&sexp) + } +} + impl Query { pub(crate) fn to_sqlite_predicates(&self) -> Result> { match self { diff --git a/src/routes.rs b/src/routes.rs index 0d22a4f..98c8f78 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -89,8 +89,7 @@ pub async fn get_query( ) -> Result { let connection = state.db_pool.get().map_err(ErrorInternalServerError)?; - let sexp = lexpr::from_str(info.query.as_str()).map_err(ErrorBadRequest)?; - let in_query = Query::try_from(&sexp).map_err(ErrorBadRequest)?; + let in_query: Query = info.query.as_str().parse().map_err(ErrorBadRequest)?; let entries = query(&connection, in_query).map_err(ErrorInternalServerError)?; let mut result: HashMap = HashMap::new(); for entry in entries {