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
pub mod colour;
pub mod cp437;
pub mod help;
pub mod meta;

use std::{
    fs::File,
    io::{stdout, Write},
    process::ExitCode,
};

#[inline]
pub fn _process<
    'a,
    F: for<'b> FnOnce(
            &'b mut File,
            &'b mut Box<dyn Write>,
            Option<meta::Meta>,
        ) -> Result<(), String>
        + 'a,
>(
    input: &String,
    output: &Option<String>,
    callback: F,
) -> ExitCode {
    match wrapped_process(input, output, callback) {
        Ok(_) => {
            return ExitCode::from(0);
        }
        Err(msg) => {
            eprintln!("\x1B[31mERROR: {}\x1B[0m", msg);
            return ExitCode::from(2);
        }
    }
}

#[inline]
fn wrapped_process<
    'a,
    F: for<'b> FnOnce(
            &'b mut File,
            &'b mut Box<dyn Write>,
            Option<meta::Meta>,
        ) -> Result<(), String>
        + 'a,
>(
    input: &String,
    output: &Option<String>,
    callback: F,
) -> Result<(), String> {
    let mut input = File::open(input).map_err(|x| return x.to_string())?;
    let meta = meta::read(&mut input)?;
    meta::check(&meta)?;
    let mut output = match output {
        Some(filename) => Box::new(File::create_new(filename).map_err(|x| return x.to_string())?)
            as Box<dyn Write>,
        None => Box::new(stdout()) as Box<dyn Write>,
    };

    return callback(&mut input, &mut output, meta);
}