Skip to content

Commit

Permalink
Using the Fromstr trait for string operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Manush-2005 committed Nov 8, 2024
1 parent eebf651 commit 1ee5e0a
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions bin/sozo/src/commands/execute.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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: <CONTRACT_ADDRESS/TAG>,<ENTRYPOINT>,<ARG1>,<ARG2>,... (ex: dojo_examples:actions,execute,1,2)"
)]

pub calls: Vec<String>,
Expand All @@ -47,16 +48,31 @@ pub struct CallArgs {
pub calldata: Option<String>,
}

impl CallArgs {
fn from_string(s: &str) -> Result<Self> {
+impl std::str::FromStr for CallArgs {

type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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: <CONTRACT_NAME>,<ENTRYPOINT_NAME>,<ARG1>,<ARG2>,..."));
}
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 },
})
}
Expand Down

0 comments on commit 1ee5e0a

Please sign in to comment.