wot-network-rpgpie 0.0.2

Transform OpenPGP certificate sets into a wot-network structure
Documentation
use std::path::Path;

use chrono::{DateTime, Utc};
use clap::Parser;
use rpgpie::Error;
use wot_network::Network;

#[derive(Parser)]
enum Cli {
    /// Transform a set of certificates into a WoT network, in "simple text" format
    Export(Source),

    /// Transform a set of certificates into a WoT network, as a mermaid graph
    Graph(Source),

    /// Ingest and dump a WoT network from "simple text" format
    Import(Wot),
}

#[derive(clap::Args)]
#[command(version, about, long_about = None)]
struct Source {
    /// A certificate ring
    certs: std::path::PathBuf,

    /// Reference time (e.g. "2024-01-01T00:00:00Z")
    #[arg(long, value_parser = humantime::parse_rfc3339)]
    time: Option<std::time::SystemTime>,

    /// Identity mapping file for graph output (CSV formatted)
    #[arg(long)]
    map: Option<std::path::PathBuf>,
}

#[derive(clap::Args)]
#[command(version, about, long_about = None)]
struct Wot {
    wot: std::path::PathBuf,

    /// Identity mapping file for graph output (CSV formatted)
    #[arg(long)]
    map: Option<std::path::PathBuf>,
}

fn load(path: &Path, time: DateTime<Utc>) -> Result<Network, Error> {
    wot_network_rpgpie::from_file(path, time)
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    match Cli::parse() {
        Cli::Export(src) => {
            let net = load(&src.certs, src.time.map(Into::into).unwrap_or(Utc::now()))?;

            let mapping = src
                .map
                .map(|path| wot_network::util::mapping::load_certid_map(&path));

            let net = if let Some(map) = mapping {
                wot_network::util::mapping::map_names(&net, map)
            } else {
                net
            };

            println!("{}", wot_network::util::simple_text::export(&net));
        }
        Cli::Graph(src) => {
            let net = load(&src.certs, src.time.map(Into::into).unwrap_or(Utc::now()))?;

            let mapping = src
                .map
                .map(|path| wot_network::util::mapping::load_certid_map(&path));

            let net = if let Some(map) = mapping {
                wot_network::util::mapping::map_names(&net, map)
            } else {
                net
            };

            println!("{}", wot_network::util::mermaid::graph(&net));
        }

        Cli::Import(wot) => {
            let data = std::fs::read_to_string(wot.wot)?;

            let net = wot_network::util::simple_text::import(&data);

            // let mapping = wot.map.map(|path| Network::load_certid_map(&path));
            // let net = if let Some(map) = mapping {
            //     net.map_names(map)
            // } else {
            //     net
            // };

            println!("{:#?}", net);
        }
    }

    Ok(())
}