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
use std::fmt::Display;
use std::time::Instant;

use cpclib_common::camino::Utf8PathBuf;
use cpclib_common::itertools::Itertools;

use super::Env;

pub struct Report<'env> {
    nb_passes: usize,
    duration: std::time::Duration,
    saved_files: Vec<&'env SavedFile>
}

#[derive(Clone, Debug)]
pub struct SavedFile {
    pub(crate) name: Utf8PathBuf,
    pub(crate) size: usize
}

impl<'env> From<(&'env Env, &Instant)> for Report<'env> {
    fn from((env, start): (&'env Env, &Instant)) -> Self {
        Report {
            nb_passes: env.real_nb_passes,
            duration: Instant::now().duration_since(*start),
            saved_files: env
                .saved_files
                .as_ref()
                .map(|v| v.iter().collect_vec())
                .unwrap_or(Vec::new())
        }
    }
}

impl<'env> Display for Report<'env> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let duration = self.duration.as_secs_f64();

        if false {
            let saved = self
                .saved_files
                .iter()
                .map(|s| format!("Saved \"{}\" for {} bytes.\n", s.name, s.size))
                .join("");
            write!(f, "{}", saved)?;
        }
        write!(
            f,
            "Assembled in {} pass{} and {}.",
            self.nb_passes,
            if self.nb_passes > 1 { "es" } else { "" },
            if duration >= 60. {
                format!("{:.2}min", duration / 60.)
            }
            else {
                format!("{:.2}s", duration)
            }
        )
    }
}