Skip to content

Commit a8f22ff

Browse files
committed
feat: add new_screen and enable/disable/enabled/disabled
add support for ray!().new_screen("Hello 🦀") and stuff around enable/disable
1 parent c52a62f commit a8f22ff

File tree

5 files changed

+195
-33
lines changed

5 files changed

+195
-33
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ray-rust"
3-
version = "0.1.3"
3+
version = "0.1.4"
44
edition = "2021"
55
authors = ["ALameLlama NicholasACiechanowski@gmail.com"]
66
description = "Spatie Ray in Rust"

src/lib.rs

Lines changed: 49 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ mod tests;
1111
macro_rules! ray {
1212
// If no arguments are passed, just create a new Ray instance
1313
() => {{
14-
Ray::new()
14+
let ray = Ray::new();
15+
16+
ray
1517
}};
1618
// If one or more arguments are passed, log them
1719
($($arg:expr),*) => {{
@@ -116,6 +118,7 @@ impl RayMeta {
116118

117119
pub struct Ray {
118120
request: RayPayload,
121+
is_enabled: bool,
119122
}
120123

121124
impl Ray {
@@ -126,10 +129,15 @@ impl Ray {
126129
payloads: vec![],
127130
meta: RayMeta::new(),
128131
},
132+
is_enabled: true,
129133
}
130134
}
131135

132136
pub fn send(&mut self) -> &mut Self {
137+
if !self.is_enabled {
138+
return self;
139+
}
140+
133141
let request = self.request.clone();
134142

135143
let client = reqwest::blocking::Client::new();
@@ -139,6 +147,14 @@ impl Ray {
139147
self
140148
}
141149

150+
pub fn die(&mut self, status: i32) {
151+
panic!("exited with code {}", status);
152+
153+
// TODO: I think we need to use process::exit here to actually exit the process since this
154+
// doesn't stop threaded work but I can't see a nice way to test this. I'm going to leave it
155+
// std::process::exit(status);
156+
}
157+
142158
pub fn clear_all(&mut self) -> &mut Self {
143159
let message = RayMessage::ClearAll(RayClearAll {
144160
label: RayMessageType::ClearAll,
@@ -155,6 +171,27 @@ impl Ray {
155171
self.send()
156172
}
157173

174+
pub fn new_screen(&mut self, name: Option<&str>) -> &mut Self {
175+
let message = RayMessage::NewScreen(RayNewScreen {
176+
label: RayMessageType::NewScreen,
177+
name: name.unwrap_or("").to_string(),
178+
});
179+
180+
let content = RayContent {
181+
content_type: RayNewScreen::get_type(),
182+
origin: RayOrigin::new(),
183+
content: message,
184+
};
185+
186+
self.request.payloads.push(content);
187+
188+
self.send()
189+
}
190+
191+
pub fn clear_screen(&mut self) -> &mut Self {
192+
self.new_screen(None)
193+
}
194+
158195
pub fn log(&mut self, values: Vec<String>) -> &mut Self {
159196
let message = RayMessage::Log(RayLog {
160197
label: RayMessageType::Log,
@@ -254,7 +291,7 @@ impl Ray {
254291
self.send()
255292
}
256293

257-
pub fn count(&mut self, name: &str) -> &mut Self {
294+
pub fn count(&mut self, _name: &str) -> &mut Self {
258295
// create a new counter hashmap with the name?
259296
// increment the counter hashmap with the name?
260297
// return a custom message with "called name x times"
@@ -270,24 +307,24 @@ impl Ray {
270307
todo!();
271308
}
272309

273-
pub fn die(&mut self, status: i32) {
274-
std::process::exit(status);
275-
}
276-
277310
pub fn disable(&mut self) -> &mut Self {
278-
todo!();
311+
self.is_enabled = false;
312+
313+
self
279314
}
280315

281-
pub fn disabled(&mut self) -> &mut Self {
282-
todo!();
316+
pub fn disabled(&mut self) -> bool {
317+
!self.is_enabled
283318
}
284319

285320
pub fn enable(&mut self) -> &mut Self {
286-
todo!();
321+
self.is_enabled = true;
322+
323+
self
287324
}
288325

289-
pub fn enabled(&mut self) -> &mut Self {
290-
todo!();
326+
pub fn enabled(&mut self) -> bool {
327+
self.is_enabled
291328
}
292329

293330
pub fn file(&mut self) -> &mut Self {
@@ -343,10 +380,6 @@ impl Ray {
343380
todo!();
344381
}
345382

346-
pub fn new_screen(&mut self) -> &mut Self {
347-
todo!();
348-
}
349-
350383
pub fn notify(&mut self) -> &mut Self {
351384
todo!();
352385
}

src/message.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ pub enum RayMessage {
1010
ClearAll(RayClearAll),
1111
Confetti(RayConfetti),
1212
Charles(RayCharles),
13+
NewScreen(RayNewScreen),
1314
}
1415

1516
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -20,6 +21,7 @@ pub enum RayMessageType {
2021
ClearAll,
2122
Confetti,
2223
Charles,
24+
NewScreen,
2325
}
2426

2527
#[derive(Debug, Serialize, Deserialize, Clone)]
@@ -29,6 +31,7 @@ pub enum RayContentType {
2931
Color,
3032
ClearAll,
3133
Confetti,
34+
NewScreen,
3235
}
3336

3437
impl RayContentType {
@@ -39,6 +42,7 @@ impl RayContentType {
3942
RayContentType::Color => "color".to_string(),
4043
RayContentType::ClearAll => "clear_all".to_string(),
4144
RayContentType::Confetti => "confetti".to_string(),
45+
RayContentType::NewScreen => "new_screen".to_string(),
4246
}
4347
}
4448
}
@@ -155,3 +159,16 @@ impl RayCharles {
155159
RayContentType::Custom.to_string()
156160
}
157161
}
162+
163+
// https://github.com/spatie/ray/blob/main/src/Payloads/NewScreenPayload.php
164+
#[derive(Debug, Serialize, Deserialize, Clone)]
165+
pub struct RayNewScreen {
166+
pub label: RayMessageType,
167+
pub name: String,
168+
}
169+
170+
impl RayNewScreen {
171+
pub fn get_type() -> String {
172+
RayContentType::NewScreen.to_string()
173+
}
174+
}

src/tests.rs

Lines changed: 127 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,146 @@
11
use super::*;
22

3-
// TODO: make these test actually test something,
4-
// this is just so I can see if they're coming into ray
3+
// TODO: Add mocking? look into the request body and add tests to ensure the correct data is being sent
54
#[test]
6-
fn test_ray_log() {
7-
ray!("Hello, Log!");
5+
fn test_ray_macro_no_args() {
6+
let ray = ray!();
7+
assert_eq!(ray.request.payloads.len(), 0);
88
}
99

1010
#[test]
11-
fn test_ray_text() {
12-
ray!().text("Hello, Text!");
11+
fn test_ray_macro_with_one_arg() {
12+
let ray = ray!("Hello, Ray Macro");
13+
assert_eq!(ray.request.payloads.len(), 1);
1314
}
1415

1516
#[test]
16-
fn test_ray_color() {
17-
ray!("Hello Color").color("green");
17+
fn test_ray_macro_with_multiple_args() {
18+
let ray = ray!("Hello", "Ray Macro");
19+
assert_eq!(ray.request.payloads.len(), 1);
1820
}
1921

2022
#[test]
21-
fn test_ray_html() {
22-
ray!().html("<strong>Hello, HTML!</strong>");
23+
#[should_panic(expected = "exited with code 1")]
24+
fn test_rd_marco_no_args() {
25+
std::panic::set_hook(Box::new(|panic_info| {
26+
if let Some(payload) = panic_info.payload().downcast_ref::<&str>() {
27+
if *payload == "exited with code 1" {
28+
std::process::exit(1);
29+
}
30+
}
31+
}));
32+
33+
let ray = rd!();
34+
assert_eq!(ray.request.payloads.len(), 0);
35+
}
36+
37+
#[test]
38+
#[should_panic(expected = "exited with code 1")]
39+
fn test_rd_macro_with_one_arg() {
40+
std::panic::set_hook(Box::new(|panic_info| {
41+
if let Some(payload) = panic_info.payload().downcast_ref::<&str>() {
42+
if *payload == "exited with code 1" {
43+
std::process::exit(1);
44+
}
45+
}
46+
}));
47+
48+
let ray = rd!("Hello, Rd Macro");
49+
assert_eq!(ray.request.payloads.len(), 1);
50+
}
51+
52+
#[test]
53+
#[should_panic(expected = "exited with code 1")]
54+
fn test_rd_macro_with_multiple_args() {
55+
std::panic::set_hook(Box::new(|panic_info| {
56+
if let Some(payload) = panic_info.payload().downcast_ref::<&str>() {
57+
if *payload == "exited with code 1" {
58+
std::process::exit(1);
59+
}
60+
}
61+
}));
62+
63+
let ray = rd!("Hello", "Rd Macro");
64+
assert_eq!(ray.request.payloads.len(), 1);
65+
}
66+
67+
#[test]
68+
fn test_ray_log_function() {
69+
let mut ray = Ray::new();
70+
ray.log(vec!["Hello, Log!".to_string()]);
71+
assert_eq!(ray.request.payloads.len(), 1);
72+
}
73+
74+
#[test]
75+
fn test_ray_text_function() {
76+
let mut ray = Ray::new();
77+
ray.text("Hello, Text!");
78+
assert_eq!(ray.request.payloads.len(), 1);
79+
}
80+
81+
#[test]
82+
fn test_ray_color_function() {
83+
let mut ray = Ray::new();
84+
ray.text("Hello, Color").color("green");
85+
assert_eq!(ray.request.payloads.len(), 2);
86+
}
87+
88+
#[test]
89+
fn test_ray_html_function() {
90+
let mut ray = Ray::new();
91+
ray.html("<strong>Hello, HTML!</strong>");
92+
assert_eq!(ray.request.payloads.len(), 1);
93+
}
94+
95+
#[test]
96+
fn test_ray_clear_all_function() {
97+
let mut ray = Ray::new();
98+
ray.clear_all();
99+
assert_eq!(ray.request.payloads.len(), 1);
100+
}
101+
102+
#[test]
103+
fn test_ray_confetti_function() {
104+
let mut ray = Ray::new();
105+
ray.confetti();
106+
assert_eq!(ray.request.payloads.len(), 1);
107+
}
108+
109+
#[test]
110+
fn test_ray_charles_function() {
111+
let mut ray = Ray::new();
112+
ray.charles();
113+
assert_eq!(ray.request.payloads.len(), 1);
114+
}
115+
116+
#[test]
117+
fn test_ray_new_screen_function() {
118+
let mut ray = Ray::new();
119+
ray.new_screen(None);
120+
assert_eq!(ray.request.payloads.len(), 1);
121+
}
122+
123+
#[test]
124+
fn test_ray_new_screen_with_name_function() {
125+
let mut ray = Ray::new();
126+
ray.new_screen(Some("Hello, New Screen!"));
127+
assert_eq!(ray.request.payloads.len(), 1);
23128
}
24129

25130
#[test]
26-
fn test_ray_clear_all() {
27-
ray!("Hello Clear");
28-
ray!().clear_all();
131+
fn test_ray_disable() {
132+
// TODO: Add a test to ensure the request is not sent
133+
let mut ray = Ray::new();
134+
ray.disable();
135+
assert_eq!(ray.disabled(), true);
136+
assert_eq!(ray.enabled(), false);
29137
}
30138

31139
#[test]
32-
fn test_ray_conffiti() {
33-
ray!().confetti();
140+
fn test_ray_enabled() {
141+
// TODO: Add a test to ensure the request is sent
142+
let mut ray = Ray::new();
143+
ray.enable();
144+
assert_eq!(ray.enabled(), true);
145+
assert_eq!(ray.disabled(), false);
34146
}

0 commit comments

Comments
 (0)