This commit is contained in:
2020-11-28 14:51:43 +00:00
parent 47a9bf5c79
commit 4d40fd6e08
5 changed files with 76 additions and 19 deletions

View File

@@ -76,16 +76,21 @@ pub fn load_game(game_data: String) -> String {
let result = Game::from(game_data);
match result {
Ok((game, _errs)) => {
Ok((game, errors)) => {
let palette_id = game.palette_ids()[0].clone();
let game_name = game.name.clone();
let errors: Vec<String> = errors.iter().map(|err| format!("{}", err)).collect();
state.game = Some(game);
state.palette = SelectedPalette::Existing { id: palette_id };
format!("Loaded game")
},
format!("Loaded game: {}. Errors: {}", game_name, errors.join(", "))
}
_ => {
state.game = None;
state.palette = SelectedPalette::None;
format!("{}", result.err().unwrap())
format!("Error: {}", result.err().unwrap())
}
}
}
@@ -167,18 +172,20 @@ pub fn get_palettes() -> String {
let mut palette_objects = json::JsonValue::new_array();
for palette in &state.game.as_ref().unwrap().palettes {
let mut object = json::JsonValue::new_object();
if state.game.is_some() {
for palette in &state.game.as_ref().unwrap().palettes {
let mut object = json::JsonValue::new_object();
object.insert("id", palette.id.clone()).unwrap();
object.insert("id", palette.id.clone()).unwrap();
object.insert(
"name",
palette.name.clone().unwrap_or(
format!("Palette {}", palette.id))
).unwrap();
object.insert(
"name",
palette.name.clone().unwrap_or(
format!("Palette {}", palette.id))
).unwrap();
palette_objects.push(object).unwrap();
palette_objects.push(object).unwrap();
}
}
json::stringify(palette_objects)
@@ -392,4 +399,16 @@ mod test {
// todo what? why are extraneous pixels appearing in the output tiles?
assert_eq!(output(), include_str!("test-resources/expected.bitsy"));
}
#[test]
fn palettes() {
load_default_game();
assert_eq!(crate::get_palettes(), "[{\"id\":\"0\",\"name\":\"blueprint\"}]");
}
#[test]
fn no_palettes() {
assert_eq!(crate::get_palettes(), "[]");
}
}