diff --git a/cli/src/create.rs b/cli/src/create.rs index 6b21595b..a08b0f98 100644 --- a/cli/src/create.rs +++ b/cli/src/create.rs @@ -1,4 +1,5 @@ use std::env; +use std::fmt::Display; use std::path::PathBuf; use anyhow::Error; @@ -26,6 +27,7 @@ trait Create { fn print_message(); } +#[derive(Clone, PartialEq, Eq)] pub enum Framework { Ios, Android, @@ -34,6 +36,19 @@ pub enum Framework { ReactNative, } +impl Display for Framework { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let as_str = match self { + Framework::Ios => "ios", + Framework::Android => "android", + Framework::Web => "web", + Framework::Flutter => "flutter", + Framework::ReactNative => "react-native", + }; + write!(f, "{}", as_str) + } +} + impl From for Framework { fn from(app: String) -> Self { match app.to_lowercase().as_str() { @@ -59,23 +74,29 @@ impl From for &str { } } -const TEMPLATES: [&str; 5] = ["ios", "android", "web", "flutter", "react-native"]; - -pub fn create_project(arg_platform: &Option) -> anyhow::Result<()> { - let platform: String = match arg_platform.as_deref() { - None => select_template()?, +const FRAMEWORKS: [Framework; 5] = [ + Framework::Ios, + Framework::Android, + Framework::Web, + Framework::Flutter, + Framework::ReactNative, +]; + +pub fn create_project(arg_framework: &Option) -> anyhow::Result<()> { + let framework: String = match arg_framework.as_deref() { + None => select_framework()?, Some(m) => { - if TEMPLATES.contains(&m) { + if FRAMEWORKS.contains(&Framework::from(m.to_string())) { m.to_string() } else { style::print_yellow("Invalid template selected. Please choose a valid template (e.g., 'ios', 'android', 'web', 'react-native', 'flutter').".to_string()); - select_template()? + select_framework()? } } }; let project_dir = env::current_dir()?; - match platform.into() { + match framework.into() { Framework::Ios => Ios::create(project_dir)?, Framework::Android => Android::create(project_dir)?, Framework::Web => Web::create(project_dir)?, @@ -86,7 +107,7 @@ pub fn create_project(arg_platform: &Option) -> anyhow::Result<()> { Ok(()) } -fn select_template() -> anyhow::Result { +fn select_framework() -> anyhow::Result { let (items, unselectable) = get_target_platforms_with_status()?; let idx = Select::with_theme(&ColorfulTheme::default()) @@ -98,9 +119,9 @@ fn select_template() -> anyhow::Result { if unselectable[selected_idx] { style::print_yellow(format!( "Cannot create {} template - build binding first", - &TEMPLATES[selected_idx] + &FRAMEWORKS[selected_idx] )); - return select_template(); + return select_framework(); } Ok(items[selected_idx].to_owned()) // Only available items will be matched with 'platform' } else { @@ -115,9 +136,9 @@ fn get_target_platforms_with_status() -> anyhow::Result<(Vec, Vec) let mut items = Vec::new(); let mut unselectable = Vec::new(); - for &template in TEMPLATES.iter() { - match template { - "flutter" | "react-native" => { + for framework in FRAMEWORKS.iter() { + match framework { + Framework::Flutter | Framework::ReactNative => { // Adding more information to the list let requires = ["ios", "android"]; let missing: Vec<&str> = requires @@ -129,21 +150,21 @@ fn get_target_platforms_with_status() -> anyhow::Result<(Vec, Vec) if !missing.is_empty() { items.push(format!( "{:<12} - Requires {} binding(s)", - template, + framework, missing.join("/") )); unselectable.push(true); } else { - items.push(template.to_string()); + items.push(framework.to_string()); unselectable.push(false); } } _ => { - if config.target_platforms.contains(template) { - items.push(template.to_string()); + if config.target_platforms.contains(&framework.to_string()) { + items.push(framework.to_string()); unselectable.push(false); } else { - items.push(format!("{:<12} - Require binding", template)); + items.push(format!("{:<12} - Require binding", framework)); unselectable.push(true); } } diff --git a/cli/src/main.rs b/cli/src/main.rs index 3c40c6dd..e770dc2a 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -38,7 +38,7 @@ enum Commands { /// Create templates for the specified platform Create { #[arg(long, help = "Specify the platform")] - template: Option, + framework: Option, }, } @@ -57,7 +57,7 @@ fn main() { Ok(_) => {} Err(e) => style::print_red_bold(format!("Failed to build project: {:?}", e)), }, - Commands::Create { template } => match create::create_project(template) { + Commands::Create { framework } => match create::create_project(framework) { Ok(_) => {} Err(e) => style::print_red_bold(format!("Failed to create template: {:?}", e)), },