use super::*;
use std::fmt;
#[derive(PartialEq, Debug)]
pub enum Keyword {
SELECT,
FROM,
WHERE,
ORDER,
BY,
CREATE,
TABLE,
TRIGGER,
DATABASE,
IF,
ELSE,
END,
DESC,
ASC,
AS,
TOP,
BOTTOM,
DISTINCT,
LIMIT,
OFFSET,
GROUP,
HAVING,
JOIN,
LIKE,
IN,
AND,
UNKNOWN,
}
impl Keyword {
pub fn value_for_command(&self) -> i32 {
match self {
Keyword::SELECT => 1,
_ => 10,
}
}
pub fn needs_data(&self) -> bool {
match self {
Keyword::SELECT => true,
Keyword::FROM => true,
_ => false,
}
}
pub fn bigger_than(&self, comparator: &Keyword) -> bool {
if self.compare_command(comparator) != 1 {
return false;
}
true
}
pub fn compare_command(&self, comparator: &Keyword) -> i32 {
if self.value_for_command() > comparator.value_for_command() {
1
} else if self.value_for_command() == comparator.value_for_command() {
0
} else {
-1
}
}
}
pub fn unwrap_string(mut convertable: String) -> Keyword {
convertable = convertable.to_lowercase();
match convertable.as_ref() {
"select" => Keyword::SELECT,
"where" => Keyword::WHERE,
"from" => Keyword::FROM,
"create" => Keyword::CREATE,
"table" => Keyword::TABLE,
"database" => Keyword::DATABASE,
"order" => Keyword::ORDER,
"if" => Keyword::IF,
"else" => Keyword::ELSE,
"distinct" => Keyword::DISTINCT,
"top" => Keyword::TOP,
"end" => Keyword::END,
"limit" => Keyword::LIMIT,
"having" => Keyword::HAVING,
"like" => Keyword::LIKE,
_ => Keyword::UNKNOWN,
}
}
impl fmt::Display for Keyword {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(self, f)
}
}
pub fn identifiy_data_after_keyword(_expression: expression::Expression) {}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn return_enum_value_for_keyword_string() {
let keyword_strings = vec!["SELECT", "FROM", "WHERE"];
let result_return = vec![Keyword::SELECT, Keyword::FROM, Keyword::WHERE];
let mut returned_result_vec: Vec<Keyword> = Vec::new();
for i in 0..keyword_strings.len() {
returned_result_vec.push(grammar::unwrap_string(keyword_strings[i].to_string()));
}
assert_eq!(result_return, returned_result_vec);
}
#[test]
fn get_wheiht_for_enum_value() {
let enum_value = Keyword::SELECT;
let expected_result = 1;
assert_eq!(expected_result, enum_value.value_for_command());
}
#[test]
fn keyword_needs_values() {
assert_eq!(Keyword::SELECT.needs_data(), true);
assert_eq!(Keyword::FROM.needs_data(), true);
assert_eq!(Keyword::ORDER.needs_data(), false);
}
}