Skip to main content

Arguments

Enum Arguments 

Source
pub enum Arguments {
    None,
    Fixed(String),
    Range(String, String),
}
Expand description

Represents parsed arguments from faker pattern parameters.

Arguments are typically extracted from parentheses in faker patterns like:

  • faker.name.first_name()Arguments::None
  • faker.number.number(100)Arguments::Fixed("100")
  • faker.number.between(1,10)Arguments::Range("1", "10")
  • faker.date.between(2020-01-01..2024-12-31)Arguments::Range("2020-01-01", "2024-12-31")

§Examples

use jgd_rs::Arguments;

// Parse single value
let args = Arguments::from("(42)");
assert_eq!(args.get_number(0), 42);

// Parse range with comma
let args = Arguments::from("(1,10)");
let range = args.get_number_range(0, 100);
assert_eq!(range.start, 1);
assert_eq!(range.end, 10);

// Parse range with dots
let args = Arguments::from("(1..10)");
let (start, end) = args.get_string_tuple("0", "100");
assert_eq!(start, "1");
assert_eq!(end, "10");

Variants§

§

None

No arguments provided (empty parentheses or no parentheses)

§

Fixed(String)

Single fixed argument value

§

Range(String, String)

Range with start and end values (separated by comma or dots)

Implementations§

Source§

impl Arguments

Source

pub fn get_string<'a>(&'a self, default_value: &'a str) -> &'a str

Extracts a string value from the arguments.

Returns the first argument for Fixed and Range variants, or the default value for None variant.

§Arguments
  • default_value - Value to return if no arguments are present
§Returns
  • Arguments::Nonedefault_value
  • Arguments::Fixed(arg)arg
  • Arguments::Range(arg1, arg2)arg1
§Examples
use jgd_rs::Arguments;

let args = Arguments::from("(hello)");
assert_eq!(args.get_string("default"), "hello");

let args = Arguments::from("(start,end)");
assert_eq!(args.get_string("default"), "start");

let args = Arguments::None;
assert_eq!(args.get_string("default"), "default");
Source

pub fn get_string_tuple<'a>( &'a self, default_start: &'a str, default_end: &'a str, ) -> (&'a str, &'a str)

Extracts a tuple of string values from the arguments.

Returns both start and end values for different argument types.

§Arguments
  • default_start - Value to return as start if no arguments are present
  • default_end - Value to return as end if no arguments are present or only one argument
§Returns
  • Arguments::None(default_start, default_end)
  • Arguments::Fixed(arg)(arg, default_end)
  • Arguments::Range(arg1, arg2)(arg1, arg2)
§Examples
use jgd_rs::Arguments;

let args = Arguments::from("(1,10)");
let (start, end) = args.get_string_tuple("0", "100");
assert_eq!(start, "1");
assert_eq!(end, "10");

let args = Arguments::from("(5)");
let (start, end) = args.get_string_tuple("0", "100");
assert_eq!(start, "5");
assert_eq!(end, "100");
Source

pub fn get_number<T: FromStr>(&self, default_value: T) -> T

Extracts a numeric value from the arguments.

Attempts to parse the first argument as type T. If parsing fails or no arguments are present, returns the default value.

§Arguments
  • default_value - Value to return if no arguments are present or parsing fails
§Returns
  • Arguments::Nonedefault_value
  • Arguments::Fixed(arg) → parsed arg or default_value if parsing fails
  • Arguments::Range(arg1, arg2) → parsed arg1 or default_value if parsing fails
§Examples
use jgd_rs::Arguments;

let args = Arguments::from("(42)");
assert_eq!(args.get_number(0), 42);

let args = Arguments::from("(invalid)");
assert_eq!(args.get_number(100), 100);

let args = Arguments::from("(1,10)");
assert_eq!(args.get_number(0), 1);
Source

pub fn get_number_range<T: FromStr>( &self, default_start: T, default_end: T, ) -> Range<T>

Extracts a numeric range from the arguments.

Creates a Range<T> from the arguments, parsing both start and end values. If parsing fails, uses the corresponding default value.

§Arguments
  • default_start - Value to use as range start if no arguments or parsing fails
  • default_end - Value to use as range end if no second argument or parsing fails
§Returns
  • Arguments::Nonedefault_start..default_end
  • Arguments::Fixed(arg)parsed_arg..default_end
  • Arguments::Range(arg1, arg2)parsed_arg1..parsed_arg2
§Examples
use jgd_rs::Arguments;

let args = Arguments::from("(1,10)");
let range = args.get_number_range(0, 100);
assert_eq!(range.start, 1);
assert_eq!(range.end, 10);

let args = Arguments::from("(5)");
let range = args.get_number_range(0, 100);
assert_eq!(range.start, 5);
assert_eq!(range.end, 100);
Source

pub fn get_time(&self, default_value: OffsetDateTime) -> OffsetDateTime

Extracts a time value from the arguments.

