From 2a66e8b4cfcd055c590efd2718db066545521ae1 Mon Sep 17 00:00:00 2001 From: dusk Date: Tue, 21 Jan 2025 00:39:25 +0900 Subject: [PATCH] refactor(commands): remove general From array impl for tokens because it doesnt make sense --- crates/commands/src/commands.rs | 3 +++ crates/commands/src/main.rs | 2 +- crates/commands/src/token.rs | 27 ++++++++------------------- 3 files changed, 12 insertions(+), 20 deletions(-) diff --git a/crates/commands/src/commands.rs b/crates/commands/src/commands.rs index 8626a52a..eef0a9ec 100644 --- a/crates/commands/src/commands.rs +++ b/crates/commands/src/commands.rs @@ -39,10 +39,13 @@ impl Command { pub fn new(tokens: impl IntoIterator, cb: impl Into) -> Self { let tokens = tokens.into_iter().collect::>(); assert!(tokens.len() > 0); + // figure out which token to parse / put flags after + // (by default, put flags after the last token) let mut parse_flags_before = tokens.len(); let mut was_parameter = true; for (idx, token) in tokens.iter().enumerate().rev() { match token { + // we want flags to go before any parameters Token::Parameter(_, _) | Token::Any(_) => { parse_flags_before = idx; was_parameter = true; diff --git a/crates/commands/src/main.rs b/crates/commands/src/main.rs index 110915ea..6c722be3 100644 --- a/crates/commands/src/main.rs +++ b/crates/commands/src/main.rs @@ -16,7 +16,7 @@ fn main() { } } else { for command in cmds::all() { - println!("{}", command); + println!("{} - {}", command, command.help); } } } diff --git a/crates/commands/src/token.rs b/crates/commands/src/token.rs index 7a525312..c21ee16e 100644 --- a/crates/commands/src/token.rs +++ b/crates/commands/src/token.rs @@ -5,7 +5,7 @@ use std::{ sync::Arc, }; -use smol_str::{SmolStr, ToSmolStr}; +use smol_str::SmolStr; use crate::{parameter::Parameter, Parameter as FfiParam}; @@ -176,7 +176,13 @@ impl Display for Token { impl From<&str> for Token { fn from(value: &str) -> Self { - Token::Value(vec![value.to_smolstr()]) + Token::Value(vec![SmolStr::new(value)]) + } +} + +impl From<[&str; L]> for Token { + fn from(value: [&str; L]) -> Self { + Token::Value(value.into_iter().map(SmolStr::from).collect::>()) } } @@ -191,20 +197,3 @@ impl From<(ParamName, P)> for Token { Token::Parameter(value.0, Arc::new(value.1)) } } - -impl> From<[T; L]> for Token { - fn from(value: [T; L]) -> Self { - let tokens = value.into_iter().map(|s| s.into()).collect::>(); - if tokens.iter().all(|t| matches!(t, Token::Value(_))) { - let values = tokens - .into_iter() - .flat_map(|t| match t { - Token::Value(v) => v, - _ => unreachable!(), - }) - .collect::>(); - return Token::Value(values); - } - Token::Any(tokens) - } -}