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
//! A small collection of tools to handle CP437 files.
//!
//! <div class="warning">
//!
//! This crate is primary written to supply CLI commands, not as a reusable
//! library.
//!
//! While I'll try and avoid making changes to the API, be warned that (at
//! least, at the moment) no guarantees are made about its stability.
//!
//! </div>
//!

#![deny(missing_docs)]

pub mod colour;
pub mod cp437;
pub mod fonts;
#[doc(hidden)]
pub mod help;
pub mod meta;

pub use self::{colour::COLOURS, cp437::*, meta::Meta};

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

#[doc(hidden)]
#[inline]
pub fn process<
    'a,
    F: for<'b> FnOnce(&'b mut File, &'b mut Box<dyn Write>, Option<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>) -> 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);
}