1use crate::common::parse_arg;
2use crate::error::unsupported_arg;
3use rand::distr::{Alphanumeric, SampleString, StandardUniform};
4use rand::rng;
5use std::collections::HashMap;
6use tera::{to_value, Result, Value};
7
8pub fn random_string(args: &HashMap<String, Value>) -> Result<Value> {
41 let str_length: usize = parse_arg(args, "length")?.unwrap_or(8usize);
42
43 let space_as_string: String =
44 parse_arg(args, "space")?.unwrap_or_else(|| String::from("alphanumeric"));
45
46 let random_string: String = match space_as_string.as_str() {
47 "alphanumeric" => Ok(Alphanumeric.sample_string(&mut rng(), str_length)),
48 "standard-uniform" => Ok(StandardUniform.sample_string(&mut rng(), str_length)),
49 _ => Err(unsupported_arg("space", space_as_string)),
50 }?;
51 let json_value: Value = to_value(random_string)?;
52 Ok(json_value)
53}
54
55#[cfg(test)]
56mod tests {
57 use crate::common::tests::test_tera_rand_function;
58 use crate::string::*;
59 use tracing_test::traced_test;
60
61 #[test]
62 #[traced_test]
63 fn test_random_string() {
64 test_tera_rand_function(
65 random_string,
66 "random_string",
67 r#"{ "some_field": "{{ random_string() }}" }"#,
68 r#"\{ "some_field": "[\w\d]{8}" }"#,
69 );
70 }
71
72 #[test]
73 #[traced_test]
74 fn test_random_string_with_custom_length() {
75 test_tera_rand_function(
76 random_string,
77 "random_string",
78 r#"{ "some_field": "{{ random_string(length=12) }}" }"#,
79 r#"\{ "some_field": "[\w\d]{12}" }"#,
80 );
81 }
82
83 #[test]
84 #[traced_test]
85 fn test_random_string_with_alphanumeric_space() {
86 test_tera_rand_function(
87 random_string,
88 "random_string",
89 r#"{ "some_field": "{{ random_string(space="alphanumeric") }}" }"#,
90 r#"\{ "some_field": "[\w\d]{8}" }"#,
91 );
92 }
93
94 #[test]
95 #[traced_test]
96 fn test_random_string_with_standard_space() {
97 test_tera_rand_function(
98 random_string,
99 "random_string",
100 r#"{ "some_field": "{{ random_string(space="standard-uniform") }}" }"#,
101 r#"\{ "some_field": ".{8}" }"#,
102 );
103 }
104
105 #[test]
106 #[traced_test]
107 fn test_random_string_with_standard_space_and_custom_length() {
108 test_tera_rand_function(
109 random_string,
110 "random_string",
111 r#"{ "some_field": "{{ random_string(space="standard-uniform", length=12) }}" }"#,
112 r#"\{ "some_field": ".{12}" }"#,
113 );
114 }
115}