~dkellner/chronofold

9fe1d3caee76a6d065496f8437ef26f78dec1b00 — Dominik Kellner 1 year, 3 months ago 7588abc
Implement `formatted_log` for nicer debug output
2 files changed, 27 insertions(+), 0 deletions(-)

A src/debug.rs
M src/lib.rs
A src/debug.rs => src/debug.rs +26 -0
@@ 0,0 1,26 @@
use std::fmt;

use crate::{Author, Chronofold, LogIndex};

impl<A: Author, T: fmt::Debug> Chronofold<A, T> {
    pub fn formatted_log(&self) -> String {
        let mut result = format!("{:<4} | {:<4} | {:<4} | change\n", "idx", "ref", "next");
        for (idx, change) in self.log.iter().enumerate() {
            let log_idx = LogIndex(idx);
            let formatted_ref = format_option(self.references.get(&log_idx));
            let next = format_option(self.next_indices.get(&log_idx));
            result += &format!(
                "{:<4} | {:<4} | {:<4} | {:?}\n",
                idx, formatted_ref, next, change
            );
        }
        result
    }
}

fn format_option<T: fmt::Display>(option: Option<T>) -> String {
    match option {
        Some(t) => format!("{}", t),
        None => "".to_owned(),
    }
}

M src/lib.rs => src/lib.rs +1 -0
@@ 72,6 72,7 @@
// private. This keeps things simple for our users and gives us more
// flexibility in restructuring the crate.
mod change;
mod debug;
mod distributed;
mod error;
mod fmt;