unitype-cli 0.1.0

Command-line interface for unitype
//! `unitype ls` — what is in this project?

use clap::ValueEnum;
use serde_json::json;
use unitype_core::canon::AliasIndex;
use unitype_core::check::View;

use super::Output;
use crate::Failure;
use crate::project::Project;

/// What to list.
#[derive(Copy, Clone, Debug, PartialEq, Eq, ValueEnum)]
pub enum What {
    /// Everything, in summary.
    All,
    /// The universes.
    Universes,
    /// The type declarations.
    Types,
    /// The propositions.
    Sigs,
    /// The proofs, and where they run.
    Impls,
    /// The links between universes.
    Links,
    /// The places proofs can run.
    Environments,
}

/// Lists what was asked for.
///
/// With a view, types and propositions are named by their address under it,
/// so that whatever it counts as the same reads the same.
pub fn run(
    project: &Project,
    what: What,
    view: Option<View>,
    out: &Output,
) -> Result<(), Failure> {
    match what {
        What::All => all(project, view, out),
        What::Universes => universes(project, out),
        What::Types => types(project, view, out),
        What::Sigs => sigs(project, view, out),
        What::Impls => impls(project, out),
        What::Links => links(project, out),
        What::Environments => environments(project, out),
    }
    Ok(())
}

fn all(project: &Project, view: Option<View>, out: &Output) {
    if out.is_machine() {
        universes(project, out);
        return;
    }
    universes(project, out);
    out.heading("Types");
    types(project, view, out);
    out.heading("Propositions");
    sigs(project, view, out);
    if !project.workspace.workspace().links.is_empty() {
        out.heading("Links");
        links(project, out);
    }
    out.heading("Proofs");
    impls(project, out);
}

fn universes(project: &Project, out: &Output) {
    let ws = project.workspace.workspace();
    out.result(
        json!(
            ws.universes
                .iter()
                .map(|u| json!({
                    "name": u.name.to_string(),
                    "doc": u.doc,
                    "types": u.types.len(),
                    "signatures": u.functions.len(),
                }))
                .collect::<Vec<_>>()
        ),
        || {
            for universe in &ws.universes {
                out.say(format!(
                    "  {}  {} type{}, {} signature{}",
                    universe.name,
                    universe.types.len(),
                    plural(universe.types.len()),
                    universe.functions.len(),
                    plural(universe.functions.len()),
                ));
                if let Some(doc) = &universe.doc {
                    out.say(format!("      {}", first_sentence(doc)));
                }
            }
        },
    );
}

fn types(project: &Project, view: Option<View>, out: &Output) {
    let ws = &project.workspace;
    // Aliases are shortened against everything loaded, so the words shown are
    // as short as they can be while still picking one declaration out.
    let index: AliasIndex = match view {
        Some(view) => ws.alias_index_under(view),
        None => ws.alias_index(),
    };

    let rows: Vec<serde_json::Value> = ws
        .def_names()
        .map(|name| {
            let shape = ws.shape_hash(name);
            let named = match view {
                Some(view) => ws.address(name, view),
                None => shape,
            };
            json!({
                "name": name.to_string(),
                "kind": ws.def(name).map(|d| d.kind_name()),
                "address": shape.map(|s| s.to_string()),
                "addresses": by_view(|v| ws.address(name, v)),
                "words": named.and_then(|s| index.alias_for(&s)),
            })
        })
        .collect();

    out.result(json!(rows), || {
        for row in &rows {
            let words = row["words"].as_str().unwrap_or("");
            out.say(format!(
                "  {:<28} {:<10} {}",
                row["name"].as_str().unwrap_or(""),
                row["kind"].as_str().unwrap_or(""),
                words
            ));
        }
    });
}

