ChineseFormatGenerator

Struct ChineseFormatGenerator 

Source
pub struct ChineseFormatGenerator { /* private fields */ }
Expand description

Parametrically generates random instances of the data structures provided by chinese_format.

Implementations§

Source§

impl ChineseFormatGenerator

Source

pub fn renminbi(&self, params: RenminbiParams) -> RenminbiCurrency

Creates a random instance of RenminbiCurrency.

use chinese_rand::*;
use chinese_format::{Variant, ChineseFormat, currency::{RenminbiCurrency, CurrencyStyle}};

let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);

fastrand::seed(90);
let currency: RenminbiCurrency = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Everyday { formal: true },
    yuan_range: 0..=500,
    include_dimes: true,
    include_cents: true
});
assert_eq!(
    currency.to_chinese(Variant::Simplified),
    "二百九十五元三角七分"
);

fastrand::seed(90);
let currency: RenminbiCurrency = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Everyday { formal: true },
    yuan_range: 0..=500,
    include_dimes: false,
    include_cents: false
});
assert_eq!(
    currency.to_chinese(Variant::Simplified),
    "二百九十五元"
);

fastrand::seed(90);
let currency: RenminbiCurrency = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Everyday { formal: true },
    yuan_range: 0..=500,
    include_dimes: true,
    include_cents: false
});
assert_eq!(
    currency.to_chinese(Variant::Simplified),
    "二百九十五元三角"
);

fastrand::seed(90);
let currency: RenminbiCurrency = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Everyday { formal: true },
    yuan_range: 0..=500,
    include_dimes: false,
    include_cents: true
});
assert_eq!(
    currency.to_chinese(Variant::Simplified),
    "二百九十五元三分"
);

fastrand::seed(90);
let currency: RenminbiCurrency = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Everyday { formal: false },
    yuan_range: 0..=500,
    include_dimes: true,
    include_cents: true
});
assert_eq!(
    currency.to_chinese(Variant::Simplified),
    "二百九十五块三毛七分"
);

fastrand::seed(90);
let currency: RenminbiCurrency = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Financial,
    yuan_range: 0..=500,
    include_dimes: true,
    include_cents: true
});
assert_eq!(
    currency.to_chinese(Variant::Simplified),
    "贰佰玖拾伍元叁角柒分整"
);

fastrand::seed(90);
let fixed_yuan = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Everyday { formal: true },
    yuan_range: 73..=73,
    include_dimes: true,
    include_cents: true
});
assert_eq!(
    fixed_yuan.to_chinese(Variant::Simplified),
    "七十三元三角七分"
);

fastrand::seed(90);
let zero = generator.renminbi(RenminbiParams {
    style: CurrencyStyle::Everyday { formal: true },
    yuan_range: 0..=0,
    include_dimes: false,
    include_cents: false
});
assert_eq!(
    zero.to_chinese(Variant::Simplified),
    "零元"
);

Required feature: currency.

Source§

impl ChineseFormatGenerator

Source

pub fn digit_sequence(&self, length_range: RangeInclusive<u8>) -> DigitSequence

Generates a random DigitSequence with length in the given range.

use chinese_rand::*;
use digit_sequence::DigitSequence;

fastrand::seed(90);
let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);

let sequence = generator.digit_sequence(0..=10);
assert_eq!(sequence, DigitSequence::from(3724260u32));

let fixed_length = generator.digit_sequence(5..=5);
assert_eq!(fixed_length, DigitSequence::from(85241u32));

let empty = generator.digit_sequence(0..=0);
assert_eq!(empty, DigitSequence::new());

Required feature: digit-sequence.

Source

pub fn decimal( &self, integer_range: RangeInclusive<IntegerPart>, fractional_length_range: RangeInclusive<u8>, ) -> Decimal

Generates a random Decimal.

use chinese_rand::*;
use chinese_format::Decimal;
use digit_sequence::DigitSequence;

fastrand::seed(90);
let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);

let decimal = generator.decimal(
    i128::MIN..=i128::MAX,
    0..=4
);
assert_eq!(decimal, Decimal {
    integer: -139744823884027955216713073977120108615,
    fractional: 242u8.into()
});

