1 | 1 |
use failure::Error;
|
2 | 2 |
use image;
|
3 | 3 |
use serde_json;
|
4 | |
use std::io::{Read, Write, Seek};
|
| 4 |
use std::io::{Read, Seek, Write};
|
5 | 5 |
use zip::{ZipArchive, ZipWriter};
|
| 6 |
|
| 7 |
use crate::constants;
|
6 | 8 |
|
7 | 9 |
/// This value represents both the current document in-memory as well
|
8 | 10 |
/// as the entirety of the values that we will want to both save and
|
|
10 | 12 |
pub struct Document {
|
11 | 13 |
pub tilesheet: image::DynamicImage,
|
12 | 14 |
pub metadata: Metadata,
|
13 | |
pub rules: (),
|
| 15 |
pub rules: Vec<RuleSpec>,
|
| 16 |
}
|
| 17 |
|
| 18 |
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
|
| 19 |
pub struct RuleSpec {
|
| 20 |
pub rule_name: String,
|
| 21 |
pub lhs: Vec<((u16, u16), (u16, u16))>,
|
| 22 |
pub rhs: Vec<((u16, u16), (u16, u16))>,
|
14 | 23 |
}
|
15 | 24 |
|
16 | 25 |
/// This should be renamed probably, but it's the configuration-level
|
|
41 | 50 |
let file = archive.by_name("rules.json")?;
|
42 | 51 |
serde_json::from_reader(file)?
|
43 | 52 |
};
|
44 | |
Ok(Document{ tilesheet, metadata, rules })
|
| 53 |
Ok(Document {
|
| 54 |
tilesheet,
|
| 55 |
metadata,
|
| 56 |
rules,
|
| 57 |
})
|
45 | 58 |
}
|
46 | 59 |
|
47 | 60 |
/// Attempt to write a `Document` from anything which implements
|
|
52 | 65 |
|
53 | 66 |
let mut zip = ZipWriter::new(w);
|
54 | 67 |
zip.start_file("tilesheet.png", FileOptions::default())?;
|
55 | |
self.tilesheet.write_to(&mut zip, image::ImageOutputFormat::PNG)?;
|
| 68 |
self.tilesheet
|
| 69 |
.write_to(&mut zip, image::ImageOutputFormat::PNG)?;
|
56 | 70 |
|
57 | 71 |
zip.start_file("metadata.json", FileOptions::default())?;
|
58 | 72 |
serde_json::to_writer(&mut zip, &self.metadata)?;
|
|
64 | 78 |
writeln!(
|
65 | 79 |
&mut zip,
|
66 | 80 |
"Created by {} v{}",
|
67 | |
::constants::PROGRAM_NAME,
|
68 | |
::constants::PROGRAM_VERSION,
|
| 81 |
constants::PROGRAM_NAME,
|
| 82 |
constants::PROGRAM_VERSION,
|
69 | 83 |
)?;
|
70 | 84 |
|
71 | 85 |
zip.finish()?;
|
|
82 | 96 |
tile_height: 16,
|
83 | 97 |
config_loop_forever: false,
|
84 | 98 |
},
|
85 | |
rules: (),
|
| 99 |
rules: Vec::new(),
|
86 | 100 |
}
|
87 | 101 |
}
|
88 | 102 |
}
|