From 70056c8b1febd5881c3de6d19ec11e4beda6abe1 Mon Sep 17 00:00:00 2001
From: Igor Kuznetsov <kuznetsovin@gmail.com>
Date: Fri, 14 Jan 2022 12:06:08 +0300
Subject: [PATCH] feat: add api method for transforming text to Bool type

---
 src/ir/expression/tests.rs |  1 -
 src/ir/operator.rs         | 32 ++++++++++++++++++++++++++++----
 2 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/src/ir/expression/tests.rs b/src/ir/expression/tests.rs
index 59b330c342..34b11568d5 100644
--- a/src/ir/expression/tests.rs
+++ b/src/ir/expression/tests.rs
@@ -1,4 +1,3 @@
-use super::*;
 use crate::ir::value::*;
 use crate::ir::*;
 use pretty_assertions::assert_eq;
diff --git a/src/ir/operator.rs b/src/ir/operator.rs
index b227ff5665..d88ab69b78 100644
--- a/src/ir/operator.rs
+++ b/src/ir/operator.rs
@@ -1,14 +1,17 @@
 //! Operators for expression transformations.
 
+use std::collections::HashMap;
+
+use serde::{Deserialize, Serialize};
+
+use crate::errors::QueryPlannerError;
+
 use super::expression::Expression;
 use super::relation::Table;
 use super::{Node, Nodes, Plan};
-use crate::errors::QueryPlannerError;
-use serde::{Deserialize, Serialize};
-use std::collections::HashMap;
 
 /// Binary operator returning Bool expression.
-#[derive(Serialize, Deserialize, PartialEq, Debug)]
+#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
 pub enum Bool {
     /// `&&`
     And,
@@ -30,6 +33,27 @@ pub enum Bool {
     Or,
 }
 
+impl Bool {
+    /// Create `Bool` from operator string.
+    ///
+    /// # Errors
+    /// Returns `QueryPlannerError` when the operator is invalid.
+    pub fn from(s: &str) -> Result<Self, QueryPlannerError> {
+        match s.to_lowercase().as_str() {
+            "and" => Ok(Bool::And),
+            "or" => Ok(Bool::Or),
+            "=" => Ok(Bool::Eq),
+            "in" => Ok(Bool::In),
+            ">" => Ok(Bool::Gt),
+            ">=" => Ok(Bool::GtEq),
+            "<" => Ok(Bool::Lt),
+            "<=" => Ok(Bool::LtEq),
+            "!=" | "<>" => Ok(Bool::NotEq),
+            _ => Err(QueryPlannerError::InvalidBool),
+        }
+    }
+}
+
 /// Relational algebra operator returning a new tuple.
 ///
 /// Transforms input tuple(s) into the output one using
-- 
GitLab