Skip to content

Commit eef128d

Browse files
author
Alex
committed
Merge pull request #9 from slicebuild/formatting
Changed formatting to comply with rust standards
2 parents 9162b9d + 3301d2d commit eef128d

File tree

12 files changed

+268
-188
lines changed

12 files changed

+268
-188
lines changed

impl/rust/src/commands/fetch_command.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use zip::read::ZipArchive;
99
use super::command::Command;
1010

1111
pub struct FetchCommand<'a> {
12-
pub slice_root_directory: &'a Path
12+
pub slice_root_directory: &'a Path,
1313
}
1414

1515
impl<'a> FetchCommand<'a> {
@@ -46,7 +46,8 @@ impl<'a> FetchCommand<'a> {
4646
}
4747

4848
fn determine_latest_version() -> Version {
49-
let body = FetchCommand::execute_request_to_uri("https://api.github.com/repos/slicebuild/slices/branches");
49+
let body = FetchCommand::execute_request_to_uri("https://api.github.\
50+
com/repos/slicebuild/slices/branches");
5051
let body = String::from_utf8(body).unwrap();
5152
let json = Json::from_str(&body).unwrap();
5253
let array = json.as_array().unwrap();
@@ -55,11 +56,13 @@ impl<'a> FetchCommand<'a> {
5556

5657
fn download_latest_version() -> Vec<u8> {
5758
let version = FetchCommand::determine_latest_version();
58-
let uri = format!("https://codeload.github.com/slicebuild/slices/zip/{}", version);
59+
let uri = format!("https://codeload.github.com/slicebuild/slices/zip/{}",
60+
version);
5961
FetchCommand::execute_request_to_uri(&uri)
6062
}
6163

62-
fn extract_archive_into_directory(mut zip_archive: ZipArchive<Cursor<Vec<u8>>>, path: PathBuf) {
64+
fn extract_archive_into_directory(mut zip_archive: ZipArchive<Cursor<Vec<u8>>>,
65+
path: PathBuf) {
6366
for i in 0..zip_archive.len() {
6467
let mut path = path.clone();
6568
let mut file = zip_archive.by_index(i).unwrap();
@@ -88,6 +91,7 @@ impl<'a> Command for FetchCommand<'a> {
8891
let bytes = FetchCommand::download_latest_version();
8992
let cursor = Cursor::new(bytes);
9093
let zip_archive = ZipArchive::new(cursor).unwrap();
91-
FetchCommand::extract_archive_into_directory(zip_archive, self.slice_root_directory.to_path_buf());
94+
FetchCommand::extract_archive_into_directory(zip_archive,
95+
self.slice_root_directory.to_path_buf());
9296
}
9397
}

impl/rust/src/commands/find_command.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::path::Path;
22
use super::helper::check_slice_root_exists;
33
use super::command::Command;
4-
use super::super::slice::directory::{get_latest_slices_from_slice_root_directory};
4+
use super::super::slice::directory::get_latest_slices_from_slice_root_directory;
55
#[cfg(test)]
66
use super::super::for_testing::get_slice_root_directory;
77

88
pub struct FindCommand<'a> {
99
pub layer: String,
1010
pub os: String,
11-
pub slice_root_directory: &'a Path
11+
pub slice_root_directory: &'a Path,
1212
}
1313

1414
impl<'a> Command for FindCommand<'a> {
@@ -23,15 +23,19 @@ impl<'a> Command for FindCommand<'a> {
2323
for slice in slices {
2424
println!("{}", slice.name);
2525
}
26-
},
27-
Err(error) => println!("{}", error)
26+
}
27+
Err(error) => println!("{}", error),
2828
}
2929
}
3030
}
3131

3232
#[test]
3333
fn test_find_command_run() {
3434
let slice_root_directory = get_slice_root_directory();
35-
let mut command = FindCommand { layer: "jekyll".to_string(), os: "debian".to_string(), slice_root_directory: &slice_root_directory };
35+
let mut command = FindCommand {
36+
layer: "jekyll".to_string(),
37+
os: "debian".to_string(),
38+
slice_root_directory: &slice_root_directory,
39+
};
3640
command.run();
37-
}
41+
}

impl/rust/src/commands/helper.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ use std::fs::metadata;
33
use std::path::Path;
44

55
pub fn check_slice_root_exists(slice_root_directory: &Path) {
6-
if let Err(error) = metadata(slice_root_directory) {
7-
if error.kind() == ErrorKind::NotFound {
8-
println!("Slice root directory is not exists. Path = {}", slice_root_directory.to_str().unwrap());
9-
} else {
10-
println!("Unknown error = {}", error);
11-
}
12-
panic!();
13-
}
6+
if let Err(error) = metadata(slice_root_directory) {
7+
if error.kind() == ErrorKind::NotFound {
8+
println!("Slice root directory is not exists. Path = {}",
9+
slice_root_directory.to_str().unwrap());
10+
} else {
11+
println!("Unknown error = {}", error);
12+
}
13+
panic!();
14+
}
1415
}

impl/rust/src/commands/make_command.rs

Lines changed: 75 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -7,83 +7,100 @@ use super::super::slice::directory::get_latest_slices_from_slice_root_directory;
77
use super::super::slice::section::Kind;
88

99
pub struct MakeCommand<'a> {
10-
pub layer: String,
10+
pub layer: String,
1111
pub os: String,
12-
pub slice_root_directory: &'a Path
12+
pub slice_root_directory: &'a Path,
1313
}
1414

