From 11052951b74b9ad4b6a9412ae490086235f9154b Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 3 Jan 2021 13:40:40 +0100 Subject: Rewrite in Rust --- src/validation/category.rs | 18 ++++++++++++++++ src/validation/income.rs | 22 +++++++++++++++++++ src/validation/login.rs | 11 ++++++++++ src/validation/mod.rs | 5 +++++ src/validation/payment.rs | 25 +++++++++++++++++++++ src/validation/utils.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 135 insertions(+) create mode 100644 src/validation/category.rs create mode 100644 src/validation/income.rs create mode 100644 src/validation/login.rs create mode 100644 src/validation/mod.rs create mode 100644 src/validation/payment.rs create mode 100644 src/validation/utils.rs (limited to 'src/validation') diff --git a/src/validation/category.rs b/src/validation/category.rs new file mode 100644 index 0000000..7b1b5d5 --- /dev/null +++ b/src/validation/category.rs @@ -0,0 +1,18 @@ +use std::collections::HashMap; + +use crate::model::category::{Create, Update}; +use crate::validation::utils::*; + +pub fn create(form: &HashMap) -> Option { + Some(Create { + name: non_empty(form, "name")?, + color: color(form, "color")?, + }) +} + +pub fn update(form: &HashMap) -> Option { + Some(Update { + name: non_empty(form, "name")?, + color: color(form, "color")?, + }) +} diff --git a/src/validation/income.rs b/src/validation/income.rs new file mode 100644 index 0000000..972e42a --- /dev/null +++ b/src/validation/income.rs @@ -0,0 +1,22 @@ +use std::collections::HashMap; + +use crate::model::income::{Create, Update}; +use crate::validation::utils::*; + +pub fn create(form: &HashMap) -> Option { + Some(Create { + user_id: parse::(form, "user_id")?, + amount: parse::(form, "amount")?, + month: parse::(form, "month")?, + year: parse::(form, "year")?, + }) +} + +pub fn update(form: &HashMap) -> Option { + Some(Update { + user_id: parse::(form, "user_id")?, + amount: parse::(form, "amount")?, + month: parse::(form, "month")?, + year: parse::(form, "year")?, + }) +} diff --git a/src/validation/login.rs b/src/validation/login.rs new file mode 100644 index 0000000..e40bb23 --- /dev/null +++ b/src/validation/login.rs @@ -0,0 +1,11 @@ +use std::collections::HashMap; + +use crate::model::login::Login; +use crate::validation::utils::*; + +pub fn login(form: &HashMap) -> Option { + Some(Login { + email: non_empty(form, "email")?, + password: non_empty(form, "password")?, + }) +} diff --git a/src/validation/mod.rs b/src/validation/mod.rs new file mode 100644 index 0000000..181abc7 --- /dev/null +++ b/src/validation/mod.rs @@ -0,0 +1,5 @@ +pub mod category; +pub mod income; +pub mod login; +pub mod payment; +pub mod utils; diff --git a/src/validation/payment.rs b/src/validation/payment.rs new file mode 100644 index 0000000..36aa852 --- /dev/null +++ b/src/validation/payment.rs @@ -0,0 +1,25 @@ +use std::collections::HashMap; + +use crate::model::payment::{Create, Update}; +use crate::validation::utils::*; + +pub fn create(form: &HashMap) -> Option { + Some(Create { + name: non_empty(form, "name")?, + cost: parse::(form, "cost")?, + user_id: parse::(form, "user_id")?, + category_id: parse::(form, "category_id")?, + date: date(form, "date")?, + frequency: frequency(form, "frequency")?, + }) +} + +pub fn update(form: &HashMap) -> Option { + Some(Update { + name: non_empty(form, "name")?, + cost: parse::(form, "cost")?, + user_id: parse::(form, "user_id")?, + category_id: parse::(form, "category_id")?, + date: date(form, "date")?, + }) +} diff --git a/src/validation/utils.rs b/src/validation/utils.rs new file mode 100644 index 0000000..4bff40a --- /dev/null +++ b/src/validation/utils.rs @@ -0,0 +1,54 @@ +use chrono::NaiveDate; +use std::collections::HashMap; +use std::str::FromStr; + +use crate::model::frequency::Frequency; + +pub fn non_empty( + form: &HashMap, + field: &str, +) -> Option { + let s = form.get(field)?.trim(); + if s.is_empty() { + None + } else { + Some(s.to_string()) + } +} + +pub fn parse( + form: &HashMap, + field: &str, +) -> Option { + let s = form.get(field)?; + s.parse::().ok() +} + +pub fn date(form: &HashMap, field: &str) -> Option { + let s = form.get(field)?; + NaiveDate::parse_from_str(s, "%Y-%m-%d").ok() +} + +pub fn frequency( + form: &HashMap, + field: &str, +) -> Option { + let s = form.get(field)?; + Frequency::from_str(s).ok() +} + +pub fn color(form: &HashMap, field: &str) -> Option { + let s = form.get(field)?; + if s.len() == 7 + && &s[0..1] == "#" + && s[1..] + .to_string() + .into_bytes() + .into_iter() + .all(|c| c.is_ascii_hexdigit()) + { + Some(s.to_string()) + } else { + None + } +} -- cgit v1.2.3