Skip to content

Commit

Permalink
style: 🎨 ran cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Jun 29, 2021
1 parent 673a61c commit 80e0507
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 64 deletions.
7 changes: 5 additions & 2 deletions src/bin/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::env;

use bonnie_lib::{get_cfg, get_cfg_path, get_command_from_cfg_and_args, help, init, run_cmd, BONNIE_VERSION};
use bonnie_lib::{
get_cfg, get_cfg_path, get_command_from_cfg_and_args, help, init, run_cmd, BONNIE_VERSION,
};

// TODO colorise output?

Expand Down Expand Up @@ -36,7 +38,8 @@ fn main() {

// Get the command to run from the arguments the user gave and the configuration file
// We parse the current version in directly here (only extracted as an argument for testing purposes)
let command_with_args = get_command_from_cfg_and_args(cfg_string, prog_args, BONNIE_VERSION);
let command_with_args =
get_command_from_cfg_and_args(cfg_string, prog_args, BONNIE_VERSION);
let command_with_args = match command_with_args {
Ok(command_with_args) => command_with_args,
Err(err) => return eprintln!("{}", err),
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub const DEFAULT_BONNIE_CFG_PATH: &str = "./bonnie.toml";
pub fn get_command_from_cfg_and_args(
cfg_string: String,
prog_args: Vec<String>,
version_str: &str // Extracted for testing purposes
version_str: &str, // Extracted for testing purposes
) -> Result<String, String> {
let cfg = parse_cfg(cfg_string, version_str)?; // This also loads necessary environment variable files
let registry = get_commands_registry_from_cfg(&cfg);
Expand Down Expand Up @@ -72,7 +72,9 @@ pub fn init() -> Result<(), String> {
// Create a new `bonnie.toml` file
let output = fs::write(
"./bonnie.toml",
"version = \"".to_string() + BONNIE_VERSION + "\"
"version = \"".to_string()
+ BONNIE_VERSION
+ "\"
[scripts]
start = \"echo \\\"No start script yet.\\\"\"
Expand Down
2 changes: 1 addition & 1 deletion src/read_cfg.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use serde::Deserialize;
use std::collections::HashMap;

use crate::version::{get_version_parts, VersionCompatibility, VersionDifference};
use crate::command::Command;
use crate::commands_registry::CommandsRegistry;
use crate::version::{get_version_parts, VersionCompatibility, VersionDifference};

// Anything with `Raw` in front of it is deserialised directly into
// Anything without `Raw` in front of it is the final, parsed form
Expand Down
58 changes: 31 additions & 27 deletions src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,39 +17,49 @@ pub enum VersionCompatibility {
DifferentMajor(VersionDifference), // Only this means the versions are incompatible
DifferentMinor(VersionDifference),
DifferentPatch(VersionDifference),
DifferentBetaVersion(VersionDifference) // In beta, this also means the versions are incompatible
DifferentBetaVersion(VersionDifference), // In beta, this also means the versions are incompatible
}

#[derive(Debug, PartialEq, Eq)]
pub struct Version {
patch: u16,
minor: u16,
major: u16
major: u16,
}
impl Version {
// Compares this with another version returns their compatibility
// It will return an embedded version difference as to whether the version being compared to is too old/new or nothing if they're identical
pub fn is_compatible_with(&self, comparison: &Version) -> VersionCompatibility {
let compatibility = match self.major {
_ if self.major > comparison.major => VersionCompatibility::DifferentMajor(VersionDifference::TooOld),
_ if self.major < comparison.major => VersionCompatibility::DifferentMajor(VersionDifference::TooNew),
_ if self.minor > comparison.minor => VersionCompatibility::DifferentMinor(VersionDifference::TooOld),
_ if self.minor < comparison.minor => VersionCompatibility::DifferentMinor(VersionDifference::TooNew),
_ if self.patch > comparison.patch => VersionCompatibility::DifferentPatch(VersionDifference::TooOld),
_ if self.patch < comparison.patch => VersionCompatibility::DifferentPatch(VersionDifference::TooNew),
_ => VersionCompatibility::Identical
_ if self.major > comparison.major => {
VersionCompatibility::DifferentMajor(VersionDifference::TooOld)
}
_ if self.major < comparison.major => {
VersionCompatibility::DifferentMajor(VersionDifference::TooNew)
}
_ if self.minor > comparison.minor => {
VersionCompatibility::DifferentMinor(VersionDifference::TooOld)
}
_ if self.minor < comparison.minor => {
VersionCompatibility::DifferentMinor(VersionDifference::TooNew)
}
_ if self.patch > comparison.patch => {
VersionCompatibility::DifferentPatch(VersionDifference::TooOld)
}
_ if self.patch < comparison.patch => {
VersionCompatibility::DifferentPatch(VersionDifference::TooNew)
}
_ => VersionCompatibility::Identical,
};
// If we're in beta (0.x.x), any difference is tantamount to treason
if self.major == 0 && !matches!(compatibility, VersionCompatibility::Identical) {
// Here we figure out if the comparison version is too old or too new
VersionCompatibility::DifferentBetaVersion(
match compatibility {
VersionCompatibility::DifferentMajor(version_difference) => version_difference,
VersionCompatibility::DifferentMinor(version_difference) => version_difference,
VersionCompatibility::DifferentPatch(version_difference) => version_difference,
_ => panic!("Critical logic failure. You should report this as a bug.") // This shouldn't be possible, we know more than the compiler
}
)
VersionCompatibility::DifferentBetaVersion(match compatibility {
VersionCompatibility::DifferentMajor(version_difference) => version_difference,
VersionCompatibility::DifferentMinor(version_difference) => version_difference,
VersionCompatibility::DifferentPatch(version_difference) => version_difference,
_ => panic!("Critical logic failure. You should report this as a bug."), // This shouldn't be possible, we know more than the compiler
})
} else {
compatibility
}
Expand Down Expand Up @@ -88,7 +98,7 @@ pub fn get_version_parts(version_str: &str) -> Result<Version, String> {
Ok(Version {
patch,
minor,
major
major,
})
}

Expand All @@ -101,7 +111,7 @@ fn build_version(parts: Vec<u16>) -> Version {
Version {
patch: parts[2],
minor: parts[1],
major: parts[0]
major: parts[0],
}
}

Expand All @@ -113,10 +123,7 @@ fn identifies_identical_versions() {
let comparison = build_version(vec![2, 3, 4]);
let compat = version.is_compatible_with(&comparison);

assert_eq!(
compat,
VersionCompatibility::Identical
);
assert_eq!(compat, VersionCompatibility::Identical);
}
#[test]
fn identifies_major_too_new() {
Expand Down Expand Up @@ -191,10 +198,7 @@ fn identifies_identical_versions_in_beta() {
let comparison = build_version(vec![0, 3, 4]);
let compat = version.is_compatible_with(&comparison);

assert_eq!(
compat,
VersionCompatibility::Identical
);
assert_eq!(compat, VersionCompatibility::Identical);
}
#[test]
fn identifies_major_too_new_in_beta() {
Expand Down
85 changes: 53 additions & 32 deletions tests/get_command_from_cfg_and_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,87 +343,90 @@ fn returns_correct_command_on_identical_version() {
let cfg_version = "1.2.3";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
let command_with_args = get_command_from_cfg_and_args(conf, prog_args, cfg_version);
assert_eq!(
command_with_args,
Ok(String::from("echo Hello World"))
)
assert_eq!(command_with_args, Ok(String::from("echo Hello World")))
}
#[test]
fn returns_correct_command_on_minor_version_too_high() {
let bonnie_version = "1.2.3";
let cfg_version = "1.3.3";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
let command_with_args = get_command_from_cfg_and_args(conf, prog_args, cfg_version);
assert_eq!(
command_with_args,
Ok(String::from("echo Hello World"))
)
assert_eq!(command_with_args, Ok(String::from("echo Hello World")))
}
#[test]
fn returns_correct_command_on_minor_version_too_low() {
let bonnie_version = "1.2.3";
let cfg_version = "1.1.3";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
let command_with_args = get_command_from_cfg_and_args(conf, prog_args, cfg_version);
assert_eq!(
command_with_args,
Ok(String::from("echo Hello World"))
)
assert_eq!(command_with_args, Ok(String::from("echo Hello World")))
}
#[test]
fn returns_correct_command_on_patch_version_too_high() {
let bonnie_version = "1.2.3";
let cfg_version = "1.2.4";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
let command_with_args = get_command_from_cfg_and_args(conf, prog_args, cfg_version);
assert_eq!(
command_with_args,
Ok(String::from("echo Hello World"))
)
assert_eq!(command_with_args, Ok(String::from("echo Hello World")))
}
#[test]
fn returns_correct_command_on_patch_version_too_low() {
let bonnie_version = "1.2.3";
let cfg_version = "1.2.2";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
let command_with_args = get_command_from_cfg_and_args(conf, prog_args, cfg_version);
assert_eq!(
command_with_args,
Ok(String::from("echo Hello World"))
)
assert_eq!(command_with_args, Ok(String::from("echo Hello World")))
}
#[test]
fn returns_error_on_major_version_too_high() {
let bonnie_version = "1.2.3";
let cfg_version = "2.2.3";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
Expand All @@ -438,7 +441,10 @@ fn returns_error_on_major_version_too_low() {
let cfg_version = "1.3.4";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
Expand All @@ -453,7 +459,10 @@ fn returns_error_on_minor_version_too_high_in_beta() {
let cfg_version = "1.2.3";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
Expand All @@ -468,7 +477,10 @@ fn returns_error_on_minor_version_too_low_in_beta() {
let cfg_version = "0.1.3";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
Expand All @@ -483,7 +495,10 @@ fn returns_error_on_patch_version_too_high_in_beta() {
let cfg_version = "0.2.4";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
Expand All @@ -498,7 +513,10 @@ fn returns_error_on_patch_version_too_low_in_beta() {
let cfg_version = "0.2.2";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
Expand All @@ -513,7 +531,10 @@ fn returns_error_on_major_version_too_high_in_beta() {
let cfg_version = "1.2.3";
let prog_args = vec!["".to_string(), "test".to_string()];
let conf = "
version = \"".to_string() + bonnie_version + "\"
version = \""
.to_string()
+ bonnie_version
+ "\"
[scripts]
test = \"echo Hello World\"
";
Expand Down

0 comments on commit 80e0507

Please sign in to comment.