Attempts to parse the first argument as a time value. If parsing fails or no arguments are present, returns the default value.

§Arguments
  • default_value - Value to return if no arguments are present or parsing fails
§Returns
  • Arguments::Nonedefault_value
  • Arguments::Fixed(arg) → parsed arg or default_value if parsing fails
  • Arguments::Range(arg1, arg2) → parsed arg1 or default_value if parsing fails
§Examples
use jgd_rs::Arguments;
use time::OffsetDateTime;

let default = OffsetDateTime::now_utc();
let args = Arguments::from("(1640995200)"); // Unix timestamp
let result = args.get_time(default);
Source

pub fn get_time_range( &self, default_start: OffsetDateTime, default_end: OffsetDateTime, ) -> (OffsetDateTime, OffsetDateTime)

Extracts a time range from the arguments.

Creates a tuple of time values from the arguments, parsing both start and end values. If parsing fails, uses the corresponding default value.

§Arguments
  • default_start - Value to use as start time if no arguments or parsing fails
  • default_end - Value to use as end time if no second argument or parsing fails
§Returns
  • Arguments::None(default_start, default_end)
  • Arguments::Fixed(arg)(parsed_arg, default_end)
  • Arguments::Range(arg1, arg2)(parsed_arg1, parsed_arg2)
§Examples
use jgd_rs::Arguments;
use time::OffsetDateTime;

let default_start = OffsetDateTime::now_utc();
let default_end = OffsetDateTime::now_utc();
let args = Arguments::from("(1640995200,1672531200)"); // Unix timestamps
let (start, end) = args.get_time_range(default_start, default_end);
Source

pub fn get_datetime(&self, default_value: DateTime<Utc>) -> DateTime<Utc>

Extracts a datetime value from the arguments.

Attempts to parse the first argument as a DateTime<Utc> using multiple formats. If parsing fails or no arguments are present, returns the default value.

§Arguments
  • default_value - Value to return if no arguments are present or parsing fails
§Returns
  • Arguments::Nonedefault_value
  • Arguments::Fixed(arg) → parsed arg or default_value if parsing fails
  • Arguments::Range(arg1, arg2) → parsed arg1 or default_value if parsing fails
§Examples
use jgd_rs::Arguments;
use chrono::{DateTime, Utc};

let default = DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z").unwrap().with_timezone(&Utc);
let args = Arguments::from("(2024-01-01T00:00:00Z)");
let result = args.get_datetime(default);
Source

pub fn get_datetime_range( &self, default_start: DateTime<Utc>, default_end: DateTime<Utc>, ) -> (DateTime<Utc>, DateTime<Utc>)

Extracts a datetime range from the arguments.

Creates a tuple of DateTime<Utc> values from the arguments, parsing both start and end values. If parsing fails, uses the corresponding default value.

§Arguments
  • default_start - Value to use as start datetime if no arguments or parsing fails
  • default_end - Value to use as end datetime if no second argument or parsing fails
§Returns
  • Arguments::None(default_start, default_end)
  • Arguments::Fixed(arg)(parsed_arg, default_end)
  • Arguments::Range(arg1, arg2)(parsed_arg1, parsed_arg2)
§Examples
use jgd_rs::Arguments;
use chrono::{DateTime, Utc};

let default_start = DateTime::parse_from_rfc3339("2020-01-01T00:00:00Z").unwrap().with_timezone(&Utc);
let default_end = DateTime::parse_from_rfc3339("2024-12-31T23:59:59Z").unwrap().with_timezone(&Utc);
let args = Arguments::from("(2024-01-01T00:00:00Z,2024-12-31T23:59:59Z)");
let (start, end) = args.get_datetime_range(default_start, default_end);

Trait Implementations§

Source§

impl Clone for Arguments

Source§

fn clone(&self) -> Arguments

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Arguments

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&str> for Arguments

Source§

fn from(value: &str) -> Self

Parses a string into Arguments enum.

Expects input in the format (content) where content can be:

  • Empty: ()Arguments::None
  • Single value: (42)Arguments::Fixed("42")
  • Comma-separated: (1,10)Arguments::Range("1", "10")
  • Dot-separated: (1..10)Arguments::Range("1", "10")
§Examples
use jgd_rs::Arguments;

let args = Arguments::from("(42)");
assert!(matches!(args, Arguments::Fixed(ref s) if s == "42"));

let args = Arguments::from("(1,10)");
assert!(matches!(args, Arguments::Range(ref s1, ref s2) if s1 == "1" && s2 == "10"));

let args = Arguments::from("(1..10)");
assert!(matches!(args, Arguments::Range(ref s1, ref s2) if s1 == "1" && s2 == "10"));

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> Fake for T

Source§

fn fake<U>(&self) -> U
where Self: FakeBase<U>,

Source§

fn fake_with_rng<U, R>(&self, rng: &mut R) -> U
where R: Rng + ?Sized, Self: FakeBase<U>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V