Skip to main content

quo_rust/
lib.rs

1mod info;
2mod request;
3mod tests;
4mod types;
5
6use crate::info::time::get_time;
7use crate::request::make_request;
8use crate::types::{QuoPayload, QuoPayloadLanguage, QuoPayloadMeta, QuoPayloadVariable};
9use std::fmt::Debug;
10
11/// This fn creates a QuoPayload. You might or might not question why this is a separate function: for testing.
12///
13/// # Example
14///
15/// let mut big_number: i128;
16///
17/// big_number = 170141183460469231731687303715884105727;
18///
19/// quo_create_payload(big_number, "big_number", line!(), file!());
20///
21#[cfg(debug_assertions)]
22#[doc(hidden)]
23fn quo_create_payload<T: Debug>(
24    value: T,
25    name: &str,
26    line: u32,
27    file: &str,
28    is_mutable: bool,
29    package_name: &str,
30) -> QuoPayload {
31    let var_type = std::any::type_name_of_val(&value).to_string();
32    let value = format!("{:?}", value);
33
34    let (time_epoch_ms, uid) = get_time();
35
36    QuoPayload {
37        language: QuoPayloadLanguage::Rust,
38        meta: QuoPayloadMeta {
39            id: 0,
40            uid,
41            origin: package_name.to_string(),
42            sender_origin: format!("{}:{}", file, line),
43            time_epoch_ms,
44            variable: QuoPayloadVariable {
45                var_type: var_type.clone(),
46                name: name.to_string(),
47                value: value.clone(),
48                mutable: is_mutable,
49                is_constant: name == name.to_uppercase(),
50            },
51        },
52    }
53}
54
55/// This fn sends the provided variable to Quo.
56///
57/// # Example
58///
59/// let mut big_number: i128;
60///
61/// big_number = 170141183460469231731687303715884105727;
62///
63/// quo(big_number, "big_number", line!(), file!());
64///
65#[cfg(debug_assertions)]
66#[doc(hidden)]
67fn quo<T: Debug>(
68    value: T,
69    name: &str,
70    line: u32,
71    file: &str,
72    is_mutable: bool,
73    package_name: &str,
74) {
75    #[cfg(debug_assertions)]
76    {
77        let env_host = option_env!("QUO_HOST").unwrap_or("http://127.0.0.1");
78        let env_port = option_env!("QUO_PORT").unwrap_or("7312");
79
80        let send_fn = move || {
81            let body = quo_create_payload(value, name, line, file, is_mutable, package_name);
82            let quo_server_address = format!("{}:{}/payload", env_host, env_port);
83
84            make_request(&quo_server_address, body);
85        };
86
87        #[cfg(target_family = "wasm")]
88        {
89            send_fn();
90        }
91
92        #[cfg(not(target_family = "wasm"))]
93        {
94            // @TODO async
95            send_fn();
96        }
97    }
98}
99
100/// Use the `quo!()` macro and not this fn directly.
101#[cfg(debug_assertions)]
102#[doc(hidden)]
103pub fn __private_quo<T: Debug>(
104    value: T,
105    name: &str,
106    line: u32,
107    file: &str,
108    is_mutable: bool,
109    package_name: &str,
110) {
111    quo(value, name, line, file, is_mutable, package_name)
112}
113
114/// This macro sends the provided variable to Quo using the quo() fn.
115///
116/// # Example
117///
118/// let mut big_number: i128;
119///
120/// big_number = 170141183460469231731687303715884105727;
121///
122/// quo!(big_number);
123///
124#[macro_export]
125macro_rules! quo {
126    ($( mut $var:ident ),*) => {{
127        #[cfg(debug_assertions)]
128        $(
129            {
130                let __quo_package_name = option_env!("CARGO_PKG_NAME").unwrap_or("Rust project"); // Make sure we don't just get `quo-rust`.
131
132                #[cfg(target_family = "wasm")]
133                {
134                    let __quo_file_path = concat!(env!("CARGO_MANIFEST_DIR"), "/", file!());
135                    $crate::__private_quo(&$var, stringify!($var), line!(), &__quo_file_path, true, __quo_package_name);
136                }
137
138                #[cfg(not(target_family = "wasm"))]
139                {
140                    let __quo_path_buf = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(file!());
141                    let __quo_file_path = __quo_path_buf.to_str().unwrap_or(file!());
142                    $crate::__private_quo(&$var, stringify!($var), line!(), __quo_file_path, true, __quo_package_name);
143                }
144            }
145        )*
146    }};
147    ($( $var:ident ),*) => {{
148        #[cfg(debug_assertions)]
149        $(
150            {
151                let __quo_package_name = option_env!("CARGO_PKG_NAME").unwrap_or("Rust project"); // Make sure we don't just get `quo-rust`.
152
153                #[cfg(target_family = "wasm")]
154                {
155                    let __quo_file_path = concat!(env!("CARGO_MANIFEST_DIR"), "/", file!());
156                    $crate::__private_quo(&$var, stringify!($var), line!(), &__quo_file_path.to_owned(), false, __quo_package_name);
157                }
158
159                #[cfg(not(target_family = "wasm"))]
160                {
161                    let __quo_path_buf = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join(file!());
162                    let __quo_file_path = __quo_path_buf.to_str().unwrap_or(file!());
163                    $crate::__private_quo(&$var, stringify!($var), line!(), __quo_file_path, false, __quo_package_name);
164                }
165            }
166        )*
167    }};
168}