let fixed = generator.decimal(90..=90, 5..=5);
assert_eq!(fixed, Decimal {
    integer: 90,
    fractional: 85241u32.into()
});

let zero = generator.decimal(0..=0, 0..=0);
assert_eq!(zero, Decimal {
    integer: 0,
    fractional: DigitSequence::new()
});

Required feature: digit-sequence.

Source§

impl ChineseFormatGenerator

Source

pub fn gregorian(&self) -> GregorianGenerator<'_>

Creates a reusable GregorianGenerator instance, for generating date/time values according to the Gregorian calendar.

use chinese_rand::{*, gregorian::*};
use chinese_format::{ChineseFormat, Variant, gregorian::*};

let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);
let gregorian = generator.gregorian();

fastrand::seed(90);
let date = gregorian.date(DateParams {
    pattern: DatePattern::YearMonthDayWeekDay,
    year_range: Some(2000..=2019),
    formal: true,
    week_format: Some(WeekFormat::Zhou)
});
assert_eq!(
    date.to_chinese(Variant::Simplified),
    "二零一三年五月二十三号周一"
);

fastrand::seed(90);
let time = gregorian.linear_time(LinearTimeParams {
    day_part: true,
    include_second: true
});
assert_eq!(time.to_chinese(Variant::Simplified), "下午四点二十分四十三秒");

Required feature: gregorian.

Source§

impl ChineseFormatGenerator

Source

pub fn integer(&self, range: RangeInclusive<i128>) -> i128

Generates a random i128 in the given range.

use chinese_rand::*;

fastrand::seed(90);
let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);

let integer = generator.integer(i128::MIN..=i128::MAX);
assert_eq!(integer, -139744823884027955216713073977120108615);

let fixed = generator.integer(90..=90);
assert_eq!(fixed, 90);
Source

pub fn fraction( &self, denominator_range: RangeInclusive<u128>, numerator_range: RangeInclusive<i128>, ) -> Result<Fraction, InvalidLowerBound<u128>>

Generates a Fraction having its components in the given ranges.

The lower bound for the denominator cannot be 0, or the function will fail with InvalidLowerBound.

use chinese_rand::*;
use chinese_format::Fraction;

fastrand::seed(90);
let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);

let fraction = generator.fraction(
    1..=u128::MAX,
    i128::MIN..=i128::MAX
)?;
assert_eq!(
    fraction,
    Fraction::try_new(
        200537543036910508246661533454648102841,
        -116432671981703681494469200238719654801
    )?
);

let fixed = generator.fraction(5..=5, 4..=4)?;
assert_eq!(
    fixed,
    Fraction::try_new(5, 4)?
);

let fraction_result = generator.fraction(0..=5, 4..=4);
assert_eq!(
    fraction_result,
    Err(InvalidLowerBound(0))
);
Source

pub fn count(&self, range: RangeInclusive<CountBase>) -> Count

Generates a random Count in the given range.

use chinese_rand::*;
use chinese_format::{Count, CountBase};

fastrand::seed(90);
let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);

let count = generator.count(0..=CountBase::MAX);
assert_eq!(count, Count(200537543036910508246661533454648102841));

let fixed = generator.count(90..=90);
assert_eq!(fixed, Count(90));
Source§

impl ChineseFormatGenerator

Source

pub fn new(raw_generator: impl RawGenerator + 'static) -> Self

Creating a ChineseFormatGenerator requires an object implementing the RawGenerator interface.

use chinese_rand::*;

// Now setting the random seed just in order to
//predict the generated values
fastrand::seed(90);

let raw_generator = FastRandGenerator::new();
let generator = ChineseFormatGenerator::new(raw_generator);

let integer = generator.integer(i128::MIN..=i128::MAX);
assert_eq!(integer, -139744823884027955216713073977120108615);

Auto Trait Implementations§

Blanket Implementations§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

§

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

§

fn into(self) -> U

Calls U::from(self).

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

§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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

§

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

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

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

Performs the conversion.