netdig 0.0.5

Utilities for analyzing and aggregating CIDR blocks
Documentation
use comfy_table::{presets::NOTHING, Table};
use std::fmt::Debug;

use crate::Output;

pub trait Tabular {
    fn table(&self, headers: bool) -> Table;
}

impl<T: Debug> Tabular for Vec<Output<T>> {
    fn table(&self, headers: bool) -> Table {
        let mut table = Table::new();
        table.load_preset(NOTHING);
        if headers {
            table.add_row(vec![
                "POS",
                "INPUT",
                "INVALID",
                "CANONICAL",
                "CONTAINED",
                "NETWORK",
                "ADDRESS",
                "NETMASK",
                "HOSTS",
                "WHOIS",
                "WARNINGS",
            ]);
        }

        for (n, output) in self.iter().enumerate() {
            table.add_row(vec![
                (n + 1).to_string(),
                if let Some(input) = &output.input {
                    format!("{input:?}")
                } else {
                    String::new()
                },
                if output.valid {
                    ""
                } else {
                    "!"
                }
                .to_string(),
                output.canonical.to_string(),
                output
                    .contained_by
                    .map_or_else(String::new, |s| ToString::to_string(&s)),
                output.network.to_string(),
                output.address.to_string(),
                output.netmask.to_string(),
                output.hosts.to_string(),
                output.whois.join(" "),
                output.warnings.join("; "),
            ]);
        }

        table
    }
}