Skip to content

Commit

Permalink
Fix build errors
Browse files Browse the repository at this point in the history
Update settings.rs to use serde_json instead of rustc-serialize
Update dependencies

Closes coeuvre#30
  • Loading branch information
Caleb Marble authored and tormol committed Mar 17, 2023
1 parent ebf96d9 commit ebb65f6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 31 deletions.
43 changes: 34 additions & 9 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ name = "rust-2048"
path = "src/main.rs"

[dependencies]
rustc-serialize = "0.3"
rustc-serialize = "0.3.24"
serde = "1.0.79"
serde_json = "1.0.27"
serde_derive = "1.0.79"
rand = "0.3.7"
piston_window = "0.127.0"
pistoncore-sdl2_window = "0.68.0"
Expand Down
2 changes: 2 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#[macro_use]
extern crate serde_derive;
extern crate rustc_serialize;
extern crate rand;
extern crate piston_window;
Expand Down
52 changes: 31 additions & 21 deletions src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
extern crate serde;
extern crate serde_json;

use self::serde_json::Error;
use std::env::current_exe;
use std::io::{BufWriter, BufReader, Write};
use std::fs::{File};
use std::fs::File;
use std::io::{BufReader, BufWriter, Write};
use std::path::Path;
use rustc_serialize::{ json, Encodable, Decodable };

static SETTING_FILENAME: &'static str = "settings.json";

Expand Down Expand Up @@ -39,7 +41,7 @@ impl Settings {
Settings::from_settings_in_json(&SettingsInJson::load())
}

fn from_settings_in_json<'a>(s: &'a SettingsInJson) -> Settings {
fn from_settings_in_json(s: &SettingsInJson) -> Settings {
let board_size = [
s.tile_size * s.tile_width as f64 + s.tile_padding * (s.tile_width + 1) as f64,
s.tile_size * s.tile_height as f64 + s.tile_padding * (s.tile_height + 1) as f64,
Expand Down Expand Up @@ -125,7 +127,7 @@ impl Settings {
}
}

#[derive(RustcEncodable, RustcDecodable)]
#[derive(Serialize, Deserialize)]
struct SettingsInJson {
asset_folder: String,

Expand Down Expand Up @@ -229,23 +231,31 @@ impl SettingsInJson {
let file = File::open(&path).unwrap();
let mut reader = BufReader::new(file);
*/
let file = File::open(&path);
let file = File::open(&path);

match file {
Err(e) => {
println!("Configuration file can't be open ({}). Try to generate a default one.", e);
let default = SettingsInJson::default_settings();
default.save();
return default;
},
_ => {}
match file {
Err(e) => {
println!("Configuration file can't be open ({}). Try to generate a default one.", e);
let default = SettingsInJson::default_settings();
default.save();
return default;
}
_ => {}
}

let mut reader = BufReader::new(file.unwrap());
// End FIXME

let mut decoder = json::Decoder::new(json::Json::from_reader(&mut reader).unwrap());
Decodable::decode(&mut decoder).unwrap()
let mut reader = BufReader::new(file.unwrap());
let v: Result<SettingsInJson, Error> = serde_json::from_reader(&mut reader);
match v {
Ok(settings) => {
return settings;
}
Err(e) => {
println!("WARNING: Failed to read settings from file. Try to generate a default one. {}", e);
let new_settings = SettingsInJson::default_settings();
new_settings.save();
return new_settings;
}
}
}

pub fn save(&self) {
Expand All @@ -260,12 +270,12 @@ impl SettingsInJson {
let file = File::create(&path.with_file_name(SETTING_FILENAME)).unwrap();
let mut writer = BufWriter::new(file);

match json::encode(self) {
match serde_json::to_string(self) {
Ok(encoded) => {
if let Err(e) = writer.write(encoded.as_bytes()) {
println!("WARNING: Failed to save settings: {}", e);
}
},
}
Err(e) => {
println!("WARNING: Failed to save settings: {}", e);
}
Expand Down

0 comments on commit ebb65f6

Please sign in to comment.