From c0260cd0bed242b23ba36c8679b3cadfa223a023 Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Mon, 8 Jan 2018 05:46:12 +0100 Subject: [PATCH 01/11] Various refactorings and updates. * NEW: Updated dependencies * NEW: Added basic integration test and fixture * NEW: Separated the main application's logic (zeps-core) to a library project (zeps) * NEW: Added config::read_world() method. * NEW: Added data::World::stations() and data::Network::stations() methods, which return optimized iterators. * BUG: Coordinates are now signed integers --- .gitignore | 3 ++ Cargo.lock | 56 +++++++++++++++++------------------ Cargo.toml | 8 +++++ src/config.rs | 11 +++++-- src/data/coordinates.rs | 51 +++++++++++++++++++++++++------- src/data/mod.rs | 15 ++++++---- src/data/network.rs | 22 +++++++++++++- src/data/station.rs | 1 + src/data/world.rs | 35 ++++++++++++++++++++++ src/lib.rs | 8 +++++ src/main.rs | 15 ++++------ tests/config.rs | 8 +++++ tests/fixtures/example.json | 58 +++++++++++++++++++++++++++++++++++++ 13 files changed, 234 insertions(+), 57 deletions(-) create mode 100644 src/lib.rs create mode 100644 tests/config.rs create mode 100644 tests/fixtures/example.json diff --git a/.gitignore b/.gitignore index 324c57f..9941cf0 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ target/ **/*.rs.bk + +/*.iml +.idea diff --git a/Cargo.lock b/Cargo.lock index d111665..ec18589 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,25 +1,16 @@ -[root] -name = "zeps-core" -version = "0.1.0" -dependencies = [ - "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", -] - [[package]] name = "dtoa" -version = "0.4.1" +version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "itoa" -version = "0.3.1" +version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "num-traits" -version = "0.1.39" +version = "0.1.41" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -29,22 +20,22 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde" -version = "1.0.8" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "serde_derive" -version = "1.0.8" +version = "1.0.27" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", - "serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)", "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive_internals" -version = "0.15.1" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", @@ -53,13 +44,13 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.2" +version = "1.0.9" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)", - "itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", - "num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)", + "dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", + "itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", + "num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -85,15 +76,24 @@ name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "zeps-core" +version = "0.1.0" +dependencies = [ + "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] -"checksum dtoa 0.4.1 (registry+https://github.com/rust-lang/crates.io-index)" = "80c8b71fd71146990a9742fc06dcbbde19161a267e0ad4e572c35162f4578c90" -"checksum itoa 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "eb2f404fbc66fd9aac13e998248505e7ecb2ad8e44ab6388684c5fb11c6c251c" -"checksum num-traits 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "1708c0628602a98b52fad936cf3edb9a107af06e52e49fdf0707e884456a6af6" +"checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" +"checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" +"checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" -"checksum serde 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "c2f530d36fb84ec48fb7146936881f026cdbf4892028835fd9398475f82c1bb4" -"checksum serde_derive 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "10552fad5500771f3902d0c5ba187c5881942b811b7ba0d8fbbfbf84d80806d3" -"checksum serde_derive_internals 0.15.1 (registry+https://github.com/rust-lang/crates.io-index)" = "37aee4e0da52d801acfbc0cc219eb1eda7142112339726e427926a6f6ee65d3a" -"checksum serde_json 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "48b04779552e92037212c3615370f6bd57a40ebba7f20e554ff9f55e41a69a7b" +"checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526" +"checksum serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "f4ba7591cfe93755e89eeecdbcc668885624829b020050e6aec99c2a03bd3fd0" +"checksum serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e03f1c9530c3fb0a0a5c9b826bdd9246a5921ae995d75f512ac917fc4dd55b5" +"checksum serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9db7266c7d63a4c4b7fe8719656ccdd51acf1bed6124b174f933b009fb10bcb" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" diff --git a/Cargo.toml b/Cargo.toml index bd341d4..27a0219 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,3 +7,11 @@ authors = ["The zDevelopers"] serde = "1.0.8" serde_json = "1.0.2" serde_derive = "1.0.8" + +[lib] +name = "zeps" +path = "src/lib.rs" + +[[bin]] +name = "zeps-core" +path = "src/main.rs" diff --git a/src/config.rs b/src/config.rs index 54da8e7..57cac24 100644 --- a/src/config.rs +++ b/src/config.rs @@ -4,12 +4,13 @@ use std::error::Error; use std::collections::HashMap; use serde_json; +use std::io::Read; #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(untagged)] pub enum Coordinates { - Simple(u64,u64), - Complete(u64,u64,u64), + Simple(i64,i64), + Complete(i64,i64,i64), } #[derive(Serialize, Deserialize, Debug)] @@ -69,7 +70,11 @@ pub struct World { pub networks_connections: Vec, } +pub fn read_world(reader: R) -> Result> { + Ok(serde_json::from_reader(reader)?) +} + pub fn read_world_from_file>(path: P) -> Result> { - Ok(serde_json::from_reader(File::open(path)?)?) + Ok(read_world(File::open(path)?)?) } diff --git a/src/data/coordinates.rs b/src/data/coordinates.rs index a4df861..2dc6151 100644 --- a/src/data/coordinates.rs +++ b/src/data/coordinates.rs @@ -1,27 +1,58 @@ #[derive(Copy, Clone, Default, Debug)] pub struct Coordinates { - x: u64, - y: u64, - z: u64 + x: i64, + y: i64, + z: i64 } impl Coordinates { - pub fn new(x: u64, y: u64, z: u64) -> Coordinates { + pub fn new(x: i64, y: i64, z: i64) -> Coordinates { Coordinates { - x: x, - y: y, - z: z, + x, + y, + z, } } - pub fn new_2d(x: u64, z: u64) -> Coordinates { + pub fn new_2d(x: i64, z: i64) -> Coordinates { Coordinates { - x: x, + x, y: 0, - z: z, + z, } } + + pub fn x(&self) -> i64 { + self.x + } + + pub fn y(&self) -> i64 { + self.y + } + + pub fn z(&self) -> i64 { + self.z + } + + /// Calculates the distance between this point and another. + /// + /// # Example + /// + /// ``` + /// # use zeps::data::Coordinates; + /// let c1 = Coordinates::new(0, 0, 0); + /// let c2 = Coordinates::new(3, 4, 0); + /// + /// assert_eq!(c1.distance(&c2), 5.0); + /// ``` + pub fn distance(&self, other: &Coordinates) -> f64 { + let x = self.x - other.x; + let y = self.y - other.y; + let z = self.z - other.z; + + ((x * x + y * y + z * z) as f64).sqrt() + } } diff --git a/src/data/mod.rs b/src/data/mod.rs index b551f0a..3d9f982 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -1,7 +1,10 @@ -mod coordinates; -mod line; -mod network; -mod network_connection; -mod station; -mod world; +pub mod coordinates; +pub mod line; +pub mod network; +pub mod network_connection; +pub mod station; +pub mod world; + +pub use self::coordinates::Coordinates; +pub use self::network::Network; \ No newline at end of file diff --git a/src/data/network.rs b/src/data/network.rs index 1a3fa14..3d7fec1 100644 --- a/src/data/network.rs +++ b/src/data/network.rs @@ -1,6 +1,7 @@ use data::station::Station; use std::collections::HashMap; +use std::collections::hash_map::Values; pub struct Network { minecraft_world_name: String, @@ -10,13 +11,31 @@ pub struct Network { stations: HashMap, } +impl Network { + pub fn stations(&self) -> Stations { + Stations { iter: self.stations.values() } + } +} + +pub struct Stations<'a> { + iter: Values<'a, String, Station> +} + +impl<'a> Iterator for Stations<'a> { + type Item = &'a Station; + + fn next(&mut self) -> Option { + self.iter.next() + } +} + impl From<::config::Network> for Network { fn from(network: ::config::Network) -> Network { let stations = network.stations.into_iter().map(|(k,v)| (k, v.into())).collect(); Network { minecraft_world_name: network.world, display_name: network.name, - stations: stations, + stations, coordinates_factor: network.coordinates_factor .or(network.is_nether.map(|is_nether| if is_nether { 0.125 } else { 1.0 })) @@ -24,3 +43,4 @@ impl From<::config::Network> for Network { } } } + diff --git a/src/data/station.rs b/src/data/station.rs index 1d19361..5fcce6b 100644 --- a/src/data/station.rs +++ b/src/data/station.rs @@ -1,5 +1,6 @@ use data::coordinates::Coordinates; +#[derive(Debug)] pub struct Station { name: String, subname: String, diff --git a/src/data/world.rs b/src/data/world.rs index ef78a10..d858c39 100644 --- a/src/data/world.rs +++ b/src/data/world.rs @@ -1,6 +1,9 @@ use data::network::Network; use std::collections::HashMap; +use std::collections::hash_map::Values; +use std::iter::FlatMap; +use data::station::Station; pub struct World { display_name: String, @@ -8,6 +11,38 @@ pub struct World { network_connections: HashMap, } +impl World { + pub fn stations(&self) -> Stations { + Stations { + iter: self.networks.values(), + inner_iter: None, + } + } +} + +pub struct Stations<'a> { + iter: Values<'a, String, Network>, + inner_iter: Option<::data::network::Stations<'a>>, +} + +impl<'a> Iterator for Stations<'a> { + type Item = &'a Station; + + fn next(&mut self) -> Option { + loop { + if let Some(ref mut inner) = self.inner_iter { + if let Some(x) = inner.by_ref().next() { + return Some(x) + } + } + match self.iter.next().map(Network::stations) { + None => return None, + next => self.inner_iter = next, + } + } + } +} + impl From<::config::World> for World { fn from(world: ::config::World) -> World { World { diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..4db765e --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,8 @@ +extern crate serde; +extern crate serde_json; + +#[macro_use] +extern crate serde_derive; + +pub mod config; +pub mod data; \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 1ac86c3..33f7032 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,10 @@ -extern crate serde; -extern crate serde_json; +extern crate zeps; -#[macro_use] -extern crate serde_derive; - -mod config; -mod data; +use zeps::data::world::World; fn main() { - let u = config::read_world_from_file("../vessinque.json").unwrap(); - println!("World: {:#?}", u); + let u: World = zeps::config::read_world_from_file("./tests/fixtures/example.json").unwrap().into(); + //println!("World: {:#?}", u); + let s: Vec<_> = u.stations().collect(); + println!("Stations: {:#?}", s); } diff --git a/tests/config.rs b/tests/config.rs new file mode 100644 index 0000000..e909dde --- /dev/null +++ b/tests/config.rs @@ -0,0 +1,8 @@ +extern crate zeps; + +const EXAMPLE_CONFIG: &[u8] = include_bytes!("fixtures/example.json"); + +#[test] +fn it_loads() { + let _ = zeps::config::read_world(EXAMPLE_CONFIG).unwrap(); +} \ No newline at end of file diff --git a/tests/fixtures/example.json b/tests/fixtures/example.json new file mode 100644 index 0000000..4a416c4 --- /dev/null +++ b/tests/fixtures/example.json @@ -0,0 +1,58 @@ +{ + "name": "Vessinque", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "v5_nether", + + "is_nether": true, + + "coordinates_factor": 0.125, + + "stations": { + "station_code_name": { + "name": "Displayed station name", + "subname": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, + + "lines": [ + { + "name": "(can be left empty or removed)", + "color": "#rrggbb", + "path": [ + "station_1", + "station_2", + { + "station": "station_3", + "path_type": "walk", + "path_secure": false, + "path_to_next": [[0, 0,0], [0, 0], [0, 0, 0]] + }, + "station_4" + ] + } + ] + } + }, + "networks_connections": [ + { + "networks": ["netherrail", "overworld"], + "autoconnect_by_name": true, + "autoconnect_by_proximity": 10, + "connections": [ + ["station_in_the_netherrail", "station_in_the_overworld"], + ["station_in_the_netherrail", "station_in_the_overworld"], + ["station_in_the_netherrail", "station_in_the_overworld"] + ] + } + ] +} From 4c831bc9e871924a0523ad552fbeb7c3507b8212 Mon Sep 17 00:00:00 2001 From: Adrien Prokopowicz Date: Wed, 10 Jan 2018 12:41:10 +0100 Subject: [PATCH 02/11] Added failure, and renamed World to Universe * NEW: Renamed the World structures to Universe * NEW: Add some public methods so the compiler won't complain about unused fields * NEW: Added error management (using the failure crate) * NEW: Added configuration errors * NEW: Added missing NetworkConnection structure * NEW: Added Station.is_hidden field --- Cargo.lock | 102 +++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/config.rs | 20 +++---- src/data/error.rs | 6 ++ src/data/line.rs | 34 +++++++++++ src/data/mod.rs | 7 ++- src/data/network.rs | 13 +++++ src/data/network_connection.rs | 32 ++++++++++- src/data/station.rs | 2 + src/data/universe.rs | 78 +++++++++++++++++++++++++ src/data/world.rs | 55 ------------------ src/lib.rs | 25 +++++++- src/main.rs | 34 ++++++++++- tests/config.rs | 4 +- 14 files changed, 336 insertions(+), 78 deletions(-) create mode 100644 src/data/error.rs create mode 100644 src/data/universe.rs delete mode 100644 src/data/world.rs diff --git a/Cargo.lock b/Cargo.lock index ec18589..a842230 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,13 +1,68 @@ +[[package]] +name = "backtrace" +version = "0.3.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)", + "cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.35 (registry+https://github.com/rust-lang/crates.io-index)", + "rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "backtrace-sys" +version = "0.1.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "cc 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.35 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "cc" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "cfg-if" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "dtoa" version = "0.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "failure" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "backtrace 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "failure_derive" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", + "synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "itoa" version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "libc" +version = "0.2.35" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "num-traits" version = "0.1.41" @@ -18,6 +73,11 @@ name = "quote" version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rustc-demangle" +version = "0.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "serde" version = "1.0.27" @@ -71,29 +131,71 @@ dependencies = [ "unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "synstructure" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "zeps-core" version = "0.1.0" dependencies = [ + "failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", + "failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] +"checksum backtrace 0.3.5 (registry+https://github.com/rust-lang/crates.io-index)" = "ebbbf59b1c43eefa8c3ede390fcc36820b4999f7914104015be25025e0d62af2" +"checksum backtrace-sys 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "44585761d6161b0f57afc49482ab6bd067e4edef48c12a152c237eb0203f7661" +"checksum cc 1.0.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a9b13a57efd6b30ecd6598ebdb302cca617930b5470647570468a65d12ef9719" +"checksum cfg-if 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "d4c819a1287eb618df47cc647173c5c4c66ba19d888a6e50d605672aed3140de" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" +"checksum failure 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "934799b6c1de475a012a02dab0ace1ace43789ee4b99bcfbf1a2e3e8ced5de82" +"checksum failure_derive 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "c7cdda555bb90c9bb67a3b670a0f42de8e73f5981524123ad8578aafec8ddb8b" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" +"checksum libc 0.2.35 (registry+https://github.com/rust-lang/crates.io-index)" = "96264e9b293e95d25bfcbbf8a88ffd1aedc85b754eba8b7d78012f638ba220eb" "checksum num-traits 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)" = "cacfcab5eb48250ee7d0c7896b51a2c5eec99c1feea5f32025635f5ae4b00070" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" +"checksum rustc-demangle 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "aee45432acc62f7b9a108cc054142dac51f979e69e71ddce7d6fc7adf29e817e" "checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526" "checksum serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "f4ba7591cfe93755e89eeecdbcc668885624829b020050e6aec99c2a03bd3fd0" "checksum serde_derive_internals 0.19.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e03f1c9530c3fb0a0a5c9b826bdd9246a5921ae995d75f512ac917fc4dd55b5" "checksum serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)" = "c9db7266c7d63a4c4b7fe8719656ccdd51acf1bed6124b174f933b009fb10bcb" "checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" +"checksum synstructure 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3a761d12e6d8dcb4dcf952a7a89b475e3a9d69e4a69307e01a470977642914bd" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" +"checksum winapi 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "b09fb3b6f248ea4cd42c9a65113a847d612e17505d6ebd1f7357ad68a8bf8693" +"checksum winapi-i686-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ec6667f60c23eca65c561e63a13d81b44234c2e38a6b6c959025ee907ec614cc" +"checksum winapi-x86_64-pc-windows-gnu 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98f12c52b2630cd05d2c3ffd8e008f7f48252c042b4871c72aed9dc733b96668" diff --git a/Cargo.toml b/Cargo.toml index 27a0219..ff7ed66 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ authors = ["The zDevelopers"] serde = "1.0.8" serde_json = "1.0.2" serde_derive = "1.0.8" +failure = "0.1.1" +failure_derive = "0.1.1" [lib] name = "zeps" diff --git a/src/config.rs b/src/config.rs index 57cac24..0289223 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,10 +1,10 @@ -use std::path::Path; -use std::fs::File; -use std::error::Error; use std::collections::HashMap; +use std::io::Read; + +use failure::Error; +use failure::ResultExt; use serde_json; -use std::io::Read; #[derive(Serialize, Deserialize, Debug, Clone)] #[serde(untagged)] @@ -43,6 +43,7 @@ pub struct Station { pub is_main: bool, pub has_fast_access: bool, pub fast_access_time: f64, + pub is_hidden: Option, } #[derive(Serialize, Deserialize, Debug)] @@ -64,17 +65,12 @@ pub struct NetworkConnection { } #[derive(Serialize, Deserialize, Debug)] -pub struct World { +pub struct Universe { pub name: String, pub networks: HashMap, pub networks_connections: Vec, } -pub fn read_world(reader: R) -> Result> { - Ok(serde_json::from_reader(reader)?) +pub fn read_universe(reader: R) -> Result { + Ok(serde_json::from_reader(reader).context("Unable to parse universe data")?) } - -pub fn read_world_from_file>(path: P) -> Result> { - Ok(read_world(File::open(path)?)?) -} - diff --git a/src/data/error.rs b/src/data/error.rs new file mode 100644 index 0000000..c491b19 --- /dev/null +++ b/src/data/error.rs @@ -0,0 +1,6 @@ + +#[derive(Debug, Fail)] +pub enum InvalidConfigurationError { + #[fail(display = "Network '{}' does not exist", name)] + NetworkNotFound { name: String } +} diff --git a/src/data/line.rs b/src/data/line.rs index e37b324..0cb7f1e 100644 --- a/src/data/line.rs +++ b/src/data/line.rs @@ -2,11 +2,13 @@ use data::coordinates::Coordinates; use data::station::Station; use std::rc::Rc; +#[derive(Debug, Copy, Clone)] pub enum PathType { Rail, Walk } +#[derive(Debug, Clone)] pub struct LinePoint { station: Rc, path_type: PathType, @@ -14,9 +16,41 @@ pub struct LinePoint { path_to_next: Vec, } +impl LinePoint { + pub fn station(&self) -> &Station { + &self.station + } + + pub fn path_type(&self) -> PathType { + self.path_type + } + + pub fn path_secure(&self) -> bool { + self.path_secure + } + + pub fn path_to_next(&self) -> &[Coordinates] { + &self.path_to_next + } +} + +#[derive(Debug, Clone)] pub struct Line { name: String, color: String, path: Vec, } +impl Line { + pub fn name(&self) -> &str { + &self.name + } + + pub fn color(&self) -> &str { + &self.color + } + + pub fn path(&self) -> &[LinePoint] { + &self.path + } +} \ No newline at end of file diff --git a/src/data/mod.rs b/src/data/mod.rs index 3d9f982..128059a 100644 --- a/src/data/mod.rs +++ b/src/data/mod.rs @@ -4,7 +4,10 @@ pub mod line; pub mod network; pub mod network_connection; pub mod station; -pub mod world; +pub mod universe; +pub mod error; pub use self::coordinates::Coordinates; -pub use self::network::Network; \ No newline at end of file +pub use self::network::Network; +pub use self::universe::Universe; +pub use self::station::Station; diff --git a/src/data/network.rs b/src/data/network.rs index 3d7fec1..458bddc 100644 --- a/src/data/network.rs +++ b/src/data/network.rs @@ -3,6 +3,7 @@ use data::station::Station; use std::collections::HashMap; use std::collections::hash_map::Values; +#[derive(Debug)] pub struct Network { minecraft_world_name: String, display_name: String, @@ -15,6 +16,18 @@ impl Network { pub fn stations(&self) -> Stations { Stations { iter: self.stations.values() } } + + pub fn display_name(&self) -> &str { + &self.display_name + } + + pub fn world_name(&self) -> &str { + &self.minecraft_world_name + } + + pub fn coordinates_factor(&self) -> f64 { + self.coordinates_factor + } } pub struct Stations<'a> { diff --git a/src/data/network_connection.rs b/src/data/network_connection.rs index 8d1c8b6..4aee62a 100644 --- a/src/data/network_connection.rs +++ b/src/data/network_connection.rs @@ -1 +1,31 @@ - +use std::collections::HashMap; +use std::rc::Rc; + +use data::network::Network; +use data::error::InvalidConfigurationError; + +#[derive(Debug)] +pub struct NetworkConnection { + network_1: Rc, + network_2: Rc, + autoconnect_by_name: bool, + autoconnect_by_proximity: u64, + //connections: Vec<(String,String)>, +} + +impl NetworkConnection { + pub fn from_config(connection: ::config::NetworkConnection, networks_map: &HashMap>) -> Result { + let network_1 = networks_map.get(&connection.networks.0).cloned() + .ok_or(InvalidConfigurationError::NetworkNotFound {name: connection.networks.0})?; + let network_2 = networks_map.get(&connection.networks.1).cloned() + .ok_or(InvalidConfigurationError::NetworkNotFound {name: connection.networks.1})?; + + Ok(NetworkConnection { + network_1, + network_2, + + autoconnect_by_name: connection.autoconnect_by_name.unwrap_or(false), + autoconnect_by_proximity: connection.autoconnect_by_proximity.unwrap_or(0), + }) + } +} diff --git a/src/data/station.rs b/src/data/station.rs index 5fcce6b..8f6dc2a 100644 --- a/src/data/station.rs +++ b/src/data/station.rs @@ -10,6 +10,7 @@ pub struct Station { is_main: bool, has_fast_access: bool, fast_access_time: f64, + is_hidden: bool, } impl From<::config::Station> for Station { @@ -23,6 +24,7 @@ impl From<::config::Station> for Station { is_main: station.is_main, has_fast_access: station.has_fast_access, fast_access_time: station.fast_access_time, + is_hidden: station.is_hidden.unwrap_or(false) } } } diff --git a/src/data/universe.rs b/src/data/universe.rs new file mode 100644 index 0000000..585a8f5 --- /dev/null +++ b/src/data/universe.rs @@ -0,0 +1,78 @@ +use std::collections::HashMap; +use std::collections::hash_map::Values; +use std::rc::Rc; + +use data::station::Station; +use data::network::Network; +use data::network_connection::NetworkConnection; +use data::error::InvalidConfigurationError; + +pub struct Universe { + display_name: String, + networks: HashMap>, + network_connections: Vec, +} + +impl Universe { + pub fn stations(&self) -> Stations { + Stations { + iter: self.networks.values(), + inner_iter: None, + } + } + + pub fn display_name(&self) -> &str { + &self.display_name + } + + pub fn get_network(&self, connection_name: &str) -> Option<&Network> { + self.networks.get(connection_name).map(|x| x.as_ref()) + } + + pub fn get_network_handle(&self, connection_name: &str) -> Option> { + self.networks.get(connection_name).cloned() + } + + pub fn get_connections(&self) -> &[NetworkConnection] { + &self.network_connections + } + + pub fn from_config(universe: ::config::Universe) -> Result { + let networks = universe.networks.into_iter() + .map(|(k,v)| (k, Rc::new(v.into()))) + .collect(); + + let network_connections: Result<_, _> = universe.networks_connections.into_iter() + .map(|c| NetworkConnection::from_config(c, &networks)).collect(); + + Ok(Universe { + display_name: universe.name, + networks, + network_connections: network_connections?, + }) + + } +} + +pub struct Stations<'a> { + iter: Values<'a, String, Rc>, + inner_iter: Option<::data::network::Stations<'a>>, +} + +impl<'a> Iterator for Stations<'a> { + type Item = &'a Station; + + fn next(&mut self) -> Option { + loop { + if let Some(ref mut inner) = self.inner_iter { + if let Some(x) = inner.by_ref().next() { + return Some(x) + } + } + match self.iter.next().map(|x| x.as_ref()).map(Network::stations) { + None => return None, + next => self.inner_iter = next, + } + } + } +} diff --git a/src/data/world.rs b/src/data/world.rs deleted file mode 100644 index d858c39..0000000 --- a/src/data/world.rs +++ /dev/null @@ -1,55 +0,0 @@ -use data::network::Network; - -use std::collections::HashMap; -use std::collections::hash_map::Values; -use std::iter::FlatMap; -use data::station::Station; - -pub struct World { - display_name: String, - networks: HashMap, - network_connections: HashMap, -} - -impl World { - pub fn stations(&self) -> Stations { - Stations { - iter: self.networks.values(), - inner_iter: None, - } - } -} - -pub struct Stations<'a> { - iter: Values<'a, String, Network>, - inner_iter: Option<::data::network::Stations<'a>>, -} - -impl<'a> Iterator for Stations<'a> { - type Item = &'a Station; - - fn next(&mut self) -> Option { - loop { - if let Some(ref mut inner) = self.inner_iter { - if let Some(x) = inner.by_ref().next() { - return Some(x) - } - } - match self.iter.next().map(Network::stations) { - None => return None, - next => self.inner_iter = next, - } - } - } -} - -impl From<::config::World> for World { - fn from(world: ::config::World) -> World { - World { - display_name: world.name, - networks: world.networks.into_iter().map(|(k,v)| (k, v.into())).collect(), - network_connections: HashMap::new(), - } - } -} - diff --git a/src/lib.rs b/src/lib.rs index 4db765e..ea274f2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,8 +1,27 @@ extern crate serde; extern crate serde_json; +extern crate failure; -#[macro_use] -extern crate serde_derive; +#[macro_use] extern crate serde_derive; +#[macro_use] extern crate failure_derive; pub mod config; -pub mod data; \ No newline at end of file +pub mod data; + +pub use data::Universe; + +use std::fs::File; +use std::io::Read; +use std::path::Path; + +use failure::Error; +use failure::ResultExt; + +pub fn read_universe(reader: R) -> Result { + Ok(Universe::from_config(config::read_universe(reader)?)?) +} + +pub fn read_universe_from_file>(path: P) -> Result { + let file = File::open(&path).with_context(|_| format!("Unable to open universe data file '{}'", path.as_ref().to_string_lossy()))?; + Ok(read_universe(file).with_context(|_| format!("Unable to load universe data file '{}'", path.as_ref().to_string_lossy()))?) +} diff --git a/src/main.rs b/src/main.rs index 33f7032..f90d40c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,10 +1,38 @@ extern crate zeps; +extern crate failure; + +use std::io::Write; + +use zeps::data::universe::Universe; +use failure::Error; + +fn print_error(e: &Error) { + let seriously_dude = "Unable to write to standard error output (stderr) !"; + let stderr = &mut ::std::io::stderr(); + let mut stderr = stderr.lock(); + + writeln!(stderr, " [!] Error: {}", e).expect(seriously_dude); + + for cause in e.causes().skip(1) { + writeln!(stderr, " | Cause: \t{}", cause).expect(seriously_dude); + } + + if cfg!(debug_assertions) { + writeln!(stderr, "\n\n [?] Backtrace: {:#?}", e.backtrace()).expect(seriously_dude); + } +} -use zeps::data::world::World; fn main() { - let u: World = zeps::config::read_world_from_file("./tests/fixtures/example.json").unwrap().into(); - //println!("World: {:#?}", u); + if let Err(ref e) = run() { + print_error(e); + ::std::process::exit(1); + } +} + +fn run() -> Result<(), Error> { + let u: Universe = zeps::read_universe_from_file("./tests/fixtures/example.json")?; let s: Vec<_> = u.stations().collect(); println!("Stations: {:#?}", s); + Ok(()) } diff --git a/tests/config.rs b/tests/config.rs index e909dde..8d20c91 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -4,5 +4,5 @@ const EXAMPLE_CONFIG: &[u8] = include_bytes!("fixtures/example.json"); #[test] fn it_loads() { - let _ = zeps::config::read_world(EXAMPLE_CONFIG).unwrap(); -} \ No newline at end of file + let _ = zeps::config::read_universe(EXAMPLE_CONFIG).unwrap(); +} From e6b17517645a2f714669856fe9eed7df33294cf7 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 12:34:46 +0100 Subject: [PATCH 03/11] Added lines import and changed network structure. * NEW: lines are now imported and validated (e.g. for valid stations in them). * NEW: instead of a HashMap, stations of a network are now in a HashMap>. * NEW: updated fixture to validate. --- src/data/error.rs | 8 ++++++- src/data/line.rs | 46 ++++++++++++++++++++++++++++++++++++- src/data/network.rs | 42 ++++++++++++++++++--------------- src/data/universe.rs | 21 +++++++++++------ tests/fixtures/example.json | 40 ++++++++++++++++++++++++++++---- 5 files changed, 125 insertions(+), 32 deletions(-) diff --git a/src/data/error.rs b/src/data/error.rs index c491b19..570f2b2 100644 --- a/src/data/error.rs +++ b/src/data/error.rs @@ -2,5 +2,11 @@ #[derive(Debug, Fail)] pub enum InvalidConfigurationError { #[fail(display = "Network '{}' does not exist", name)] - NetworkNotFound { name: String } + NetworkNotFound { name: String }, + + #[fail(display= "Station '{}' does not exist", name)] + StationNotFound { name: String }, + + #[fail(display= "Path type '{}' is not recognized", name)] + InvalidPathType { name: String } } diff --git a/src/data/line.rs b/src/data/line.rs index 0cb7f1e..8a6c1e7 100644 --- a/src/data/line.rs +++ b/src/data/line.rs @@ -1,6 +1,9 @@ use data::coordinates::Coordinates; +use data::error::InvalidConfigurationError; use data::station::Station; + use std::rc::Rc; +use std::collections::HashMap; #[derive(Debug, Copy, Clone)] pub enum PathType { @@ -8,6 +11,17 @@ pub enum PathType { Walk } +impl PathType { + fn from_config(path_type: Option) -> Result { + Ok(match path_type.as_ref().map(|s| &s[..]) { + Some("rail") => PathType::Rail, + Some("walk") => PathType::Walk, + Some(other) => Err(InvalidConfigurationError::InvalidPathType { name: other.to_string() })?, + None => PathType::Rail + }) + } +} + #[derive(Debug, Clone)] pub struct LinePoint { station: Rc, @@ -32,6 +46,26 @@ impl LinePoint { pub fn path_to_next(&self) -> &[Coordinates] { &self.path_to_next } + + pub fn from_config(line_point: ::config::LinePoint, stations: &HashMap>) -> Result { + Ok(match line_point { + ::config::LinePoint::Simple(station) => LinePoint { + station: stations.get(&station).cloned().ok_or(InvalidConfigurationError::StationNotFound { name: station })?, + path_type: PathType::Rail, + path_secure: true, + path_to_next: Vec::new(), + }, + ::config::LinePoint::WithMetadata { station, path_type, path_secure, path_to_next } => LinePoint { + station: stations.get(&station).cloned().ok_or(InvalidConfigurationError::StationNotFound { name: station })?, + path_type: PathType::from_config(path_type)?, + path_secure: path_secure.unwrap_or(true), + path_to_next: match path_to_next { + Some(vec_coordinates) => vec_coordinates.into_iter().map(|c| c.into()).collect(), + None => Vec::new() + } + } + }) + } } #[derive(Debug, Clone)] @@ -53,4 +87,14 @@ impl Line { pub fn path(&self) -> &[LinePoint] { &self.path } -} \ No newline at end of file + + pub fn from_config(line: ::config::Line, stations: &HashMap>) -> Result { + let path = line.path.into_iter().map(|p| LinePoint::from_config(p, stations)).collect::>()?; + + Ok(Line { + name: line.name, + color: line.color, + path + }) + } +} diff --git a/src/data/network.rs b/src/data/network.rs index 458bddc..ecfe867 100644 --- a/src/data/network.rs +++ b/src/data/network.rs @@ -1,7 +1,10 @@ use data::station::Station; +use data::line::Line; +use data::error::InvalidConfigurationError; use std::collections::HashMap; use std::collections::hash_map::Values; +use std::rc::Rc; #[derive(Debug)] pub struct Network { @@ -9,7 +12,9 @@ pub struct Network { display_name: String, coordinates_factor: f64, - stations: HashMap, + stations: HashMap>, + + lines: Vec } impl Network { @@ -28,32 +33,33 @@ impl Network { pub fn coordinates_factor(&self) -> f64 { self.coordinates_factor } -} - -pub struct Stations<'a> { - iter: Values<'a, String, Station> -} -impl<'a> Iterator for Stations<'a> { - type Item = &'a Station; - - fn next(&mut self) -> Option { - self.iter.next() - } -} + pub fn from_config(network: ::config::Network) -> Result { + let stations = network.stations.into_iter().map(|(k, v)| (k, Rc::new(v.into()))).collect(); + let lines = network.lines.into_iter().map(|l| Line::from_config(l, &stations)).collect::>()?; -impl From<::config::Network> for Network { - fn from(network: ::config::Network) -> Network { - let stations = network.stations.into_iter().map(|(k,v)| (k, v.into())).collect(); - Network { + Ok(Network { minecraft_world_name: network.world, display_name: network.name, + stations, + lines, coordinates_factor: network.coordinates_factor .or(network.is_nether.map(|is_nether| if is_nether { 0.125 } else { 1.0 })) .unwrap_or(1.0) - } + }) } } +pub struct Stations<'a> { + iter: Values<'a, String, Rc> +} + +impl<'a> Iterator for Stations<'a> { + type Item = &'a Rc; + + fn next(&mut self) -> Option { + self.iter.next() + } +} diff --git a/src/data/universe.rs b/src/data/universe.rs index 585a8f5..b57ca94 100644 --- a/src/data/universe.rs +++ b/src/data/universe.rs @@ -38,17 +38,24 @@ impl Universe { } pub fn from_config(universe: ::config::Universe) -> Result { - let networks = universe.networks.into_iter() - .map(|(k,v)| (k, Rc::new(v.into()))) - .collect(); + let mut networks: HashMap> = HashMap::new(); - let network_connections: Result<_, _> = universe.networks_connections.into_iter() - .map(|c| NetworkConnection::from_config(c, &networks)).collect(); + for network in universe.networks.into_iter().map(|(k, v)| (k, Network::from_config(v))) { + match network.1 { + Ok(n) => networks.insert(network.0, Rc::new(n)), + Err(e) => return Err(e) + }; + } + + let networks = networks; + + let network_connections = universe.networks_connections.into_iter() + .map(|c| NetworkConnection::from_config(c, &networks)).collect::>()?; Ok(Universe { display_name: universe.name, networks, - network_connections: network_connections?, + network_connections, }) } @@ -60,7 +67,7 @@ pub struct Stations<'a> { } impl<'a> Iterator for Stations<'a> { - type Item = &'a Station; + type Item = &'a Rc; fn next(&mut self) -> Option { loop { diff --git a/tests/fixtures/example.json b/tests/fixtures/example.json index 4a416c4..44c0f4a 100644 --- a/tests/fixtures/example.json +++ b/tests/fixtures/example.json @@ -19,6 +19,30 @@ "is_main": true, + "has_fast_access": true, + "fast_access_time": 10 + }, + "station_code_name_2": { + "name": "Displayed station name", + "subname": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + }, + "station_code_name_3": { + "name": "Displayed station name", + "subname": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + "has_fast_access": true, "fast_access_time": 10 } @@ -29,18 +53,24 @@ "name": "(can be left empty or removed)", "color": "#rrggbb", "path": [ - "station_1", - "station_2", + "station_code_name", + "station_code_name_2", { - "station": "station_3", + "station": "station_code_name_3", "path_type": "walk", "path_secure": false, "path_to_next": [[0, 0,0], [0, 0], [0, 0, 0]] - }, - "station_4" + } ] } ] + }, + + "overworld": { + "name": "Surface", + "world": "V5", + "stations": {}, + "lines": [] } }, "networks_connections": [ From 567c53c8b09b440a5b9799cf2070dcc7f5579089 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 12:52:25 +0100 Subject: [PATCH 04/11] Updated JSON structure: subname -> sub_name. * NEW: the `subname` key in the stations objects is now `sub_name`. --- src/config.rs | 2 +- src/data/station.rs | 4 ++-- tests/fixtures/example.json | 6 +++--- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/config.rs b/src/config.rs index 0289223..3df06d0 100644 --- a/src/config.rs +++ b/src/config.rs @@ -35,7 +35,7 @@ pub struct Line { #[derive(Serialize, Deserialize, Debug)] pub struct Station { pub name: String, - pub subname: String, + pub sub_name: String, pub description: String, pub coordinates: Coordinates, pub real_coordinates: Option, diff --git a/src/data/station.rs b/src/data/station.rs index 8f6dc2a..1298254 100644 --- a/src/data/station.rs +++ b/src/data/station.rs @@ -3,7 +3,7 @@ use data::coordinates::Coordinates; #[derive(Debug)] pub struct Station { name: String, - subname: String, + sub_name: String, description: String, coordinates: Coordinates, real_coordinates: Coordinates, @@ -17,7 +17,7 @@ impl From<::config::Station> for Station { fn from(station: ::config::Station) -> Station { Station { name: station.name, - subname: station.subname, + sub_name: station.sub_name, description: station.description, coordinates: station.coordinates.clone().into(), real_coordinates: station.real_coordinates.unwrap_or(station.coordinates).into(), diff --git a/tests/fixtures/example.json b/tests/fixtures/example.json index 44c0f4a..10da946 100644 --- a/tests/fixtures/example.json +++ b/tests/fixtures/example.json @@ -12,7 +12,7 @@ "stations": { "station_code_name": { "name": "Displayed station name", - "subname": "For stations with a main name and another pomf", + "sub_name": "For stations with a main name and another pomf", "description": "If needed, displayed in the map balloon. Can be omitted.", "coordinates": [0, 0, 0], "real_coordinates": [0, 0, 0], @@ -24,7 +24,7 @@ }, "station_code_name_2": { "name": "Displayed station name", - "subname": "For stations with a main name and another pomf", + "sub_name": "For stations with a main name and another pomf", "description": "If needed, displayed in the map balloon. Can be omitted.", "coordinates": [0, 0, 0], "real_coordinates": [0, 0, 0], @@ -36,7 +36,7 @@ }, "station_code_name_3": { "name": "Displayed station name", - "subname": "For stations with a main name and another pomf", + "sub_name": "For stations with a main name and another pomf", "description": "If needed, displayed in the map balloon. Can be omitted.", "coordinates": [0, 0, 0], "real_coordinates": [0, 0, 0], From 5c5e204872928f7f55351f93f761737a74d08589 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 13:38:03 +0100 Subject: [PATCH 05/11] Networks connections are loaded and checked. * NEW: network explicit connections (in networks_connections.connections) are loaded and the stations are checked in their respective networks. What could be improved: - error message on unknown station, to specify the context and the network. --- src/data/network.rs | 4 ++++ src/data/network_connection.rs | 18 +++++++++++++++++- tests/fixtures/example.json | 32 ++++++++++++++++++++++++++++---- 3 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/data/network.rs b/src/data/network.rs index ecfe867..830bd8a 100644 --- a/src/data/network.rs +++ b/src/data/network.rs @@ -22,6 +22,10 @@ impl Network { Stations { iter: self.stations.values() } } + pub fn station(&self, station_code: &String) -> Option> { + self.stations.get(station_code).cloned() + } + pub fn display_name(&self) -> &str { &self.display_name } diff --git a/src/data/network_connection.rs b/src/data/network_connection.rs index 4aee62a..f4e2f65 100644 --- a/src/data/network_connection.rs +++ b/src/data/network_connection.rs @@ -10,7 +10,7 @@ pub struct NetworkConnection { network_2: Rc, autoconnect_by_name: bool, autoconnect_by_proximity: u64, - //connections: Vec<(String,String)>, + connections: Vec<(String,String)>, } impl NetworkConnection { @@ -20,12 +20,28 @@ impl NetworkConnection { let network_2 = networks_map.get(&connection.networks.1).cloned() .ok_or(InvalidConfigurationError::NetworkNotFound {name: connection.networks.1})?; + let mut connections: Vec<(String,String)> = Vec::new(); + + if connection.connections.is_some() { + for conn in connection.connections.unwrap() { + // Checks if the references are valid + network_1.station(&conn.0).ok_or(InvalidConfigurationError::StationNotFound { name: conn.0.clone() })?; + network_2.station(&conn.1).ok_or(InvalidConfigurationError::StationNotFound { name: conn.1.clone() })?; + + connections.push((conn.0.clone(), conn.1.clone())); + } + } + + let connections = connections; + Ok(NetworkConnection { network_1, network_2, autoconnect_by_name: connection.autoconnect_by_name.unwrap_or(false), autoconnect_by_proximity: connection.autoconnect_by_proximity.unwrap_or(0), + + connections }) } } diff --git a/tests/fixtures/example.json b/tests/fixtures/example.json index 10da946..2159401 100644 --- a/tests/fixtures/example.json +++ b/tests/fixtures/example.json @@ -69,7 +69,32 @@ "overworld": { "name": "Surface", "world": "V5", - "stations": {}, + "stations": { + "overworld_station": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + }, + "overworld_station_2": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, "lines": [] } }, @@ -79,9 +104,8 @@ "autoconnect_by_name": true, "autoconnect_by_proximity": 10, "connections": [ - ["station_in_the_netherrail", "station_in_the_overworld"], - ["station_in_the_netherrail", "station_in_the_overworld"], - ["station_in_the_netherrail", "station_in_the_overworld"] + ["station_code_name", "overworld_station"], + ["station_code_name_2", "overworld_station_2"] ] } ] From d1fea7622629d5cd1d94314d9fc364c7c4a59953 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 15:16:08 +0100 Subject: [PATCH 06/11] Added new unit tests. * NEW: added tests to check if the universes are correctly loaded and parsed, and to check if errors are correctly handled. --- src/main.rs | 7 +- tests/config.rs | 81 ++++++++++++++++++- .../connections_invalid_ref_network.json | 23 ++++++ .../connections_invalid_ref_station.json | 63 +++++++++++++++ ...f_station_but_exists_in_other_network.json | 75 +++++++++++++++++ .../fixtures/coordinates_factor_default.json | 13 +++ .../coordinates_factor_default_nether.json | 15 ++++ .../coordinates_factor_overridden.json | 16 ++++ tests/fixtures/empty.json | 1 + tests/fixtures/lines_invalid_ref.json | 39 +++++++++ .../fixtures/lines_invalid_ref_expanded.json | 45 +++++++++++ tests/fixtures/no_data.json | 5 ++ .../fixtures/{example.json => with_data.json} | 0 13 files changed, 376 insertions(+), 7 deletions(-) create mode 100644 tests/fixtures/connections_invalid_ref_network.json create mode 100644 tests/fixtures/connections_invalid_ref_station.json create mode 100644 tests/fixtures/connections_invalid_ref_station_but_exists_in_other_network.json create mode 100644 tests/fixtures/coordinates_factor_default.json create mode 100644 tests/fixtures/coordinates_factor_default_nether.json create mode 100644 tests/fixtures/coordinates_factor_overridden.json create mode 100644 tests/fixtures/empty.json create mode 100644 tests/fixtures/lines_invalid_ref.json create mode 100644 tests/fixtures/lines_invalid_ref_expanded.json create mode 100644 tests/fixtures/no_data.json rename tests/fixtures/{example.json => with_data.json} (100%) diff --git a/src/main.rs b/src/main.rs index f90d40c..7488140 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,12 +2,11 @@ extern crate zeps; extern crate failure; use std::io::Write; - -use zeps::data::universe::Universe; use failure::Error; +use zeps::data::universe::Universe; fn print_error(e: &Error) { - let seriously_dude = "Unable to write to standard error output (stderr) !"; + let seriously_dude = "Unable to write to standard error output (stderr)!"; let stderr = &mut ::std::io::stderr(); let mut stderr = stderr.lock(); @@ -31,7 +30,7 @@ fn main() { } fn run() -> Result<(), Error> { - let u: Universe = zeps::read_universe_from_file("./tests/fixtures/example.json")?; + let u: Universe = zeps::read_universe_from_file("./tests/fixtures/with_data.json")?; let s: Vec<_> = u.stations().collect(); println!("Stations: {:#?}", s); Ok(()) diff --git a/tests/config.rs b/tests/config.rs index 8d20c91..49d7d0c 100644 --- a/tests/config.rs +++ b/tests/config.rs @@ -1,8 +1,83 @@ extern crate zeps; +extern crate failure; -const EXAMPLE_CONFIG: &[u8] = include_bytes!("fixtures/example.json"); +use zeps::data::universe::Universe; +use failure::Error; + +fn load_from_fixture(fixture: &str) -> Result { + zeps::read_universe_from_file(format!("./tests/fixtures/{}.json", fixture)) +} + +#[test] +fn it_fails_from_nothing() { + assert!(load_from_fixture("empty").is_err()); +} + +#[test] +fn it_loads_without_data() { + let universe = load_from_fixture("no_data").unwrap(); + assert_eq!(universe.stations().into_iter().fold(0, |acc, _| acc + 1), 0); +} + +#[test] +fn it_loads_with_data() { + let _ = load_from_fixture("with_data").unwrap(); +} + +#[test] +fn stations_are_all_there() { + let universe = load_from_fixture("with_data").unwrap(); + assert_eq!(universe.stations().into_iter().fold(0, |acc, _| acc + 1), 5); + assert_eq!(universe.get_network("netherrail").unwrap().stations().into_iter().fold(0, |acc, _| acc + 1), 3); + assert_eq!(universe.get_network("overworld").unwrap().stations().into_iter().fold(0, |acc, _| acc + 1), 2); +} + + +// TODO for the following reference tests: test if the correct error is returned too. + +#[test] +fn it_fails_if_a_line_references_an_unknown_station() { + assert!(load_from_fixture("lines_invalid_ref").is_err()); +} + +#[test] +fn it_fails_if_a_line_references_an_unknown_station_in_expanded_mode() { + assert!(load_from_fixture("lines_invalid_ref_expanded").is_err()); +} + +#[test] +fn it_fails_if_a_connection_references_an_unknown_network() { + assert!(load_from_fixture("connections_invalid_ref_network").is_err()); +} + +#[test] +fn it_fails_if_a_connection_references_an_unknown_station() { + assert!(load_from_fixture("connections_invalid_ref_station").is_err()); +} + +#[test] +fn it_fails_if_a_connection_references_an_unknown_station_valid_in_the_other_network() { + assert!(load_from_fixture("connections_invalid_ref_station_but_exists_in_other_network").is_err()); +} + + +#[test] +fn the_coordinates_factor_defaults_to_one() { + let universe = load_from_fixture("coordinates_factor_default").unwrap(); + let network = universe.get_network("netherrail").unwrap(); + assert_eq!(network.coordinates_factor(), 1.0); +} + +#[test] +fn the_coordinates_factor_defaults_to_point_125_if_nether() { + let universe = load_from_fixture("coordinates_factor_default_nether").unwrap(); + let network = universe.get_network("netherrail").unwrap(); + assert_eq!(network.coordinates_factor(), 0.125); +} #[test] -fn it_loads() { - let _ = zeps::config::read_universe(EXAMPLE_CONFIG).unwrap(); +fn the_coordinates_factor_overrides_is_nether_if_explicit() { + let universe = load_from_fixture("coordinates_factor_overridden").unwrap(); + let network = universe.get_network("netherrail").unwrap(); + assert_eq!(network.coordinates_factor(), 1.84); } diff --git a/tests/fixtures/connections_invalid_ref_network.json b/tests/fixtures/connections_invalid_ref_network.json new file mode 100644 index 0000000..58e8efd --- /dev/null +++ b/tests/fixtures/connections_invalid_ref_network.json @@ -0,0 +1,23 @@ +{ + "name": "Vessinque", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "v5_nether", + + "is_nether": true, + + "coordinates_factor": 0.125, + + "stations": {}, + "lines": [] + } + }, + "networks_connections": [ + { + "networks": ["netherrail", "overworld"], + "autoconnect_by_name": true, + "autoconnect_by_proximity": 10 + } + ] +} diff --git a/tests/fixtures/connections_invalid_ref_station.json b/tests/fixtures/connections_invalid_ref_station.json new file mode 100644 index 0000000..177cedb --- /dev/null +++ b/tests/fixtures/connections_invalid_ref_station.json @@ -0,0 +1,63 @@ +{ + "name": "Vessinque", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "v5_nether", + + "is_nether": true, + + "coordinates_factor": 0.125, + + "stations": { + "station_code_name": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, + + "lines": [] + }, + + "overworld": { + "name": "Surface", + "world": "V5", + + "stations": { + "overworld_station": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, + + "lines": [] + } + }, + + "networks_connections": [ + { + "networks": ["netherrail", "overworld"], + "autoconnect_by_name": true, + "autoconnect_by_proximity": 10, + "connections": [ + ["station_code_name", "unknown_station"] + ] + } + ] +} diff --git a/tests/fixtures/connections_invalid_ref_station_but_exists_in_other_network.json b/tests/fixtures/connections_invalid_ref_station_but_exists_in_other_network.json new file mode 100644 index 0000000..8572391 --- /dev/null +++ b/tests/fixtures/connections_invalid_ref_station_but_exists_in_other_network.json @@ -0,0 +1,75 @@ +{ + "name": "Vessinque", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "v5_nether", + + "is_nether": true, + + "coordinates_factor": 0.125, + + "stations": { + "station_code_name": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + }, + "station_code_name_2": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, + + "lines": [] + }, + + "overworld": { + "name": "Surface", + "world": "V5", + + "stations": { + "overworld_station": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, + + "lines": [] + } + }, + + "networks_connections": [ + { + "networks": ["netherrail", "overworld"], + "autoconnect_by_name": true, + "autoconnect_by_proximity": 10, + "connections": [ + ["station_code_name", "station_code_name_2"] + ] + } + ] +} diff --git a/tests/fixtures/coordinates_factor_default.json b/tests/fixtures/coordinates_factor_default.json new file mode 100644 index 0000000..a1452a8 --- /dev/null +++ b/tests/fixtures/coordinates_factor_default.json @@ -0,0 +1,13 @@ +{ + "name": "Empty universe", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "V5_nether", + + "stations": {}, + "lines": [] + } + }, + "networks_connections": [] +} diff --git a/tests/fixtures/coordinates_factor_default_nether.json b/tests/fixtures/coordinates_factor_default_nether.json new file mode 100644 index 0000000..674f8ab --- /dev/null +++ b/tests/fixtures/coordinates_factor_default_nether.json @@ -0,0 +1,15 @@ +{ + "name": "Empty universe", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "V5_nether", + + "is_nether": true, + + "stations": {}, + "lines": [] + } + }, + "networks_connections": [] +} diff --git a/tests/fixtures/coordinates_factor_overridden.json b/tests/fixtures/coordinates_factor_overridden.json new file mode 100644 index 0000000..041bd87 --- /dev/null +++ b/tests/fixtures/coordinates_factor_overridden.json @@ -0,0 +1,16 @@ +{ + "name": "Empty universe", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "V5_nether", + + "is_nether": true, + "coordinates_factor": 1.84, + + "stations": {}, + "lines": [] + } + }, + "networks_connections": [] +} diff --git a/tests/fixtures/empty.json b/tests/fixtures/empty.json new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/tests/fixtures/empty.json @@ -0,0 +1 @@ +{} diff --git a/tests/fixtures/lines_invalid_ref.json b/tests/fixtures/lines_invalid_ref.json new file mode 100644 index 0000000..e5af20d --- /dev/null +++ b/tests/fixtures/lines_invalid_ref.json @@ -0,0 +1,39 @@ +{ + "name": "Vessinque", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "v5_nether", + + "is_nether": true, + + "coordinates_factor": 0.125, + + "stations": { + "station_code_name": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, + + "lines": [ + { + "name": "(can be left empty or removed)", + "color": "#rrggbb", + "path": [ + "station_code_name_unknown" + ] + } + ] + } + }, + "networks_connections": [] +} diff --git a/tests/fixtures/lines_invalid_ref_expanded.json b/tests/fixtures/lines_invalid_ref_expanded.json new file mode 100644 index 0000000..82087c7 --- /dev/null +++ b/tests/fixtures/lines_invalid_ref_expanded.json @@ -0,0 +1,45 @@ +{ + "name": "Vessinque", + "networks": { + "netherrail": { + "name": "Netherrail", + "world": "v5_nether", + + "is_nether": true, + + "coordinates_factor": 0.125, + + "stations": { + "station_code_name": { + "name": "Displayed station name", + "sub_name": "For stations with a main name and another pomf", + "description": "If needed, displayed in the map balloon. Can be omitted.", + "coordinates": [0, 0, 0], + "real_coordinates": [0, 0, 0], + + "is_main": true, + + "has_fast_access": true, + "fast_access_time": 10 + } + }, + + "lines": [ + { + "name": "(can be left empty or removed)", + "color": "#rrggbb", + "path": [ + "station_code_name", + { + "station": "station_code_name_unknown", + "path_type": "walk", + "path_secure": false, + "path_to_next": [[0, 0,0], [0, 0], [0, 0, 0]] + } + ] + } + ] + } + }, + "networks_connections": [] +} diff --git a/tests/fixtures/no_data.json b/tests/fixtures/no_data.json new file mode 100644 index 0000000..2001d27 --- /dev/null +++ b/tests/fixtures/no_data.json @@ -0,0 +1,5 @@ +{ + "name": "Empty universe", + "networks": {}, + "networks_connections": [] +} diff --git a/tests/fixtures/example.json b/tests/fixtures/with_data.json similarity index 100% rename from tests/fixtures/example.json rename to tests/fixtures/with_data.json From bdb47923b024dcfb9f6e285f1e357119447ae0a2 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 16:09:31 +0100 Subject: [PATCH 07/11] Added Jenkinsfile --- Dockerfile | 1 + Jenkinsfile | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) create mode 100644 Dockerfile create mode 100644 Jenkinsfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..1d4c16b --- /dev/null +++ b/Dockerfile @@ -0,0 +1 @@ +FROM rust:latest \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..41228b1 --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,21 @@ +node { + stage('Prepare') { + checkout scm; + + def environment = docker.build 'rust' + + environment.inside { + stage('Clean') { + sh 'cargo clean' + } + + stage('Build') { + sh 'cargo build' + } + + stage('Test') { + sh 'cargo test' + } + } + } +} From 223b1ca9a60ba1a3a6b0386546ffe69c5cd6072c Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 16:55:20 +0100 Subject: [PATCH 08/11] Fixed stages for Jenkins --- Jenkinsfile | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Jenkinsfile b/Jenkinsfile index 41228b1..e8d282f 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,21 +1,21 @@ node { stage('Prepare') { checkout scm; - def environment = docker.build 'rust' + } - environment.inside { - stage('Clean') { - sh 'cargo clean' - } + environment.inside { + stage('Clean') { + sh 'cargo clean' + } - stage('Build') { - sh 'cargo build' - } + stage('Build') { + sh 'cargo build' + } - stage('Test') { - sh 'cargo test' - } + stage('Test') { + sh 'cargo test' } } + } } From 4ea04967051e329e57472010bb46448c854511e0 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 17:01:46 +0100 Subject: [PATCH 09/11] Fixed Jenkinsfile (again) --- Dockerfile | 1 - Jenkinsfile | 27 ++++++++++++++++++--------- 2 files changed, 18 insertions(+), 10 deletions(-) delete mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile deleted file mode 100644 index 1d4c16b..0000000 --- a/Dockerfile +++ /dev/null @@ -1 +0,0 @@ -FROM rust:latest \ No newline at end of file diff --git a/Jenkinsfile b/Jenkinsfile index e8d282f..f5a50b3 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,21 +1,30 @@ -node { - stage('Prepare') { - checkout scm; - def environment = docker.build 'rust' +pipeline { + agent { + docker { image: 'rust:latest' } } + stages { + stage('Prepare') { + steps { + checkout scm; + } + } - environment.inside { stage('Clean') { - sh 'cargo clean' + steps { + sh 'cargo clean' + } } stage('Build') { - sh 'cargo build' + steps { + sh 'cargo build' + } } stage('Test') { - sh 'cargo test' + steps { + sh 'cargo test' + } } } - } } From bc8491f7a3ffa5a65098edb38db03683c70f2077 Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Mon, 19 Mar 2018 17:03:47 +0100 Subject: [PATCH 10/11] Fixed Jenkinsfile (docker agent) --- Jenkinsfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Jenkinsfile b/Jenkinsfile index f5a50b3..a369834 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,6 +1,6 @@ pipeline { agent { - docker { image: 'rust:latest' } + docker 'rust:latest' } stages { stage('Prepare') { From 359d9e8909f2b857178225c10ea81c8c29292daa Mon Sep 17 00:00:00 2001 From: Amaury Carrade Date: Tue, 20 Mar 2018 20:54:07 +0100 Subject: [PATCH 11/11] =?UTF-8?q?More=20=E2=80=9Crustic=E2=80=9D=20conditi?= =?UTF-8?q?on.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * OPT: made a condition more “rustic”. --- src/data/network_connection.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/data/network_connection.rs b/src/data/network_connection.rs index f4e2f65..84253f3 100644 --- a/src/data/network_connection.rs +++ b/src/data/network_connection.rs @@ -22,8 +22,8 @@ impl NetworkConnection { let mut connections: Vec<(String,String)> = Vec::new(); - if connection.connections.is_some() { - for conn in connection.connections.unwrap() { + if let Some(config_connections) = connection.connections { + for conn in config_connections { // Checks if the references are valid network_1.station(&conn.0).ok_or(InvalidConfigurationError::StationNotFound { name: conn.0.clone() })?; network_2.station(&conn.1).ok_or(InvalidConfigurationError::StationNotFound { name: conn.1.clone() })?;