update http lib + remove utils mod + remove lib prefix when calling functions

This commit is contained in:
landrigun 2023-02-15 16:59:28 +00:00
parent bfd539731d
commit 881bd2e24d
7 changed files with 12 additions and 47 deletions

4
Cargo.lock generated
View File

@ -635,8 +635,8 @@ dependencies = [
[[package]] [[package]]
name = "http" name = "http"
version = "0.1.2" version = "0.1.3"
source = "git+https://gitea.thegux.fr/rmanach/http#fb164ba137c3f1492f7452ac29d5d08afd30e87d" source = "git+https://gitea.thegux.fr/rmanach/http#b8c0fbba0b62906823a79e34bb2eadc1fe419d90"
dependencies = [ dependencies = [
"json", "json",
"lazy_static", "lazy_static",

View File

@ -16,7 +16,7 @@ simple_logger = "4.0.0"
log = "0.4.17" log = "0.4.17"
base64 = "0.13.1" 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 ?) # useful for tests (embedded files should be delete in release ?)
#rust-embed="6.4.1" #rust-embed="6.4.1"

View File

@ -2,7 +2,6 @@ mod config;
mod jwt; mod jwt;
mod router; mod router;
mod stores; mod stores;
mod utils;
use clap::Parser; use clap::Parser;
use configparser::ini::Ini; use configparser::ini::Ini;

View File

@ -1,5 +1,5 @@
use async_trait::async_trait; use async_trait::async_trait;
use json; use json::JsonValue;
use std::path::Path; use std::path::Path;
use super::store::{Credentials, Store}; use super::store::{Credentials, Store};
@ -67,7 +67,7 @@ impl FileStore {
#[async_trait] #[async_trait]
impl Store for FileStore { impl Store for FileStore {
async fn is_auth(&mut self, data: &json::JsonValue) -> Option<Credentials> { async fn is_auth(&mut self, data: &JsonValue) -> Option<Credentials> {
// ensure that the store file already exists even after its instanciation // ensure that the store file already exists even after its instanciation
if !Path::new(&self.path).is_file() { if !Path::new(&self.path).is_file() {
log::error!("{} path referencing file store does not exist", self.path); log::error!("{} path referencing file store does not exist", self.path);

View File

@ -1,11 +1,11 @@
use async_trait::async_trait; use async_trait::async_trait;
use json; use json::JsonValue;
use crate::utils::extract_json_value; use http::extract_json_value;
#[async_trait] #[async_trait]
pub trait Store { pub trait Store {
async fn is_auth(&mut self, data: &json::JsonValue) -> Option<Credentials>; async fn is_auth(&mut self, data: &JsonValue) -> Option<Credentials>;
} }
#[derive(Default, Debug)] #[derive(Default, Debug)]
@ -24,11 +24,11 @@ impl Credentials {
} }
} }
impl From<&json::JsonValue> for Credentials { impl From<&JsonValue> for Credentials {
fn from(data: &json::JsonValue) -> Self { fn from(data: &JsonValue) -> Self {
let mut credentials = Credentials::default(); let mut credentials = Credentials::default();
match data { match data {
json::JsonValue::Object(ref d) => { JsonValue::Object(ref d) => {
credentials.email = extract_json_value(&d, "email").unwrap_or("".to_string()); credentials.email = extract_json_value(&d, "email").unwrap_or("".to_string());
credentials.password = extract_json_value(&d, "password").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] #[test]
fn test_credentials() { fn test_credentials() {
struct Expect { struct Expect {
data: json::JsonValue, data: JsonValue,
is_empty: bool, is_empty: bool,
} }
let test_cases: [Expect; 2] = [ let test_cases: [Expect; 2] = [

View File

@ -1,4 +0,0 @@
//! includes utility function, that's all !
mod utils;
pub use utils::extract_json_value;

View File

@ -1,30 +0,0 @@
use json::object::Object;
/// extracts JSON value from a key
pub fn extract_json_value(data: &Object, key: &str) -> Option<String> {
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),
}
}
}