pub struct ChineseFormatGenerator { /* private fields */ }Expand description
Parametrically generates random instances of the data structures provided by chinese_format.
Implementations§
Source§impl ChineseFormatGenerator
impl ChineseFormatGenerator
Sourcepub fn renminbi(&self, params: RenminbiParams) -> RenminbiCurrency
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
impl ChineseFormatGenerator
Sourcepub fn digit_sequence(&self, length_range: RangeInclusive<u8>) -> DigitSequence
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.
Sourcepub fn decimal(
&self,
integer_range: RangeInclusive<IntegerPart>,
fractional_length_range: RangeInclusive<u8>,
) -> Decimal
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
impl ChineseFormatGenerator
Sourcepub fn gregorian(&self) -> GregorianGenerator<'_>
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
impl ChineseFormatGenerator
Sourcepub fn integer(&self, range: RangeInclusive<i128>) -> i128
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);Sourcepub fn fraction(
&self,
denominator_range: RangeInclusive<u128>,
numerator_range: RangeInclusive<i128>,
) -> Result<Fraction, InvalidLowerBound<u128>>
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))
);
Sourcepub fn count(&self, range: RangeInclusive<CountBase>) -> Count
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
impl ChineseFormatGenerator
Sourcepub fn new(raw_generator: impl RawGenerator + 'static) -> Self
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);