1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use rustc_version::version_meta;
use serde::{Deserialize, Serialize};

mod message;
use message::*;

#[cfg(test)]
mod tests;

#[macro_export]
macro_rules! ray {
    // If no arguments are passed, just create a new Ray instance
    () => {{
        let ray = Ray::new();

        ray
    }};
    // If one or more arguments are passed, log them
    ($($arg:expr),*) => {{
        let mut ray = Ray::new();
        let mut vec = Vec::new();

        // Push each argument to the vector
        $(vec.push(format!("{:#?}", $arg));)*

        ray.log(vec);

        ray
    }};
}

#[macro_export]
macro_rules! rd {
    () => {{
        let mut ray = Ray::new();

        ray.die(1);

        ray
    }};
    ($($arg:expr),*) => {{
        let mut ray = Ray::new();
        let mut vec = Vec::new();

        $(vec.push(format!("{:#?}", $arg));)*

        ray.log(vec);

        ray.die(1);

        ray
    }};
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RayPayload {
    uuid: String,
    payloads: Vec<RayContent>,
    meta: RayMeta,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RayContent {
    #[serde(rename = "type")] // rename the field to "type" since it's a reserved keyword
    content_type: String,
    content: RayMessage,
    origin: RayOrigin,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RayOrigin {
    function_name: String,
    file: String,
    line_number: u32,
    hostname: String,
}

impl RayOrigin {
    pub fn new() -> Self {
        // TODO: Get the function name, file, and line number from the macro
        // I don't think we can get the function name, file, and line number from the macro, at
        // least not easily. So we'll just set them to empty strings and 0 for now.
        let function_name = "ray".to_string();
        let file = "".to_string();
        let line_number = 0;
        let hostname = "localhost".to_string();

        Self {
            function_name,
            file,
            line_number,
            hostname,
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct RayMeta {
    // TODO: See if we can get more useful information, edition? etc.
    // I don't even what if this shows up on ray, but I'm going to leave it here for now
    rustc_version: String,
    package_version: String,
}

impl RayMeta {
    pub fn new() -> Self {
        let rustc_version = match version_meta() {
            Ok(meta) => meta.short_version_string,
            Err(_) => "🤷".to_string(),
        };

        Self {
            rustc_version,
            package_version: env!("CARGO_PKG_VERSION").to_string(),
        }
    }
}

pub struct Ray {
    request: RayPayload,
    is_enabled: bool,
}

impl Ray {
    pub fn new() -> Self {
        Self {
            request: RayPayload {
                uuid: uuid::Uuid::new_v4().to_string(),
                payloads: vec![],
                meta: RayMeta::new(),
            },
            is_enabled: true,
        }
    }

    pub fn send(&mut self) -> &mut Self {
        if !self.is_enabled {
            return self;
        }

        let request = self.request.clone();

        let client = reqwest::blocking::Client::new();

        let _ = client.post("http://localhost:23517").json(&request).send();

        self
    }

    pub fn die(&mut self, status: i32) {
        panic!("exited with code {}", status);

        // TODO: I think we need to use process::exit here to actually exit the process since this
        // doesn't stop threaded work but I can't see a nice way to test this. I'm going to leave it
        // std::process::exit(status);
    }

    pub fn clear_all(&mut self) -> &mut Self {
        let message = RayMessage::ClearAll(RayClearAll {
            label: RayMessageType::ClearAll,
        });

        let content = RayContent {
            content_type: RayClearAll::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn new_screen(&mut self, name: Option<&str>) -> &mut Self {
        let message = RayMessage::NewScreen(RayNewScreen {
            label: RayMessageType::NewScreen,
            name: name.unwrap_or("").to_string(),
        });

        let content = RayContent {
            content_type: RayNewScreen::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn clear_screen(&mut self) -> &mut Self {
        self.new_screen(None)
    }

    pub fn log(&mut self, values: Vec<String>) -> &mut Self {
        let message = RayMessage::Log(RayLog {
            label: RayMessageType::Log,
            values,
        });

        let content = RayContent {
            content_type: RayLog::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn text(&mut self, value: &str) -> &mut Self {
        let message = RayMessage::Text(RayText {
            label: RayMessageType::Text,
            content: value.to_string(),
        });

        let content = RayContent {
            content_type: RayText::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn color(&mut self, value: &str) -> &mut Self {
        let message = RayMessage::Color(RayColor {
            color: RayColors::from(value.to_string()),
        });

        let content = RayContent {
            content_type: RayColor::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn html(&mut self, value: &str) -> &mut Self {
        let message = RayMessage::HTML(RayHtml {
            label: RayMessageType::HTML,
            content: value.to_string(),
        });

        let content = RayContent {
            content_type: RayHtml::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn confetti(&mut self) -> &mut Self {
        let message = RayMessage::Confetti(RayConfetti {
            label: RayMessageType::Confetti,
        });

        let content = RayContent {
            content_type: RayConfetti::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn charles(&mut self) -> &mut Self {
        let message = RayMessage::Charles(RayCharles {
            content: "🎶 🎹 🎷 🕺".to_string(),
        });

        let content = RayContent {
            content_type: RayCharles::get_type(),
            origin: RayOrigin::new(),
            content: message,
        };

        self.request.payloads.push(content);

        self.send()
    }

    pub fn count(&mut self, _name: &str) -> &mut Self {
        // create a new counter hashmap with the name?
        // increment the counter hashmap with the name?
        // return a custom message with "called name x times"

        todo!();
    }

    pub fn counter_value(&mut self) -> &mut Self {
        todo!();
    }

    pub fn clear_counters(&mut self) -> &mut Self {
        todo!();
    }

    pub fn disable(&mut self) -> &mut Self {
        self.is_enabled = false;

        self
    }

    pub fn disabled(&mut self) -> bool {
        !self.is_enabled
    }

    pub fn enable(&mut self) -> &mut Self {
        self.is_enabled = true;

        self
    }

    pub fn enabled(&mut self) -> bool {
        self.is_enabled
    }

    pub fn file(&mut self) -> &mut Self {
        todo!();
    }

    pub fn gray(&mut self) -> &mut Self {
        todo!();
    }

    pub fn green(&mut self) -> &mut Self {
        todo!();
    }

    pub fn hide(&mut self) -> &mut Self {
        todo!();
    }

    pub fn hide_app(&mut self) -> &mut Self {
        todo!();
    }

    pub fn image(&mut self) -> &mut Self {
        todo!();
    }

    // I'm not sure if this is possible in Rust
    pub fn r#if(&mut self) -> &mut Self {
        todo!();
    }

    pub fn json(&mut self) -> &mut Self {
        todo!();
    }

    pub fn label(&mut self) -> &mut Self {
        todo!();
    }

    pub fn large(&mut self) -> &mut Self {
        todo!();
    }

    pub fn limit(&mut self) -> &mut Self {
        todo!();
    }

    pub fn link(&mut self) -> &mut Self {
        todo!();
    }

    pub fn measure(&mut self) -> &mut Self {
        todo!();
    }

    pub fn notify(&mut self) -> &mut Self {
        todo!();
    }

    pub fn orange(&mut self) -> &mut Self {
        todo!();
    }

    pub fn pass(&mut self) -> &mut Self {
        todo!();
    }

    pub fn pause(&mut self) -> &mut Self {
        todo!();
    }

    pub fn info(&mut self) -> &mut Self {
        todo!();
    }

    pub fn purple(&mut self) -> &mut Self {
        todo!();
    }

    // TODO: This has 3 functions max, per_second and clear
    pub fn rate_limiter(&mut self) -> &mut Self {
        todo!();
    }

    pub fn red(&mut self) -> &mut Self {
        todo!();
    }

    pub fn separator(&mut self) -> &mut Self {
        todo!();
    }

    pub fn show_app(&mut self) -> &mut Self {
        todo!();
    }

    pub fn small(&mut self) -> &mut Self {
        todo!();
    }

    pub fn table(&mut self) -> &mut Self {
        todo!();
    }

    pub fn to_json(&mut self) -> &mut Self {
        todo!();
    }

    pub fn trace(&mut self) -> &mut Self {
        todo!();
    }

    pub fn url(&mut self) -> &mut Self {
        todo!();
    }

    pub fn xml(&mut self) -> &mut Self {
        todo!();
    }
}