Add module to handle configuration files

next
Sayan Nandan 4 years ago
parent 308e57c846
commit 0690be4d4e
No known key found for this signature in database
GPG Key ID: C31EFD7DDA12AEE0

34
Cargo.lock generated

@ -127,12 +127,6 @@ dependencies = [
"libc",
]
[[package]]
name = "itoa"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
[[package]]
name = "kernel32-sys"
version = "0.2.2"
@ -394,12 +388,6 @@ version = "0.6.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
[[package]]
name = "ryu"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -426,17 +414,6 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.57"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "164eacbdb13512ec2745fb09d51fd5b22b0d65ed294a1dcf7285a360c80a675c"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "signal-hook-registry"
version = "1.2.0"
@ -492,8 +469,8 @@ dependencies = [
"libtdb",
"parking_lot",
"serde",
"serde_json",
"tokio",
"toml",
]
[[package]]
@ -549,6 +526,15 @@ dependencies = [
"syn",
]
[[package]]
name = "toml"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffc92d160b1eef40665be3a05630d003936a3bc7da7421277846c2613e92c71a"
dependencies = [
"serde",
]
[[package]]
name = "tsh"
version = "0.4.0"

@ -0,0 +1,3 @@
# This is a 'bad' configuration file since it contains an invalid port
[server]
port = 20033002

@ -0,0 +1,4 @@
# This is a 'good' configuration file since it contains a valid port
# and appropriate keys
[server]
port = 2003 # Set the server's port to 2003

@ -13,7 +13,7 @@ libtdb = {path ="../libtdb"}
bincode = "1.3.1"
parking_lot = "0.11.0"
lazy_static = "1.4.0"
serde = {version= "1.0.115", features=["derive"]}
serde_json = "1.0.57"
serde = {version = "1.0.115", features= ["derive"]}
toml = "0.5.6"
[dev-dependencies]
tokio = { version = "0.2", features = ["test-util"] }

@ -0,0 +1,95 @@
/*
* Created on Tue Sep 01 2020
*
* This file is a part of TerrabaseDB
* Copyright (c) 2020, Sayan Nandan <ohsayan at outlook dot com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
//! This module provides tools to handle configuration files and settings
use libtdb::TResult;
use serde::Deserialize;
use toml;
#[derive(Deserialize, Debug, PartialEq)]
struct Config {
server: ServerConfig,
}
#[derive(Deserialize, Debug, PartialEq)]
struct ServerConfig {
port: u16,
}
impl Config {
pub fn new(file: String) -> TResult<Self> {
let res: Config = toml::from_str(&file)?;
Ok(res)
}
}
#[test]
#[cfg(test)]
fn test_config_toml_okayport() {
let file = r#"
[server]
port = 2003
"#
.to_owned();
let cfg = Config::new(file).unwrap();
assert_eq!(
cfg,
Config {
server: ServerConfig { port: 2003 }
}
);
}
#[test]
#[cfg(test)]
fn test_config_toml_badport() {
let file = r#"
[server]
port = 20033002
"#
.to_owned();
let cfg = Config::new(file);
assert!(cfg.is_err());
}
#[test]
#[cfg(test)]
fn test_config_file_ok() {
let fileloc = "../examples/config-files/tdb.toml";
let file = std::fs::read_to_string(fileloc).unwrap();
let cfg: Config = Config::new(file).unwrap();
assert_eq!(
cfg,
Config {
server: ServerConfig { port: 2003 }
}
);
}
#[test]
#[cfg(test)]
fn test_config_file_err() {
let fileloc = "../examples/config-files/badcfg.toml";
let file = std::fs::read_to_string(fileloc).unwrap();
let cfg = Config::new(file);
assert!(cfg.is_err());
}

@ -20,6 +20,7 @@
*/
use tokio::net::TcpListener;
mod config;
mod coredb;
mod dbnet;
mod diskstore;

Loading…
Cancel
Save