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
use anyhow::{Context, Result};
use log::LevelFilter;

use necsim_core::{event::TypedEvent, reporter::Reporter};

use necsim_plugins_core::match_any_reporter_plugin_vec;

use crate::args::{
    cli::{replay::ReplayArgs, CommandArgs},
    utils::parse::{try_parse, try_print},
};

#[allow(clippy::module_name_repetitions)]
pub fn replay_with_logger(replay_args: CommandArgs) -> Result<()> {
    log::set_max_level(LevelFilter::Info);

    let replay_args: ReplayArgs = try_parse("replay", &replay_args.into_config_string())?;

    let config_str =
        try_print(&replay_args).context("Failed to normalise the event replay config.")?;

    println!("\n{:=^80}\n", " Replay Configuration ");
    println!("{}", config_str.trim_start_matches("Replay"));
    println!("\n{:=^80}\n", " Replay Configuration ");

    info!("Starting event replay ...");

    match_any_reporter_plugin_vec!(replay_args.reporters => |mut reporter| {
        reporter.initialise().map_err(anyhow::Error::msg)?;

        let mut remaining = replay_args.event_log.length() as u64;

        reporter.report_progress(&remaining.into());

        for event in replay_args.event_log {
            remaining -= 1;
            reporter.report_progress(&remaining.into());

            match event.into() {
                TypedEvent::Speciation(event) => {
                    reporter.report_speciation(&event.into());
                },
                TypedEvent::Dispersal(event) => {
                    reporter.report_dispersal(&event.into());
                },
            }
        }

        if log::log_enabled!(log::Level::Info) {
            println!("\n");
            println!("{:=^80}", " Reporter Summary ");
            println!();
        }
        reporter.finalise();
        if log::log_enabled!(log::Level::Info) {
            println!();
            println!("{:=^80}", " Reporter Summary ");
            println!();
        }
    });

    info!("The event replay has completed.");

    Ok(())
}