fn sigs(project: &Project, view: Option<View>, out: &Output) {
    let ws = &project.workspace;
    let index = view.map(|v| ws.alias_index_under(v));
    let rows: Vec<serde_json::Value> = ws
        .sig_names()
        .map(|name| {
            let sig = ws.sig(name).expect("listed");
            let proofs = project.registry.proofs_of(name).len();
            let words = view
                .zip(index.as_ref())
                .and_then(|(v, index)| index.alias_for(&ws.sig_address(name, v)?));
            json!({
                "name": name.to_string(),
                "type": unitype_core::canon::canonical_string(&sig.function_type()),
                "refines": sig.refines.as_ref().map(ToString::to_string),
                "refined_by": ws.refiners_of(name).iter().map(ToString::to_string).collect::<Vec<_>>(),
                "equivalent_to": ws.equivalents_of(name).iter().map(|e| e.sig.to_string()).collect::<Vec<_>>(),
                "has_body": sig.body.is_some(),
                "proofs": proofs,
                "address": ws.sig_hash(name).map(|h| h.to_string()),
                "addresses": by_view(|v| ws.sig_address(name, v)),
                "words": words,
            })
        })
        .collect();

    out.result(json!(rows), || {
        for row in &rows {
            let name = row["name"].as_str().unwrap_or("");
            let proofs = row["proofs"].as_u64().unwrap_or(0);
            let words = row["words"]
                .as_str()
                .map(|w| format!("  {w}"))
                .unwrap_or_default();
            out.say(format!(
                "  {:<28} {} proof{}{}{}",
                name,
                proofs,
                plural(proofs as usize),
                if row["has_body"].as_bool() == Some(true) {
                    "  (proves itself)"
                } else {
                    ""
                },
                words,
            ));
            if let Some(refines) = row["refines"].as_str() {
                out.say(format!("      strengthens {refines}"));
            }
            for same in row["equivalent_to"].as_array().into_iter().flatten() {
                out.say(format!(
                    "      the same as {}, arguments reordered",
                    same.as_str().unwrap_or("")
                ));
            }
        }
    });
}

/// An address under each view, keyed by the view's name.
fn by_view(address: impl Fn(View) -> Option<unitype_core::TypeId>) -> serde_json::Value {
    View::ALL
        .into_iter()
        .map(|v| (v.as_str().to_string(), json!(address(v).map(|a| a.to_string()))))
        .collect::<serde_json::Map<_, _>>()
        .into()
}

fn impls(project: &Project, out: &Output) {
    let rows: Vec<serde_json::Value> = project
        .registry
        .implementations()
        .iter()
        .map(|i| {
            json!({
                "id": i.id.to_string(),
                "proves": i.proves.to_string(),
                "environment": i.environment.to_string(),
                "entry": i.entry,
                "cost": i.cost.to_string(),
                "notes": i.notes,
            })
        })
        .collect();

    out.result(json!(rows), || {
        for row in &rows {
            out.say(format!(
                "  {:<28} {:<20} {:<10} {}",
                row["id"].as_str().unwrap_or(""),
                row["proves"].as_str().unwrap_or(""),
                row["environment"].as_str().unwrap_or(""),
                row["cost"].as_str().unwrap_or(""),
            ));
        }
    });
}

fn links(project: &Project, out: &Output) {
    let rows: Vec<serde_json::Value> = project
        .workspace
        .workspace()
        .links
        .iter()
        .map(|l| {
            json!({
                "name": l.name.to_string(),
                "from": l.from.to_string(),
                "to": l.to.to_string(),
                "kind": match &l.kind {
                    unitype_core::LinkKind::Identical => "identical",
                    unitype_core::LinkKind::FieldMap { .. } => "field map",
                    unitype_core::LinkKind::Witness { .. } => "witnessed",
                    unitype_core::LinkKind::Equivalent { .. } => "equivalent",
                },
                "both_ways": l.is_reversible(),
                "lossy": l.is_lossy(),
            })
        })
        .collect();

    out.result(json!(rows), || {
        for row in &rows {
            out.say(format!(
                "  {} {} {}  ({}{})",
                row["from"].as_str().unwrap_or(""),
                if row["both_ways"].as_bool() == Some(true) {
                    "<->"
                } else {
                    "->"
                },
                row["to"].as_str().unwrap_or(""),
                row["kind"].as_str().unwrap_or(""),
                if row["lossy"].as_bool() == Some(true) {
                    ", loses fields"
                } else {
                    ""
                }
            ));
        }
    });
}

fn environments(project: &Project, out: &Output) {
    let rows: Vec<serde_json::Value> = project
        .registry
        .environments()
        .map(|e| {
            json!({
                "id": e.id.to_string(),
                "language": e.language.to_string(),
                "reach": e.reach.kind(),
                "doc": e.doc,
            })
        })
        .collect();

    out.result(json!(rows), || {
        for row in &rows {
            out.say(format!(
                "  {:<12} {:<12} {}",
                row["id"].as_str().unwrap_or(""),
                row["language"].as_str().unwrap_or(""),
                row["reach"].as_str().unwrap_or(""),
            ));
        }
    });
}

fn plural(n: usize) -> &'static str {
    if n == 1 { "" } else { "s" }
}

fn first_sentence(text: &str) -> String {
    let single = text.trim().replace('\n', " ");
    match single.find(". ") {
        Some(at) => single[..=at].to_string(),
        None => single,
    }
}