rusql_alchemy/utils.rs
1use std::any::type_name;
2
3/// Returns the name of the type `T` as a string.
4///
5/// # Arguments
6///
7/// * `value` - A value of type `T`.
8///
9/// # Returns
10///
11/// * A string slice that represents the name of the type `T`.
12///
13/// # Example
14///
15/// ```
16/// let type_name = get_type_name(42);
17/// assert_eq!(type_name, "i32");
18/// ```
19pub fn get_type_name<T: Sized>(_: T) -> &'static str {
20 type_name::<T>()
21}
22
23/// Converts a value into a JSON string.
24///
25/// # Arguments
26///
27/// * `value` - A value that can be converted into `serde_json::Value`.
28///
29/// # Returns
30///
31/// * A `String` representation of the JSON value.
32/// * If the value is a boolean, it converts `true` to `1` and `false` to `0`.
33///
34/// # Example
35///
36/// ```
37/// let json_string = to_string(true);
38/// assert_eq!(json_string, "1");
39///
40/// let json_string = to_string("Hello");
41/// assert_eq!(json_string, "\"Hello\"");
42/// ```
43pub fn to_string(value: impl Into<serde_json::Value>) -> String {
44 let json_value = value.into();
45 match json_value {
46 serde_json::Value::Bool(true) => serde_json::json!(1),
47 serde_json::Value::Bool(false) => serde_json::json!(0),
48 _ => json_value,
49 }
50 .to_string()
51}