Skip to content

Commit

Permalink
Refactor/combine strings in CLI and mopro-ffi (#297)
Browse files Browse the repository at this point in the history
* refactor Platform, Framework, Arch... in constants

* using arch def. under CLI in ffi

* replace mode as well

* move Arch and Mode to mopro-ffi

* fix missed arch in iOS

* update cargo.lock

* chore: remove unnecessary 'pub' modifier

* feat: replace more strings in ios.rs

* fix lint

* feat: remove dep. between CLI and mopro-ffi by duplicating the constants def.

* chore: leftover
  • Loading branch information
KimiWu123 authored Jan 17, 2025
1 parent e424efd commit 7c441b2
Show file tree
Hide file tree
Showing 10 changed files with 530 additions and 315 deletions.
28 changes: 15 additions & 13 deletions cli/src/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use anyhow::Error;
use anyhow::Ok;
use anyhow::Result;
use dialoguer::theme::ColorfulTheme;
use dialoguer::Confirm;
Expand All @@ -10,9 +11,8 @@ use std::env;
use crate::config::read_config;
use crate::config::write_config;
use crate::constants::Adapter;
use crate::constants::Mode;
use crate::constants::Platform;
use crate::constants::MODES;
use crate::constants::PLATFORMS;
use crate::create::utils::copy_embedded_dir;
use crate::print::print_build_success_message;
use crate::style;
Expand Down Expand Up @@ -44,11 +44,11 @@ pub fn build_project(arg_mode: &Option<String>, arg_platforms: &Option<Vec<Strin
let mut config = read_config(&config_path)?;

// Mode selection, select `release` or `debug`
let mode: String = match arg_mode.as_deref() {
let mode: Mode = match arg_mode.as_deref() {
None => select_mode()?,
Some(m) => {
if MODES.contains(&m) {
m.to_string()
if Mode::all_strings().contains(&m) {
Mode::parse_from_str(m)
} else {
style::print_yellow("Invalid mode selected. Please choose a valid mode (e.g., 'release' or 'debug').".to_string());
select_mode()?
Expand All @@ -62,7 +62,7 @@ pub fn build_project(arg_mode: &Option<String>, arg_platforms: &Option<Vec<Strin
Some(p) => {
let valid_platforms: Vec<String> = p
.iter()
.filter(|&platform| PLATFORMS.contains(&platform.as_str()))
.filter(|&platform| Platform::all_strings().contains(&platform.as_str()))
.map(|platform| platform.to_owned())
.collect();

Expand Down Expand Up @@ -101,7 +101,8 @@ pub fn build_project(arg_mode: &Option<String>, arg_platforms: &Option<Vec<Strin
style::print_yellow(
"Web platform is not support Circom only, choose different platform".to_string(),
);
build_project(&Some(mode.clone()), &None)?;
build_project(&Some(mode.as_str().to_string()), &None)?;
return Ok(());
}

// Notification when the user selects the 'circom' adapter and includes the 'web' platform in the selection.
Expand All @@ -112,7 +113,8 @@ pub fn build_project(arg_mode: &Option<String>, arg_platforms: &Option<Vec<Strin
.interact()?;

if !confirm {
build_project(&Some(mode.clone()), &None)?;
build_project(&Some(mode.as_str().to_string()), &None)?;
return Ok(());
}

copy_mopro_wasm_lib()?;
Expand All @@ -137,7 +139,7 @@ pub fn build_project(arg_mode: &Option<String>, arg_platforms: &Option<Vec<Strin
let selected_architectures = platform.select_archs();

for p in platform.platforms.clone() {
let platform_str: &str = p.into();
let platform_str: &str = p.as_str();
let selected_arch = selected_architectures
.get(platform_str)
.map(|archs| archs.join(","))
Expand All @@ -147,7 +149,7 @@ pub fn build_project(arg_mode: &Option<String>, arg_platforms: &Option<Vec<Strin
.arg("run")
.arg("--bin")
.arg(platform_str)
.env("CONFIGURATION", mode.clone())
.env("CONFIGURATION", mode.as_str())
.env(p.arch_key(), selected_arch)
.status()?;

Expand All @@ -171,13 +173,13 @@ pub fn build_project(arg_mode: &Option<String>, arg_platforms: &Option<Vec<Strin
Ok(())
}

fn select_mode() -> Result<String> {
fn select_mode() -> Result<Mode> {
let idx = Select::with_theme(&ColorfulTheme::default())
.with_prompt("Build mode")
.items(&MODES)
.items(Mode::all_strings().as_ref())
.interact()?;

Ok(MODES[idx].to_owned())
Ok(Mode::from_idx(idx))
}

fn print_binding_message(platforms: &Vec<Platform>) -> anyhow::Result<()> {
Expand Down
Loading

0 comments on commit 7c441b2

Please sign in to comment.