diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..53db109 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,28 @@ +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub enum ForumInputError { + ForbiddenCharacter, + ForbiddenString, + EmptyParameter, +} + +impl std::fmt::Display for ForumInputError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + &ForumInputError::ForbiddenCharacter => { + write!(f, "Forbidden Character Detected") + } + &ForumInputError::ForbiddenString => { + write!(f, "Forbidden String Detected") + } + &ForumInputError::EmptyParameter => write!(f, "Parameter is Empty"), + } + } +} + +impl std::error::Error for ForumInputError { + fn cause(&self) -> Option<&dyn std::error::Error> { + self.source() + } +} diff --git a/src/lib.rs b/src/lib.rs index 2381027..27fc9e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,4 +1,5 @@ pub mod database; +pub mod error; pub mod feature; pub mod utils; diff --git a/src/utils.rs b/src/utils.rs index 4650a52..565e99e 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,5 +1,7 @@ use std::{fs::File, io::Read}; +use crate::error::ForumInputError; + pub fn naive_toml_parser(file_location: &str) -> (String, Vec) { let mut toml_file = File::open(file_location).unwrap(); let mut toml_ingredients = String::default(); @@ -22,3 +24,16 @@ pub fn naive_toml_parser(file_location: &str) -> (String, Vec) { (header, parsed) } + +pub fn input_checker(input: &String) -> Result { + if input.is_empty() { + Err(ForumInputError::EmptyParameter) + } else { + let input_without_space = input.split(' ').collect::>().join(""); + if input_without_space.chars().all(char::is_alphanumeric) { + Ok(input.to_owned()) + } else { + Err(ForumInputError::ForbiddenCharacter) + } + } +}