Skip to content

Commit

Permalink
Alter deserialize of args with regex
Browse files Browse the repository at this point in the history
  • Loading branch information
wirednkod committed Sep 18, 2023
1 parent 6e6a102 commit 7254f22
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 15 deletions.
2 changes: 2 additions & 0 deletions crates/configuration/src/shared/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ pub const RW_FAILED: &str = "Should be able to read/write - failed. ";
pub const DEFAULT_TYPESTATE: &str = "'default' overriding should be ensured by typestate.";
pub const VALIDATION_CHECK: &str = "Validation failed. ";

pub const PREFIX_CANT_BE_NONE : &str = "Name prefix can't be None if a value exists.";

pub const THIS_IS_A_BUG: &str =
"This is a bug please report it: https://github.com/paritytech/zombienet-sdk/issues";
38 changes: 23 additions & 15 deletions crates/configuration/src/shared/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use regex::Regex;
use serde::{de, Deserialize, Deserializer, Serialize};
use url::Url;

use super::{constants::INFAILABLE, errors::ConversionError, resources::Resources};
use crate::shared::constants::{SHOULD_COMPILE, THIS_IS_A_BUG};
use super::{errors::ConversionError, resources::Resources};
use crate::shared::constants::{SHOULD_COMPILE, THIS_IS_A_BUG, INFAILABLE, PREFIX_CANT_BE_NONE};

/// An alias for a duration in seconds.
pub type Duration = u32;
Expand Down Expand Up @@ -355,21 +355,29 @@ impl<'de> de::Visitor<'de> for ArgVisitor {
where
E: de::Error,
{
if v.contains('=') || v.starts_with("--") || v.starts_with('-') {
if v.contains('=') {
let split: Vec<&str> = v.split('=').collect::<Vec<&str>>();
Ok(Arg::Option(split[0].to_string(), split[1].to_string()))
} else {
let split: Vec<&str> = v.split(' ').collect::<Vec<&str>>();
if split.len() == 1 {
Ok(Arg::Flag(v.to_string()))
} else {
Ok(Arg::Option(split[0].to_string(), split[1].to_string()))
}
let re = Regex::new("^(?<name_prefix>(?<prefix>-{1,2})(?<name>[a-zA-Z]+(-[a-zA-Z]+)*))((?<separator>=| )(?<value>.+))?$").unwrap();
let captures = re.captures(v);

if let Some(captures) = captures {
if let Some(value) = captures.name("value") {
return Ok(Arg::Option(
captures
.name("name_prefix")
.expect(&format!("{} {}", PREFIX_CANT_BE_NONE, THIS_IS_A_BUG))
.as_str()
.to_string(),
value.as_str().to_string(),
));
}

if let Some(name_prefix) = captures.name("name_prefix") {
return Ok(Arg::Flag(name_prefix.as_str().to_string()));
}
} else {
Ok(Arg::Flag(v.to_string()))
}

Err(de::Error::custom(
"the provided argument is invalid and doesn't match Arg::Option or Arg::Flag",
))
}
}

Expand Down

0 comments on commit 7254f22

Please sign in to comment.