aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: 9c186c9b328b9340bd369bbcce0e14df55ab1f03 (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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
mod app;
mod bitmap;
mod brush;
mod cli;
mod command;
mod consts;
mod dither;
mod error;
mod lisp;
mod message;
mod symmetry;
mod undo;
mod utils;

use {
    app::AppState,
    cli::Config,
    error::{AppError, SdlTTFError},
};

use log::{error, info};

pub fn error_sink() -> Result<(), AppError> {
    let init = || {
        let sdl_context = sdl2::init().map_err(AppError::Sdl)?;
        info!("Initialized SDL context");

        let ttf_context = sdl2::ttf::init()
            .map_err(SdlTTFError::Init)
            .map_err(AppError::SdlTTF)?;
        info!("Initialized SDL_ttf context");
        Ok((sdl_context, ttf_context))
    };

    match cli::parse_args().map_err(AppError::Cli)? {
        Config::Help => {
            println!("{}", cli::HELP_TEXT);
        }

        Config::NewProject {
            file_name,
            dimensions: (width, height),
        } => {
            let (sdl_context, ttf_context) = init()?;
            AppState::init(width, height, &sdl_context, &ttf_context, None, file_name)?.run();
        }

        Config::ExistingProject { file_name } => {
            let (sdl_context, ttf_context) = init()?;
            let image = utils::load_file(&file_name).map_err(AppError::File)?;
            AppState::init(
                image.width(),
                image.height(),
                &sdl_context,
                &ttf_context,
                Some(image.data),
                Some(file_name),
            )?
            .run();
        }
    }
    return Ok(());
}

pub fn main() {
    env_logger::init();
    match error_sink() {
        Err(e) => error!("{}", e),
        _ => (),
    }
}