diff --git a/Cargo.lock b/Cargo.lock index 8ee4025..d925400 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -635,8 +635,8 @@ dependencies = [ [[package]] name = "http" -version = "0.1.2" -source = "git+https://gitea.thegux.fr/rmanach/http#fb164ba137c3f1492f7452ac29d5d08afd30e87d" +version = "0.1.3" +source = "git+https://gitea.thegux.fr/rmanach/http#b8c0fbba0b62906823a79e34bb2eadc1fe419d90" dependencies = [ "json", "lazy_static", diff --git a/Cargo.toml b/Cargo.toml index f43bf0d..a0dc9e1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,7 +16,7 @@ simple_logger = "4.0.0" log = "0.4.17" base64 = "0.13.1" -http = { git = "https://gitea.thegux.fr/rmanach/http", version = "0.1.2" } +http = { git = "https://gitea.thegux.fr/rmanach/http", version = "0.1.3" } # useful for tests (embedded files should be delete in release ?) #rust-embed="6.4.1" diff --git a/src/main.rs b/src/main.rs index 7407b1a..9b9384d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,6 @@ mod config; mod jwt; mod router; mod stores; -mod utils; use clap::Parser; use configparser::ini::Ini; diff --git a/src/stores/file.rs b/src/stores/file.rs index 3f0cb1b..f3839d0 100644 --- a/src/stores/file.rs +++ b/src/stores/file.rs @@ -1,5 +1,5 @@ use async_trait::async_trait; -use json; +use json::JsonValue; use std::path::Path; use super::store::{Credentials, Store}; @@ -67,7 +67,7 @@ impl FileStore { #[async_trait] impl Store for FileStore { - async fn is_auth(&mut self, data: &json::JsonValue) -> Option { + async fn is_auth(&mut self, data: &JsonValue) -> Option { // ensure that the store file already exists even after its instanciation if !Path::new(&self.path).is_file() { log::error!("{} path referencing file store does not exist", self.path); diff --git a/src/stores/store.rs b/src/stores/store.rs index f034103..ec84e04 100644 --- a/src/stores/store.rs +++ b/src/stores/store.rs @@ -1,11 +1,11 @@ use async_trait::async_trait; -use json; +use json::JsonValue; -use crate::utils::extract_json_value; +use http::extract_json_value; #[async_trait] pub trait Store { - async fn is_auth(&mut self, data: &json::JsonValue) -> Option; + async fn is_auth(&mut self, data: &JsonValue) -> Option; } #[derive(Default, Debug)] @@ -24,11 +24,11 @@ impl Credentials { } } -impl From<&json::JsonValue> for Credentials { - fn from(data: &json::JsonValue) -> Self { +impl From<&JsonValue> for Credentials { + fn from(data: &JsonValue) -> Self { let mut credentials = Credentials::default(); match data { - json::JsonValue::Object(ref d) => { + JsonValue::Object(ref d) => { credentials.email = extract_json_value(&d, "email").unwrap_or("".to_string()); credentials.password = extract_json_value(&d, "password").unwrap_or("".to_string()); } @@ -41,7 +41,7 @@ impl From<&json::JsonValue> for Credentials { #[test] fn test_credentials() { struct Expect { - data: json::JsonValue, + data: JsonValue, is_empty: bool, } let test_cases: [Expect; 2] = [ diff --git a/src/utils/mod.rs b/src/utils/mod.rs deleted file mode 100644 index 4815b85..0000000 --- a/src/utils/mod.rs +++ /dev/null @@ -1,4 +0,0 @@ -//! includes utility function, that's all ! -mod utils; - -pub use utils::extract_json_value; diff --git a/src/utils/utils.rs b/src/utils/utils.rs deleted file mode 100644 index e330062..0000000 --- a/src/utils/utils.rs +++ /dev/null @@ -1,30 +0,0 @@ -use json::object::Object; - -/// extracts JSON value from a key -pub fn extract_json_value(data: &Object, key: &str) -> Option { - match data.get(key) { - Some(u) => match u.as_str() { - Some(s) => return Some(s.to_string()), - None => None, - }, - None => None, - } -} - -#[test] -fn test_extract_json_value() { - let test_cases: [(json::JsonValue, bool, bool); 3] = [ - (json::parse(r#"{"test": ""}"#).unwrap(), true, true), - (json::parse(r#"{}"#).unwrap(), true, false), - (json::parse(r#"[]"#).unwrap(), false, false), - ]; - - for (value, is_valid, has_key) in test_cases { - match value { - json::JsonValue::Object(d) => { - assert_eq!(has_key, extract_json_value(&d, "test").is_some()); - } - _ => assert!(!is_valid), - } - } -}