aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/export.rs
blob: 45e980e5ed11f8c86bfaa9acabae2e4b4ab529c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
use std::path::PathBuf;

use crate::AppModel;
use crate::actions::ImageFormat;
use crate::dialogs;
use wmap_renderer::{render_to_png, render_to_svg};

/// Exports the map to an image file in the specified format.
pub fn export_to_file(model: &AppModel, path: PathBuf, format: ImageFormat) {
    let result: Result<(), String> = match format {
        ImageFormat::Png => {
            render_to_png(&model.map, &model.stage_type, &model.render_configuration)
                .map_err(|e| e.to_string())
                .and_then(|data| std::fs::write(&path, data).map_err(|e| e.to_string()))
        }
        ImageFormat::Svg => {
            render_to_svg(&model.map, &model.stage_type, &model.render_configuration)
                .map_err(|e| e.to_string())
                .and_then(|data| std::fs::write(&path, data).map_err(|e| e.to_string()))
        }
    };

    if let Err(error) = result {
        dialogs::show_error(&model.window, "Error Exporting Image", &error);
    }
}