kataru/
packer.rs

1use crate::traits::{LoadYaml, SaveMessagePack, SaveYaml};
2use crate::{
3    structs::{Bookmark, Story},
4    Error,
5};
6
7use std::path::Path;
8
9// Dumps the file to disk.
10fn dump<S: SaveYaml + SaveMessagePack>(obj: &S, outpath: &Path) -> Result<(), Error> {
11    // Dump yaml for debugging
12    if cfg!(debug_assertions) {
13        obj.save_yml(outpath.with_extension("yml"))?;
14    }
15
16    // Dump message pack
17    obj.save_mp(outpath)
18}
19
20/// Parses the config and story files into RMP and writes to the output.
21pub fn pack(dir: &str, outdir: &str) -> Result<(), Error> {
22    let path = Path::new(dir);
23    let outpath = Path::new(outdir);
24
25    let story = Story::load_yml(path.join("story"))?;
26    dump(&story, &outpath.join("story"))?;
27
28    // Copy default configs to bookmark.
29    let mut bookmark = Bookmark::load_yml(path.join("bookmark.yml"))?;
30    bookmark.init_state(&story);
31    dump(&bookmark, &outpath.join("bookmark"))?;
32    Ok(())
33}