Files
peachy/src/scene.rs

68 lines
1.8 KiB
Rust
Raw Normal View History

2021-05-17 20:51:32 +01:00
use std::fs::read_to_string;
2021-05-17 20:50:32 +01:00
use std::path::PathBuf;
use serde_derive::{Serialize, Deserialize};
2021-11-14 18:05:29 +00:00
use crate::Position;
2021-05-17 20:50:32 +01:00
#[derive(Debug, Eq, PartialEq)]
pub struct Scene {
pub name: String,
// rename to "tiles"?
pub background: Vec<Option<String>>,
// rename to "things"?
pub foreground: Vec<Option<String>>,
}
impl Scene {
pub fn from_file(path: PathBuf) -> Self {
let name = path.file_stem().unwrap().to_str().unwrap().into();
let data = read_to_string(path).unwrap();
let intermediate: IntermediateScene = toml::from_str(&data).unwrap();
let mut background = Vec::new();
let mut foreground = Vec::new();
for name in intermediate.background.iter() {
match name.as_ref() {
"" => background.push(None),
2021-05-17 20:51:32 +01:00
_ => background.push(Some(name.to_owned())),
2021-05-17 20:50:32 +01:00
}
}
for name in intermediate.foreground.iter() {
match name.as_ref() {
"" => foreground.push(None),
_ => foreground.push(Some(name.to_owned())),
}
}
Self { name, background, foreground }
}
2021-11-14 18:05:29 +00:00
pub fn set_tile(_position: Position, _new: String) {
todo!();
}
2021-05-17 20:50:32 +01:00
}
2021-05-17 20:58:24 +01:00
/// scene name is derived from the filename,
/// and None values are represented as "" in the toml
/// so it's Vec<String> instead of Vec<Option<String>>
2021-05-17 20:50:32 +01:00
#[derive(Serialize, Deserialize)]
struct IntermediateScene {
background: Vec<String>,
foreground: Vec<String>,
}
#[cfg(test)]
mod test {
use std::path::PathBuf;
use crate::scene::Scene;
#[test]
fn scene_from_file() {
let path = PathBuf::from("src/test-resources/basic/scenes/zero.toml");
let output = Scene::from_file(path);
let expected = crate::mock::scenes::zero();
assert_eq!(output, expected);
}
}