From 1ee5e0adb13d2859a2251b095cf9204c00dfc7e9 Mon Sep 17 00:00:00 2001 From: Manush Sanchela Date: Fri, 8 Nov 2024 15:53:02 +0530 Subject: [PATCH] Using the Fromstr trait for string operations --- bin/sozo/src/commands/execute.rs | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/bin/sozo/src/commands/execute.rs b/bin/sozo/src/commands/execute.rs index a3726c475e..c43ca8f559 100644 --- a/bin/sozo/src/commands/execute.rs +++ b/bin/sozo/src/commands/execute.rs @@ -1,5 +1,6 @@ use anyhow::{anyhow, Result}; use clap::Args; +use std::str::FromStr; use dojo_types::naming; use dojo_utils::{Invoker, TxnConfig}; use scarb::core::Config; @@ -22,7 +23,7 @@ use crate::utils; pub struct ExecuteArgs { #[arg( - help = "The address or the tag (ex: dojo_examples:actions) of the contract to be executed." + help = "List of calls to execute. Each call should be in format: ,,,,... (ex: dojo_examples:actions,execute,1,2)" )] pub calls: Vec, @@ -47,16 +48,31 @@ pub struct CallArgs { pub calldata: Option, } -impl CallArgs { - fn from_string(s: &str) -> Result { ++impl std::str::FromStr for CallArgs { + + type Err = anyhow::Error; + + fn from_str(s: &str) -> Result { + let s = s.trim(); + if s.is_empty() { + return Err(anyhow!("Empty call string")); + } + let parts: Vec<&str> = s.split(',').collect(); if parts.len() < 2 { return Err(anyhow!("Invalid call format. Expected format: ,,,,...")); } + let entrypoint = parts[1].trim(); + if entrypoint.is_empty() { + return Err(anyhow!("Empty entrypoint")); + } + if !entrypoint.chars().all(|c| c.is_ascii_alphanumeric() || c == '_') { + return Err(anyhow!("Invalid entrypoint format. Must contain only alphanumeric characters and underscores")); + } Ok(CallArgs { tag_or_address: parts[0].parse()?, - entrypoint: parts[1].to_string(), + entrypoint: entrypoint.to_string(), calldata: if parts.len() > 2 { Some(parts[2..].join(",")) } else { None }, }) }