Skip to main content

NumberSpec

Struct NumberSpec 

Source
pub struct NumberSpec {
    pub min: f64,
    pub max: f64,
    pub integer: bool,
}
Expand description

A specification for generating random numbers within a specified range.

NumberSpec defines constraints for number generation in JGD (JSON Generator Definition) schemas, including the minimum and maximum values, and whether the generated numbers should be integers or floating-point numbers.

This corresponds to the Number field type in the JGD schema specification.

§JGD Schema Representation

In a JGD schema, a number specification is represented as:

{
  "number": {
    "min": 0.0,
    "max": 100.0,
    "integer": true
  }
}

The integer field is optional and defaults to false if not specified.

§Examples

use jgd_rs::{NumberSpec, JsonGenerator, GeneratorConfig};
use serde_json::Value;

// Create a spec for integers between 1 and 100
let int_spec = NumberSpec {
    min: 1.0,
    max: 100.0,
    integer: true,
};

// Create a spec for floating-point numbers between 0.0 and 1.0
let float_spec = NumberSpec {
    min: 0.0,
    max: 1.0,
    integer: false,
};

Fields§

§min: f64

The minimum value (inclusive) for generated numbers.

This value defines the lower bound of the range for number generation. For integer generation, this will be cast to i64 during generation.

Maps to the min property in the JGD schema’s number specification.

§max: f64

The maximum value (inclusive) for generated numbers.

This value defines the upper bound of the range for number generation. For integer generation, this will be cast to i64 during generation.

Maps to the max property in the JGD schema’s number specification.

§integer: bool

Whether to generate integers instead of floating-point numbers.

When true, the generated numbers will be integers within the range [min as i64, max as i64]. When false (default), floating-point numbers will be generated within the range [min, max].

Maps to the optional integer property in the JGD schema’s number specification. Defaults to false when not specified in the schema.

Implementations§

Source§

impl NumberSpec

Source

pub fn new_float(min: f64, max: f64) -> Self

Creates a new NumberSpec for generating floating-point numbers.

This is a convenience constructor for creating number specifications that generate floating-point values, equivalent to the JGD schema:

{ "number": { "min": <min>, "max": <max>, "integer": false } }
§Arguments
  • min - The minimum value (inclusive) for generated numbers
  • max - The maximum value (inclusive) for generated numbers
§Returns

A new NumberSpec configured to generate floating-point numbers.

§Examples
use jgd_rs::NumberSpec;

let spec = NumberSpec::new_float(0.0, 1.0);
assert_eq!(spec.min, 0.0);
assert_eq!(spec.max, 1.0);
assert!(!spec.integer);
Source

pub fn new_integer(min: f64, max: f64) -> Self

Creates a new NumberSpec for generating integer numbers.

This is a convenience constructor for creating number specifications that generate integer values, equivalent to the JGD schema:

{ "number": { "min": <min>, "max": <max>, "integer": true } }
§Arguments
  • min - The minimum value (inclusive) for generated numbers
  • max - The maximum value (inclusive) for generated numbers
§Returns

A new NumberSpec configured to generate integer numbers.

§Examples
use jgd_rs::NumberSpec;

let spec = NumberSpec::new_integer(1.0, 100.0);
assert_eq!(spec.min, 1.0);
assert_eq!(spec.max, 100.0);
assert!(spec.integer);
Source

pub fn is_valid_range(&self) -> bool

Validates that the number specification has a valid range.

This is useful for validating JGD schema constraints before generation. A valid range requires that min is less than or equal to max.

§Returns

true if the range is valid for number generation, false otherwise.

§Examples
use jgd_rs::NumberSpec;

let valid_spec = NumberSpec::new_float(0.0, 10.0);
assert!(valid_spec.is_valid_range());

let invalid_spec = NumberSpec::new_float(10.0, 0.0);
assert!(!invalid_spec.is_valid_range());

let single_value_spec = NumberSpec::new_integer(5.0, 5.0);
assert!(single_value_spec.is_valid_range());
Source

pub fn range_size(&self) -> f64

Returns the size of the numeric range.

This can be useful for understanding the distribution space of the number generation within the JGD schema context.

§Returns

The difference between max and min. Returns 0.0 if the range is invalid.

§Examples
use jgd_rs::NumberSpec;

let spec = NumberSpec::new_float(0.0, 10.0);
assert_eq!(spec.range_size(), 10.0);

let single_value = NumberSpec::new_integer(5.0, 5.0);
assert_eq!(single_value.range_size(), 0.0);
Source

pub fn integer_count(&self) -> u64

Returns the number of possible integer values in the range.

This method is useful for understanding the cardinality of possible integer values when integer is true in the JGD schema specification.

§Returns

The number of possible integer values, or 0 if the range is invalid.

§Examples
use jgd_rs::NumberSpec;

let spec = NumberSpec::new_integer(1.0, 5.0);
assert_eq!(spec.integer_count(), 5); // 1, 2, 3, 4, 5

let single = NumberSpec::new_integer(10.0, 10.0);
assert_eq!(single.integer_count(), 1); // just 10

Trait Implementations§

Source§

impl Clone for NumberSpec

Source§

fn clone(&self) -> NumberSpec

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 NumberSpec

Source§

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

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

impl<'de> Deserialize<'de> for NumberSpec

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl JsonGenerator for NumberSpec

Source§

fn generate( &self, config: &mut GeneratorConfig, local_config: Option<&mut LocalConfig>, ) -> Result<Value, JgdGeneratorError>

Generates a random number according to the JGD number specification.

This method produces either an integer or floating-point number within the specified range, depending on the integer field of the NumberSpec. The generated value will be serialized as a JSON number in the output.

§Arguments
  • config - A mutable reference to the generator configuration containing the random number generator and other generation context.
§Returns

A serde_json::Value::Number containing either:

  • An integer value (when integer is true) within [min as i64, max as i64]
  • A floating-point value (when integer is false) within [min, max]
§Schema Compliance

This implementation ensures the generated values conform to the JGD schema’s number specification requirements:

  • Respects the min and max bounds (inclusive)
  • Generates integers when integer is true
  • Generates floating-point numbers when integer is false
§Examples
use jgd_rs::{NumberSpec, JsonGenerator, GeneratorConfig};
use serde_json::Value;

let mut config = GeneratorConfig::new("EN", Some(42));

// Generate integers between 1 and 10 (as per JGD schema)
let int_spec = NumberSpec {
    min: 1.0,
    max: 10.0,
    integer: true,
};
let value = int_spec.generate(&mut config, None).unwrap();
if let Value::Number(n) = value {
    assert!(n.is_i64());
    let int_val = n.as_i64().unwrap();
    assert!((1..=10).contains(&int_val));
}

// Generate floats between 0.0 and 1.0 (as per JGD schema)
let float_spec = NumberSpec {
    min: 0.0,
    max: 1.0,
    integer: false,
};
let value = float_spec.generate(&mut config, None).unwrap();
if let Value::Number(n) = value {
    let float_val = n.as_f64().unwrap();
    assert!((0.0..=1.0).contains(&float_val));
}
§Panics

This method may panic if:

  • The random number generator fails to generate a value in the specified range
  • Integer conversion fails when integer is true and the range exceeds i64 bounds
§Performance Notes
  • Integer generation is slightly faster than floating-point generation
  • Range size does not significantly impact generation performance

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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,