1515
impl<'a> MakeCommand<'a> {
16-
fn find_main_slice(&self, slices: &'a Vec<Slice>) -> &'a Slice {
17-
slices.iter().find(|slice| {
18-
slice.name.contains(&self.layer) && slice.get_os_list().contains(&self.os)
19-
}).unwrap()
20-
}
21-
22-
fn add_code_for_slice(current_code: &mut String, slice: &Slice, available_slices: &mut Vec<&Slice>) {
23-
if let Some(dep_section) = slice.sections.iter().find(|section| section.kind == Kind::Dep) {
24-
for dependency in &dep_section.items {
25-
let dependency_position = available_slices.iter().position(|slice| slice.name == *dependency);
26-
if let Some(dependency_position) = dependency_position {
27-
let dependency = available_slices.remove(dependency_position);
28-
MakeCommand::add_code_for_slice(current_code, &dependency, available_slices);
29-
}
30-
}
31-
}
32-
let run_section = slice.sections.iter().find(|section| section.kind == Kind::Run).unwrap();
33-
for item in &run_section.items {
34-
current_code.push_str(&item);
35-
current_code.push('\n');
36-
}
37-
}
38-
39-
fn get_code_for_latest_slice(&self) -> Result<String, String> {
40-
match get_latest_slices_from_slice_root_directory(&self.slice_root_directory) {
41-
Ok(slices) => {
42-
let mut available_slices: Vec<&Slice> = Vec::new();
43-
for slice in &slices {
44-
available_slices.push(&slice);
45-
}
46-
let main_layer = self.find_main_slice(&slices);
47-
let mut string = String::new();
48-
MakeCommand::add_code_for_slice(&mut string, &main_layer, &mut available_slices);
49-
Ok(string)
50-
},
51-
Err(error) => Err(error)
52-
}
53-
}
16+
fn find_main_slice(&self, slices: &'a Vec<Slice>) -> &'a Slice {
17+
slices.iter()
18+
.find(|slice| {
19+
slice.name.contains(&self.layer) && slice.get_os_list().contains(&self.os)
20+
})
21+
.unwrap()
22+
}
23+
24+
fn add_code_for_slice(current_code: &mut String,
25+
slice: &Slice,
26+
available_slices: &mut Vec<&Slice>) {
27+
if let Some(dep_section) = slice.sections.iter().find(|section| section.kind == Kind::Dep) {
28+
for dependency in &dep_section.items {
29+
let dependency_position = available_slices.iter().position(|slice| {
30+
slice.name == *dependency
31+
});
32+
if let Some(dependency_position) = dependency_position {
33+
let dependency = available_slices.remove(dependency_position);
34+
MakeCommand::add_code_for_slice(current_code, &dependency, available_slices);
35+
}
36+
}
37+
}
38+
let run_section = slice.sections.iter().find(|section| section.kind == Kind::Run).unwrap();
39+
for item in &run_section.items {
40+
current_code.push_str(&item);
41+
current_code.push('\n');
42+
}
43+
}
44+
45+
fn get_code_for_latest_slice(&self) -> Result<String, String> {
46+
match get_latest_slices_from_slice_root_directory(&self.slice_root_directory) {
47+
Ok(slices) => {
48+
let mut available_slices: Vec<&Slice> = Vec::new();
49+
for slice in &slices {
50+
available_slices.push(&slice);
51+
}
52+
let main_layer = self.find_main_slice(&slices);
53+
let mut string = String::new();
54+
MakeCommand::add_code_for_slice(&mut string, &main_layer, &mut available_slices);
55+
Ok(string)
56+
}
57+
Err(error) => Err(error),
58+
}
59+
}
5460
}
5561

5662
impl<'a> Command for MakeCommand<'a> {
57-
fn run(&mut self) {
58-
match self.get_code_for_latest_slice() {
59-
Ok(code) => println!("Code = {}", code),
60-
Err(error) => println!("Error = {}", error)
61-
}
62-
}
63+
fn run(&mut self) {
64+
match self.get_code_for_latest_slice() {
65+
Ok(code) => println!("Code = {}", code),
66+
Err(error) => println!("Error = {}", error),
67+
}
68+
}
6369
}
6470

6571
#[test]
6672
fn test_make_command() {
67-
let expected_code = "apt-get update -y
68-
apt-get install libc6-dev libssl-dev make build-essential libssl-dev libreadline6-dev zlib1g-dev libyaml-dev libz-dev -y
73+
let expected_code = "apt-get update -y
74+
apt-get install libc6-dev libssl-dev make \
75+
build-essential libssl-dev libreadline6-dev zlib1g-dev libyaml-dev \
76+
libz-dev -y
6977
apt-get upgrade -y
7078
apt-get install wget -y
7179
cd /tmp
72-
wget https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz
73-
tar xvzf ruby-2.2.3.tar.gz
80+
wget \
81+
https://cache.ruby-lang.org/pub/ruby/2.2/ruby-2.2.3.tar.gz
82+
tar xvzf \
83+
ruby-2.2.3.tar.gz
7484
cd ruby-2.2.3
7585
./configure --prefix=/usr/local
7686
make
77-
make install
87+
\
88+
make install
7889
cd ..
7990
wget https://rubygems.org/rubygems/rubygems-2.4.8.tgz
80-
tar xvzf rubygems-2.4.8.tgz
91+
\
92+
tar xvzf rubygems-2.4.8.tgz
8193
cd rubygems-2.4.8
8294
ruby setup.rb
83-
gem install jekyll -v '3.0.0.pre.beta9'
95+
gem install \
96+
jekyll -v '3.0.0.pre.beta9'
8497
";
85-
let slice_root_directory = get_slice_root_directory();
86-
let command = MakeCommand { layer: "jekyll".to_string(), os: "debian".to_string(), slice_root_directory: &slice_root_directory };
87-
let returned_code = command.get_code_for_latest_slice().unwrap();
88-
assert_eq!(returned_code, expected_code);
89-
}
98+
let slice_root_directory = get_slice_root_directory();
99+
let command = MakeCommand {
100+
layer: "jekyll".to_string(),
101+
os: "debian".to_string(),
102+
slice_root_directory: &slice_root_directory,
103+
};
104+
let returned_code = command.get_code_for_latest_slice().unwrap();
105+
assert_eq!(returned_code, expected_code);
106+
}

impl/rust/src/commands/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ mod helper;
22
pub mod command;
33
pub mod fetch_command;
44
pub mod find_command;
5-
pub mod make_command;
5+
pub mod make_command;

impl/rust/src/for_testing.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ use std::env::current_dir;
22
use std::path::PathBuf;
33

44
pub fn get_slice_root_directory() -> PathBuf {
5-
let mut slice_root_directory = current_dir().unwrap();
6-
slice_root_directory.push("target");
7-
let build_type = if cfg!(debug_assertions) {
8-
"debug"
9-
} else {
10-
"release"
11-
};
12-
slice_root_directory.push(build_type);
13-
slice_root_directory.push(".sb");
14-
slice_root_directory.push("slices");
15-
slice_root_directory.to_path_buf()
16-
}
5+
let mut slice_root_directory = current_dir().unwrap();
6+
slice_root_directory.push("target");
7+
let build_type = if cfg!(debug_assertions) {
8+
"debug"
9+
} else {
10+
"release"
11+
};
12+
slice_root_directory.push(build_type);
13+
slice_root_directory.push(".sb");
14+
slice_root_directory.push("slices");
15+
slice_root_directory.to_path_buf()
16+
}

0 commit comments

Comments
 (0)