aboutsummaryrefslogtreecommitdiff
path: root/src/handlers/export.rs
blob: 6fb17136fc941c71a3580fd3e7caddda882979b0 (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Map, wardley map editor for linux
// Copyright (C) 2026 Rubén Beltrán del Río

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.

// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
use std::path::PathBuf;

use crate::AppModel;
use crate::actions::ImageFormat;
use crate::dialogs;
use crate::stages::LocalizedStageType;
use crate::tr;
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.to_localized(),
            &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.to_localized(),
            &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,
            &tr!("Could not export image. Please make sure the destination is writable."),
            &error,
        );
    }
}