use anyhow::Result;
use clap::Args;
use std::{
convert::TryFrom,
fmt,
};
#[derive(Default, Clone, Debug, Args)]
pub struct VerbosityFlags {
#[clap(long)]
quiet: bool,
#[clap(long)]
verbose: bool,
}
impl TryFrom<&VerbosityFlags> for Verbosity {
type Error = anyhow::Error;
fn try_from(value: &VerbosityFlags) -> Result<Self, Self::Error> {
match (value.quiet, value.verbose) {
(false, false) => Ok(Verbosity::Default),
(true, false) => Ok(Verbosity::Quiet),
(false, true) => Ok(Verbosity::Verbose),
(true, true) => anyhow::bail!("Cannot pass both --quiet and --verbose flags"),
}
}
}
#[derive(
Clone, Copy, Default, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug,
)]
pub enum Verbosity {
#[default]
Default,
Quiet,
Verbose,
}
impl Verbosity {
pub fn is_verbose(&self) -> bool {
match self {
Verbosity::Quiet => false,
Verbosity::Default | Verbosity::Verbose => true,
}
}
}
#[derive(Eq, PartialEq, Copy, Clone, Debug, Default, serde::Serialize)]
pub enum Network {
#[default]
Online,
Offline,
}
impl Network {
pub fn append_to_args(&self, args: &mut Vec<String>) {
match self {
Self::Online => (),
Self::Offline => args.push("--offline".to_owned()),
}
}
}
#[derive(
Copy,
Clone,
Default,
Eq,
PartialEq,
Debug,
clap::ValueEnum,
serde::Serialize,
serde::Deserialize,
)]
#[clap(name = "build-artifacts")]
pub enum BuildArtifacts {
#[clap(name = "all")]
#[default]
All,
#[clap(name = "code-only")]
CodeOnly,
#[clap(name = "check-only")]
CheckOnly,
}
impl BuildArtifacts {
pub fn steps(&self) -> usize {
match self {
BuildArtifacts::All => 5,
BuildArtifacts::CodeOnly => 4,
BuildArtifacts::CheckOnly => 1,
}
}
}
#[derive(
Eq,
PartialEq,
Copy,
Clone,
Debug,
Default,
clap::ValueEnum,
serde::Serialize,
serde::Deserialize,
strum::EnumIter,
)]
pub enum Target {
#[clap(name = "wasm")]
#[default]
Wasm,
#[clap(name = "riscv")]
RiscV,
}
impl Target {
pub fn llvm_target(&self) -> &'static str {
match self {
Self::Wasm => "wasm32-unknown-unknown",
Self::RiscV => "riscv32i-unknown-none-elf",
}
}
pub fn rustflags(&self) -> Option<&'static str> {
match self {
Self::Wasm => Some("-Clink-arg=-zstack-size=65536\x1f-Clink-arg=--import-memory\x1f-Ctarget-cpu=mvp"),
Self::RiscV => None,
}
}
pub fn source_extension(&self) -> &'static str {
match self {
Self::Wasm => "wasm",
Self::RiscV => "",
}
}
pub fn dest_extension(&self) -> &'static str {
match self {
Self::Wasm => "wasm",
Self::RiscV => "riscv",
}
}
}
#[derive(
Eq, PartialEq, Copy, Clone, Debug, Default, serde::Serialize, serde::Deserialize,
)]
pub enum BuildMode {
#[default]
Debug,
Release,
Verifiable,
}
impl fmt::Display for BuildMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Debug => write!(f, "debug"),
Self::Release => write!(f, "release"),
Self::Verifiable => write!(f, "verifiable"),
}
}
}
#[derive(Clone, Debug, Default)]
pub enum OutputType {
#[default]
HumanReadable,
Json,
}
#[derive(Default, Clone, Debug, Args)]
pub struct UnstableOptions {
#[clap(long = "unstable-options", short = 'Z', number_of_values = 1)]
options: Vec<String>,
}
#[derive(Clone, Default)]
pub struct UnstableFlags {
pub original_manifest: bool,
}
impl TryFrom<&UnstableOptions> for UnstableFlags {
type Error = anyhow::Error;
fn try_from(value: &UnstableOptions) -> Result<Self, Self::Error> {
let valid_flags = ["original-manifest"];
let invalid_flags = value
.options
.iter()
.filter(|o| !valid_flags.contains(&o.as_str()))
.collect::<Vec<_>>();
if !invalid_flags.is_empty() {
anyhow::bail!("Unknown unstable-options {:?}", invalid_flags)
}
Ok(UnstableFlags {
original_manifest: value.options.contains(&"original-manifest".to_owned()),
})
}
}
#[derive(Default, Clone, Debug, Args)]
pub struct Features {
#[clap(long, value_delimiter = ',')]
features: Vec<String>,
}
impl Features {
pub fn push(&mut self, feature: &str) {
self.features.push(feature.to_owned())
}
pub fn append_to_args(&self, args: &mut Vec<String>) {
if !self.features.is_empty() {
args.push("--features".to_string());
let features = if self.features.len() == 1 {
self.features[0].clone()
} else {
self.features.join(",")
};
args.push(features);
}
}
}