Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch config to YAML #239

Merged
merged 10 commits into from
Feb 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ version = "3.0.0-dev"
authors = ["Artem Zinnatullin <[email protected]>", "Artur Dryomov <[email protected]>", "Mainframer Developers and Contributors"]

[dependencies]
yaml-rust = "0.4.2"
linked-hash-map = "0.5.1"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, what? No maps in Rust?

166 changes: 13 additions & 153 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,161 +1,21 @@
use std::fs::File;
use std::io::prelude::*;
use std::path::Path;

#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq)]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still have no idea why language has PartialEq and Eq.

pub struct Config {
pub remote_machine_name: String,
pub local_compression_level: u8,
pub remote_compression_level: u8,
}

impl Config {
pub fn from_file(file_path: &Path) -> Result<Config, String> {
let mut content = String::new();

let mut file = match File::open(file_path) {
Err(_) => return Err(format!("Could not open config file '{}'.", file_path.to_string_lossy())),
Ok(value) => value,
};

file.read_to_string(&mut content)
.unwrap_or_else(|_| panic!("Could not read config file '{}'.", file_path.to_string_lossy()));

match parse_config_from_str(&content) {
Err(message) => Err(format!("Error during parsing config file '{}'\n{}", file_path.to_string_lossy(), message)),
Ok(config) => Ok(config)
}
}
pub remote: Remote,
pub push: Push,
pub pull: Pull,
}

// Parses config content as https://en.wikipedia.org/wiki/.properties
fn find_value(config_content: &str, key: &str) -> Option<String> {
match config_content.find(&format!("{}=", key)) {
None => None,
Some(start_index) => {
let content_starting_with_key = &config_content[start_index..config_content.len()];
let value_start_index = match content_starting_with_key.find('=') {
None => return None,
Some(index) => index
};

let line_end_index = match content_starting_with_key.find('\n') {
None => content_starting_with_key.len(),
Some(index) => index
};

let value = (&content_starting_with_key[value_start_index + 1..line_end_index]).trim();

match value.len() {
0 => None,
_ => Some(String::from(value))
}
}
}
#[derive(Debug, PartialEq)]
pub struct Remote {
pub host: String,
}

fn parse_config_from_str(config_content: &str) -> Result<Config, String> {
Ok(Config {
remote_machine_name: match find_value(&config_content, &"remote_machine") {
None => return Err("please specify 'remote_machine'.".to_string()),
Some(value) => value
},
local_compression_level: match find_value(&config_content, &"local_compression_level") {
None => 1,
Some(value) => match value.parse() {
Err(_) => return Err(format!("'local_compression_level' must be a positive number, found '{}'.", value)),
Ok(value) => value
}
},
remote_compression_level: match find_value(&config_content, &"remote_compression_level") {
None => 1,
Some(value) => match value.parse() {
Err(_) => return Err(format!("'remote_compression_level' must be a positive number, found '{}'.", value)),
Ok(value) => value
}
},
})
#[derive(Debug, PartialEq)]
pub struct Push {
pub compression: u8,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn find_value_no_new_line_at_the_end() {
let content = "key=value";
assert_eq!(find_value(content, "key"), Some(String::from("value")));
}

#[test]
fn find_value_new_line_at_the_end() {
let content = "key=value\n";
assert_eq!(find_value(content, "key"), Some(String::from("value")));
}

#[test]
fn find_value_trims_value() {
let content = "key=value ";
assert_eq!(find_value(content, "key"), Some(String::from("value")));
}

#[test]
fn find_value_no_value_after_equals_sign() {
let content = "key=";
assert_eq!(find_value(content, "key"), None);
}

#[test]
fn find_value_whitespace_instead_of_value() {
let content = "key= ";
assert_eq!(find_value(content, "key"), None);
}

#[test]
fn parse_config_from_str_all_fields() {
let content = "remote_machine=test@machine\nlocal_compression_level=2\nremote_compression_level=3";
assert_eq!(parse_config_from_str(content), Ok(Config {
remote_machine_name: String::from("test@machine"),
local_compression_level: 2,
remote_compression_level: 3,
}));
}

#[test]
fn parse_config_from_str_all_fields_unordered() {
let content = "remote_compression_level=3\nremote_machine=test@machine\nlocal_compression_level=2";
assert_eq!(parse_config_from_str(content), Ok(Config {
remote_machine_name: String::from("test@machine"),
local_compression_level: 2,
remote_compression_level: 3,
}));
}

#[test]
fn parse_config_from_str_only_remote_machine_name() {
let content = "remote_machine=test@machine";
assert_eq!(parse_config_from_str(content), Ok(Config {
remote_machine_name: String::from("test@machine"),
local_compression_level: 1, // Default value.
remote_compression_level: 1, // Default value.
}));
}

#[test]
fn parse_config_from_str_no_remote_machine_name() {
let content = "local_compression_level=2\nremote_compression_level=3";
assert_eq!(parse_config_from_str(content), Err(String::from("please specify 'remote_machine'.")));
}

#[test]
fn parse_config_from_str_local_compression_level_not_a_number() {
let content = "remote_machine=test@machine\nlocal_compression_level=yooo";
assert_eq!(parse_config_from_str(content), Err(String::from("'local_compression_level' must be a positive number, found 'yooo'.")));
}

#[test]
fn parse_config_from_str_remote_compression_level_not_a_number() {
let content = "remote_machine=test@machine\nremote_compression_level=wut";
assert_eq!(parse_config_from_str(content), Err(String::from("'remote_compression_level' must be a positive number, found 'wut'.")));
}
#[derive(Debug, PartialEq)]
pub struct Pull {
pub compression: u8,
}
Loading