Ir al contenido
← Todos los ejemplos

Ejemplo 15

Capabilities

what this terminal offers programs right now, contract by

Categoría
Espacio de trabajo: lee o cambia gráficos y paneles
Permisos
none (it describes; it grants nothing)
Antes de ejecutarlo
running inside TTT (the CLI has no host)
Qué hace
none
Cómo ejecutarlo
in TTT, open "T0 Lang", open this file, Run. Then grant something
Lenguaje
>=0.1.1, <0.2.0
Host
>=0.1.3, <0.2.0
Necesita
capabilities >=0.1.0, <0.2.0

Código

// Purpose: what this terminal offers programs right now, contract by
// contract: what can run here (available) and what this session has
// granted (authorized) — two different things.
// Category: workspace
// Permissions: none (it describes; it grants nothing)
// Preconditions: running inside TTT (the CLI has no host)
// Side effects: none
// Run: in TTT, open "T0 Lang", open this file, Run. Then grant something
// and Run again: the "granted" counts change, the "available" ones do not.

lang ">=0.1.1, <0.2.0";
host "ttt" ">=0.1.3, <0.2.0";
requires "capabilities" ">=0.1.0, <0.2.0";
import std.list;

fn main() {
    let all = capabilities.operations();
    let mut contracts: List<String> = [];
    for op in all {
        if !list.contains(contracts, op.contract) {
            contracts.push(op.contract);
        }
    }
    for name in contracts {
        let mut total = 0;
        let mut available = 0;
        let mut usable = 0;
        let mut version = "";
        for op in all {
            if op.contract == name {
                total = total + 1;
                version = op.version;
                if op.available {
                    available = available + 1;
                    if op.granted {
                        usable = usable + 1;
                    }
                }
            }
        }
        print(name + " " + version + ": " + to_string(total) + " operations, " + to_string(available) + " available here, " + to_string(usable) + " of those granted");
    }
    for op in all {
        match op.why {
            Some(why) => print("  not here: " + op.contract + "." + op.operation + " (" + why + ")"),
            None => {}
        }
    }
}