From e34adfddcf477daa85ff843c609e10bc1212e60a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=9CM=C3=9C=C5=9E?= <96421894+Tahinli@users.noreply.github.com> Date: Wed, 11 Dec 2024 21:28:40 +0300 Subject: [PATCH] feat: :sparkles: error type and input check --- src/error.rs | 28 ++++++++++++++++++++++++++++ src/lib.rs | 1 + src/utils.rs | 15 +++++++++++++++ 3 files changed, 44 insertions(+) create mode 100644 src/error.rs 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) + } + } +}