#![forbid(unsafe_code)]
mod builtins;
mod commands;
mod project;
use std::path::PathBuf;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
mod exit {
pub const USAGE: u8 = 1;
pub const CHECK: u8 = 2;
pub const RUN: u8 = 3;
pub const DISAGREEMENT: u8 = 4;
}
#[derive(Parser)]
#[command(
name = "unitype",
version,
about = "A universal type system: one canonical form, one address, many proofs.",
long_about = None,
)]
struct Cli {
#[arg(long, short, global = true, value_name = "PATH")]
project: Option<PathBuf>,
#[arg(long, global = true)]
json: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Check,
Ls {
#[arg(value_enum, default_value_t = commands::ls::What::All)]
what: commands::ls::What,
#[arg(long)]
view: Option<unitype_core::check::View>,
},
Words {
name: String,
},
Infer {
#[arg(long, conflicts_with = "file")]
data: Option<String>,
#[arg(long)]
file: Option<PathBuf>,
#[arg(long)]
tags: bool,
#[arg(long, value_name = "NAME")]
lift: Option<String>,
},
Validate {
#[arg(long, value_name = "TYPE")]
r#type: String,
#[arg(long, conflicts_with = "file")]
data: Option<String>,
#[arg(long)]
file: Option<PathBuf>,
},
Cast {
#[arg(long, value_name = "TYPE")]
r#type: String,
#[arg(long, conflicts_with = "file")]
data: Option<String>,
#[arg(long)]
file: Option<PathBuf>,
#[arg(long, default_value = "json")]
lang: String,
},
Gen {
#[arg(long, value_delimiter = ',')]
lang: Vec<String>,
#[arg(long, value_name = "DIR")]
out: Option<PathBuf>,
#[arg(long)]
dry_run: bool,
},
Run {
signature: String,
#[arg(long, default_value = "[]")]
args: String,
#[arg(long, value_name = "TYPES")]
at: Option<String>,
#[arg(long, value_enum, default_value_t = commands::run::Choice::Default)]
policy: commands::run::Choice,
#[arg(long, value_name = "ID")]
r#impl: Option<String>,
#[arg(long, short)]
verbose: bool,
},
Bridge {
#[arg(long, value_name = "TYPE")]
from: String,
#[arg(long, value_name = "TYPE")]
to: String,
#[arg(long)]
data: Option<String>,
},
Conform {
#[arg(long, value_name = "SIGNATURE")]
sig: Option<String>,
},
Reflect {
name: Option<String>,
},
Classes,
Suggest {
#[arg(long, default_value_t = 0.5)]
min: f32,
},
}
#[tokio::main]
async fn main() -> ExitCode {
let cli = Cli::parse();
match run(cli).await {
Ok(()) => ExitCode::SUCCESS,
Err(failure) => {
eprintln!("{}", failure.message);
ExitCode::from(failure.code)
}
}
}
pub struct Failure {
message: String,
code: u8,
}
impl Failure {
pub fn usage(message: impl Into<String>) -> Self {
Self {
message: message.into(),
code: exit::USAGE,
}
}
pub fn check(message: impl Into<String>) -> Self {
Self {
message: message.into(),
code: exit::CHECK,
}
}
pub fn run(message: impl Into<String>) -> Self {
Self {
message: message.into(),
code: exit::RUN,
}
}
pub fn disagreement(message: impl Into<String>) -> Self {
Self {
message: message.into(),
code: exit::DISAGREEMENT,
}
}
}
async fn run(cli: Cli) -> Result<(), Failure> {
let here = std::env::current_dir().map_err(|e| Failure::usage(e.to_string()))?;
let path =
project::find(cli.project.as_deref(), &here).map_err(|e| Failure::usage(e.to_string()))?;
let project = project::Project::load(&path).map_err(|e| Failure::check(e.to_string()))?;
let out = commands::Output::new(cli.json);
match cli.command {
Command::Check => commands::check::run(&project, &out),
Command::Ls { what, view } => commands::ls::run(&project, what, view, &out),
Command::Words { name } => commands::words::run(&project, &name, &out),
Command::Infer {
data,
file,
tags,
lift,
} => commands::infer::run(&project, data, file, tags, lift, &out),
Command::Validate { r#type, data, file } => {
commands::validate::run(&project, &r#type, data, file, &out)
}
Command::Cast {
r#type,
data,
file,
lang,
} => commands::cast::run(&project, &r#type, data, file, &lang, &out),
Command::Gen {
lang,
out: into,
dry_run,
} => commands::generate::run(&project, lang, into, dry_run, &out),
Command::Run {
signature,
args,
at,
policy,
r#impl,
verbose,
} => {
commands::run::run(
&project,
&signature,
&args,
at.as_deref(),
policy,
r#impl,
verbose,
&out,
)
.await
}
Command::Bridge { from, to, data } => {
commands::bridge::run(&project, &from, &to, data, &out).await
}
Command::Conform { sig } => commands::conform::run(&project, sig, &out).await,
Command::Reflect { name } => commands::reflect::run(&project, name, &out),
Command::Classes => commands::classes::run(&project, &out),
Command::Suggest { min } => commands::suggest::run(&project, min, &out),
}
}