chinese_rand/
lib.rs

1//! This crate is designed to instantiate random instances
2//! of the data structures implementing the [ChineseFormat](chinese_format::ChineseFormat) trait, made available by [chinese_format].
3//!
4//! The core concept is the [ChineseFormatGenerator] struct,
5//! which can be instantiated by passing an object implementing
6//! the [RawGenerator] trait:
7//!
8//! ```
9//! use chinese_rand::*;
10//! use chinese_format::{ChineseFormat, Fraction, Variant};
11//!
12//! # fn main() -> GenericResult<()> {
13//! let generator = ChineseFormatGenerator::new(
14//!     FastRandGenerator::new()
15//! );
16//!
17//! // Now setting the random seed just in order to
18//! //predict the generated values
19//! fastrand::seed(90);
20//!
21//! let fraction: Fraction = generator.fraction(
22//!     1..=10,
23//!     1..=10
24//! )?;
25//!
26//! let chinese = fraction.to_chinese(Variant::Simplified);
27//!
28//! assert_eq!(chinese, "六分之七");
29//!
30//! Ok(())
31//! }
32//! ```
33//!
34//! # Features
35//!
36//! - `fastrand`: enables [FastRandGenerator], based on [fastrand]. **Enabled by default**.
37//!
38//! - `digit-sequence`: enables random generation of data types - like [Decimal](chinese_format::Decimal) - based on [DigitSequence](digit_sequence::DigitSequence).
39//!
40//! - `currency`: enables the random generation of data types in the [currency](chinese_format::currency) module.
41//!
42//! - `gregorian`: enables the random generation of data types in the [gregorian](chinese_format::gregorian) module, which is dedicated to dates and times.
43//!
44//!   _Also enables_: `digit-sequence`.
45
46#[cfg(feature = "currency")]
47mod currency;
48#[cfg(feature = "digit-sequence")]
49mod digit_sequences;
50mod errors;
51#[cfg(feature = "gregorian")]
52pub mod gregorian;
53mod numeric;
54mod raw;
55
56use std::error::Error;
57
58#[cfg(feature = "currency")]
59pub use currency::*;
60pub use errors::*;
61pub use raw::*;
62
63/// The most generic [Error]-based [Result].
64pub type GenericResult<T> = Result<T, Box<dyn Error>>;
65
66/// Parametrically generates random instances of the data structures
67/// provided by [chinese_format].
68pub struct ChineseFormatGenerator {
69    pub(crate) raw_generator: Box<dyn RawGenerator>,
70}
71
72impl ChineseFormatGenerator {
73    /// Creating a [ChineseFormatGenerator] requires an object
74    /// implementing the [RawGenerator] interface.
75    ///
76    /// ```
77    /// use chinese_rand::*;
78    ///
79    /// // Now setting the random seed just in order to
80    /// //predict the generated values
81    /// fastrand::seed(90);
82    ///
83    /// let raw_generator = FastRandGenerator::new();
84    /// let generator = ChineseFormatGenerator::new(raw_generator);
85    ///
86    /// let integer = generator.integer(i128::MIN..=i128::MAX);
87    /// assert_eq!(integer, -139744823884027955216713073977120108615);
88    /// ```
89    pub fn new(raw_generator: impl RawGenerator + 'static) -> Self {
90        Self {
91            raw_generator: Box::new(raw_generator),
92        }
93    }
94}