tera_rand/
lib.rs

1//! # tera-rand
2//!
3//! A crate of random data generation functions for the [Tera] template engine.
4//!
5//! For a CLI tool implementation using this crate, see [tera-rand-cli].
6//!
7//! # Example usage
8//! Import the function you would like to use, and register it with [`Tera::register_function`].
9//! Your Tera templates may then use that function:
10//!
11//! ```edition2021
12//! use tera::{Context, Tera};
13//! use tera_rand::random_string;
14//! use tera_rand::random_ipv4_cidr;
15//!
16//! let mut tera: Tera = Tera::default();
17//! tera.register_function("random_string", random_string);
18//! tera.register_function("random_ipv4_cidr", random_ipv4_cidr);
19//!
20//! let context: Context = Context::new();
21//! // generate a random String and a random IPv4 CIDR address in a JSON template string
22//! let rendered_json: String = tera
23//!     .render_str(
24//!         r#"{"hostname": "{{ random_string() }}", "subnet": "{{ random_ipv4_cidr() }}"}"#,
25//!         &context
26//!     )
27//!     .unwrap();
28//! ```
29//!
30//! Tera takes the template String, inserts random values wherever the functions were called, and
31//! spits out a rendered String, which in this case might look like this:
32//! ```json
33//! {"hostname": "VtNCOgwH", "subnet": "196.119.240.0/23"}
34//! ```
35//!
36//! Some functions provide customization parameters. For example, [`random_string`] provides a
37//! `length` parameter to specify the length of the generated String, and [`random_ipv4_cidr`]
38//! provides `length_start` and `length_end` parameters to limit the possible prefix lengths. The
39//! template in the above example could be modified to this:
40//! ```json
41//! {
42//!     "hostname": "{{ random_string(length=12) }}",
43//!     "subnet": "{{ random_ipv4_cidr(length_start=28, length_end=30) }}"
44//! }
45//! ```
46//! and the generated JSON could look like this:
47//! ```json
48//! {
49//!     "hostname": "YCHcsV6bRkVW",
50//!     "subnet": "171.150.226.224/29"
51//! }
52//! ```
53//!
54//! [Tera]: https://github.com/Keats/tera
55//! [tera-rand-cli]: https://docs.rs/tera-rand-cli
56//! [`Tera::register_function`]: https://docs.rs/tera/latest/tera/struct.Tera.html#method.register_function
57//! [`random_string`]: crate::random_string
58//! [`random_ipv4_cidr`]: crate::random_ipv4_cidr
59#![warn(missing_debug_implementations, missing_docs)]
60
61mod common;
62mod error;
63
64// public functions live in separate modules for maintainability,
65// but expose them in the root module for searchability
66
67mod file;
68pub use file::*;
69
70mod net;
71pub use net::*;
72
73mod primitives;
74pub use primitives::*;
75
76mod string;
77pub use string::*;
78
79mod uuid;
80pub use crate::uuid::*;