Using Ray with Rust | ray-rust | Spatie      

 SPATIE  

ray-rust
========

spatie.be/open-source

  [Docs](https://spatie.be/docs)  [Ray-rust](https://spatie.be/docs/ray-rust/v1)  Using Ray with Rust

 Version   v1      

 Other versions for crawler [v1](https://spatie.be/docs/ray-rust/v1) 

  Using Ray with Rust    
- [ Introduction ](https://spatie.be/docs/ray-rust/v1/introduction)
- [ Support us ](https://spatie.be/docs/ray-rust/v1/support-us)
- [ Installation ](https://spatie.be/docs/ray-rust/v1/installation-and-setup)
- [ Using Ray with Rust ](https://spatie.be/docs/ray-rust/v1/usage)
- [ Configuration ](https://spatie.be/docs/ray-rust/v1/configuration)
- [ PHP compatibility ](https://spatie.be/docs/ray-rust/v1/compatibility)
- [ Testing ](https://spatie.be/docs/ray-rust/v1/testing)
- [ Changelog ](https://spatie.be/docs/ray-rust/v1/changelog)

 Using Ray with Rust
===================

###  On this page 

1. [ Basic output ](#content-basic-output)
2. [ Customizing output ](#content-customizing-output)
3. [ Output formats ](#content-output-formats)
4. [ Inspecting values ](#content-inspecting-values)
5. [ Caller &amp;amp; stack trace ](#content-caller--stack-trace)
6. [ Measuring &amp;amp; counting ](#content-measuring--counting)
7. [ Limiting output ](#content-limiting-output)
8. [ Conditional output ](#content-conditional-output)
9. [ Handling exceptions ](#content-handling-exceptions)
10. [ Updating &amp;amp; removing items ](#content-updating--removing-items)
11. [ Execution control ](#content-execution-control)
12. [ Flow helpers ](#content-flow-helpers)
13. [ Custom functions ](#content-custom-functions)
14. [ Managing the Ray app ](#content-managing-the-ray-app)
15. [ Fun stuff ](#content-fun-stuff)

This page covers how to use Ray with Rust. There are many different methods available, and this page attempts to show most of them. It follows the [PHP usage guide](https://myray.app/docs/php/vanilla-php/usage). See the [API reference](https://docs.rs/spatie-ray) for a full list of methods.

Basic output
--------------------------------------------------------------------------------------------

To display something in Ray, use the `ray!` macro. It accepts many different data types: strings, arrays, structs, and more.

```
use spatie_ray::{ray, json};

ray!("Hello world")?;
ray!(json!({"a": 1, "b": 2}))?.color("red")?;
ray!(an_object)?;
```

The `ray!` macro accepts multiple arguments. These are displayed as separate messages in the same entry.

```
ray!("as", "many", "arguments", "as", "you", "like")?;
```

Values must implement `serde::Serialize`. Derive it on your own types, or use `ray!().debug(&value)` for a type implementing `Debug`. Rust arrays, structs and floating-point values use Ray's native JSON renderer instead of PHP's reflection-based HTML dump. [Compatibility](compatibility) records the differences.

Customizing output
--------------------------------------------------------------------------------------------------------------

Ray has some useful methods that you can use to add more context or differentiate messages from one another.

### Expanding arrays and objects

Ray's `expand` command opens nodes in compatible renderers. Pass a depth to `expand`, or use `expand_keys` for names and dot notation. These commands are primarily intended for custom payloads that use Ray's expandable HTML renderer; native JSON expansion is controlled by the app.

```
ray!(&array_or_object)?.expand(1)?;
ray!(&array_or_object)?.expand(3)?;
ray!(&array_or_object)?.expand_keys(&["myKey", "anotherKey"])?;
ray!(&array_or_object)?.expand_keys(&["myKey.nestedKey"])?;
ray!(&array_or_object)?.expand_all()?;
```

### Using colors

You can colorize things you sent to Ray by using one of the color functions.

```
ray!("this is green")?.green()?;
ray!("this is yellow")?.yellow()?;
ray!("this is red")?.red()?;
ray!("this is blue")?.blue()?;
ray!("this is purple")?.purple()?;
ray!("this is gray")?.gray()?;
```

Like PHP, `yellow()` sends the `orange` color. `grey()` is an alias for `gray()`.

### Using sizes

Ray can display things in different sizes.

```
ray!("small")?.small()?;
ray!("regular")?;
ray!("large")?.large()?;
```

### Adding a label

You can customize the label displayed next to an item with the `label` function.

```
ray!(["John", "Paul", "George", "Ringo"])?.label("Beatles")?;
```

### Displaying a table

You can send a map or array to Ray with the `table` function.

```
ray!().table(json!({
    "First": "First value",
    "Second": "Second value",
    "Third": "Third value"
}))?;

ray!().table_with_label(["John", "Paul", "George", "Ringo"], "Beatles")?;
```

Nested table cells are escaped, preformatted JSON. Top-level strings, integers, booleans and null match PHP's table payloads.

Output formats
--------------------------------------------------------------------------------------------------

### Working with JSON

Want to display the JSON representation of anything you'd like in Ray? Use `to_json`. You can provide any value that can be serialized with Serde.

```
ray!().to_json(json!({"a": 1, "b": {"c": 3}}))?;
```

You can send a valid JSON string to Ray with the `json` function. It will be displayed using Ray's JSON renderer. Unlike PHP's `json`, invalid JSON returns an error instead of displaying null.

```
let json_string = r#"{"a": 1, "b": {"c": 3}}"#;
ray!().json(json_string)?;
```

Rust has no variadic methods. Chain `to_json` or `json` for multiple values, or use `send_many(&[Value])` to send multiple serializable values in one request.

### Working with XML

You can send a valid XML string to Ray with the `xml` function. It will be displayed as formatted XML. Invalid XML returns an error. XML entity spelling and declarations may differ from PHP's DOM formatter for complex documents.

```
let xml_string = "<one><two><three>3</three></two></one>";
ray!().xml(xml_string)?;
```

### Working with files

You can display the contents of any UTF-8 text file in Ray with the `file` function. A missing file is displayed as a message. Other file errors are returned.

```
ray!().file("somefile.txt")?;
```

### Displaying images

To display an image, call the `image` function and pass a fully-qualified filename, URL, or a valid base64-encoded image as its only argument.

```
ray!().image("https://placekitten.com/200/300")?;
ray!().image("/home/user/kitten.jpg")?;
ray!().image("data:image/png;base64,aGVsbG8=")?;
ray!().image("aGVsbG8=")?;
```

The short base64 examples demonstrate transport syntax, not a valid image. Supply actual image bytes when displaying one. Like PHP, base64 input is sent with an `image/png` data prefix, even when the supplied data prefix used another image type.

### Displaying a link

You can render a clickable link in Ray by using the `link` or `url` methods. Pass an empty label to use the URL.

```
ray!().link("spatie.be", "")?;
ray!().link("spatie.be", "Spatie homepage")?;
ray!().url("myray.app", "")?;
```

We assume that `spatie.be` means `https://spatie.be`. URL and label are escaped rather than interpreted as HTML; use `html` for trusted markup.

### Rendering HTML

To render a piece of HTML directly in Ray, you can use the `html` method.

```
ray!().html("<b>Bold string</b>")?;
```

### Displaying text content

To display raw text while preserving whitespace formatting, use the `text` method. If the text contains HTML, it will be displayed as-is and is not rendered.

```
ray!().text("<em>this string is html encoded</em>")?;
ray!().text("  whitespace formatting\n   is preserved as well.")?;
```

Inspecting values
-----------------------------------------------------------------------------------------------------------

### Display the type name of an object

To quickly send the Rust type name of a value to Ray, use the `class_name` function.

```
ray!().class_name(&an_object)?;
```

### Showing raw values

Use `raw` to bypass specialized boolean/null handling. Structured values use preformatted JSON, not PHP's private-property reflection. For `Debug`-only values, use `debug`.

```
ray!().raw(true)?;
ray!().debug(&std::time::Duration::from_millis(25))?;
```

PHP runtime information, Carbon-specific formatting and private-property invasion have no direct Rust equivalent and are intentionally omitted.

Caller &amp; stack trace
----------------------------------------------------------------------------------------------------------------------

### See the caller of a function

Sometimes you want to know where your code is being called. You can determine that by using the `caller` function. To see the entire backtrace, use `trace` or `backtrace`.

```
ray!().caller()?;
ray!().trace()?;
```

Stack resolution depends on debug symbols. The macro's file/line metadata remains available even without them. `trace_frames` and `caller_frame` also accept explicit protocol frames.

Measuring &amp; counting
----------------------------------------------------------------------------------------------------------------------

### Counting execution times

You can display a count of how many times a piece of code was called using `count`.

```
for _ in 1..=2 {
    ray!().count()?;
    for _ in 1..=4 {
        ray!().count()?;
    }
}
```

Use `count_named` to share a named counter. Read its value with `counter_value`. All calls for a given counter update the same entry. `clear_counters` resets the session's counters.

```
let client = Client::new(Config::default());
for _ in 1..=4 {
    client.ray().count_named("first")?;
    if client.ray().counter_value("first")? == 2 {
        println!("counter value is two!");
    }
}
```

### Measuring performance

You can use `measure` to display runtime. When `measure` is called again, the time between this and the previous call is also displayed. An empty name selects the default timer. `stop_time("")` resets all timers; pass a name to reset just one.

```
ray!().measure("")?;
// Do some work.
ray!().measure("")?;
ray!().measure_with(|| {
    let _ = (1..=100).sum::<u64>();
})?;
```

Durations are measured with a monotonic clock and sent in milliseconds, like PHP. PHP heap-memory measurements are not portable to Rust; the corresponding protocol fields are zero. Panics in the closure are not caught.

Limiting output
-----------------------------------------------------------------------------------------------------

### Limiting the number of sent payloads

To limit the number of requests sent by a particular callsite, use `limit`. It works well for debugging loops.

```
for i in 1..=10 {
    ray!().limit(3)?.text(&format!("A #{i}"))?;
    ray!().limit(6)?.text(&format!("B #{i}"))?;
    ray!().text(&format!("C #{i}"))?;
}
```

If the limit is zero or negative, limiting is disabled. Like PHP, each chained request, including color and label changes, consumes one allowance. The first limit for a source location is retained for the life of the client.

### Using a rate limiter

A rate limiter reduces the number of sent messages. Configure it on the shared client session:

```
let client = Client::new(Config::default());
client.max(Some(10))?;
client.per_second(Some(10))?;
client.ray().send("limited output")?;
client.clear_rate_limits()?;
```

Ray displays one notification when a limit is reached. `None` disables a particular rate limit. Per-second limits use a sliding one-second window. Failed network sends count toward limits, matching PHP's attempt-based behavior.

### Sending a payload once

To only send a payload once from a source location, use `once`.

```
for i in 1..=10 {
    ray!().once()?.send(i)?;
}
for i in 1..=10 {
    ray!().once()?.html(&format!("<strong>{i}</strong>"))?;
}
```

Conditional output
--------------------------------------------------------------------------------------------------------------

### Conditionally showing items

`show_if` removes the entry when the condition is false. `remove_when` removes it when the condition is true.

```
ray!("will be shown")?.show_if(true)?;
ray!("will not be shown")?.show_if(false)?;
```

### Conditionally sending items to Ray

Use `when` to gate all following sends. A later call replaces the gate. Use `when_with` for a conditional callback that does not change the gate of later methods.

```
for number in 1..=10 {
    ray!().when(number < 10).text(&format!("value is less than ten: {number}"))?.blue()?;
    ray!().text(&format!("value: {number}"))?
        .when_with(number % 2 == 0, |ray| ray.large())?
        .green()?;
}
```

Handling exceptions
-----------------------------------------------------------------------------------------------------------------

### Displaying errors

You can display information about a Rust error in Ray with `exception`.

```
let error = std::io::Error::other("test");
ray!().exception(&error)?;
```

This sends the Rust type name, message, source chain and an observation-site stack trace. Rust errors do not retain PHP-style throw sites or snippets. Handle `Result` with ordinary Rust control flow; deferred exception queues and shutdown rethrows are not emulated.

Updating &amp; removing items
-------------------------------------------------------------------------------------------------------------------------------------

### Updating displayed items

You can update values that are already displayed in Ray. Hold on to the returned instance and call `send` on it.

```
let mut ray = ray!("counting down!")?;
for number in (1..=10).rev() {
    ray = ray.send(number)?;
}
ray.red()?.large()?;
```

Fluent methods consume and return the handle. Cloning a handle keeps the same UUID; creating a new one creates a new entry.

### Removing items

To remove an item that is already displayed in Ray, call `remove` on the returned instance.

```
let ray = ray!("will be removed")?;
ray.remove()?;
```

Execution control
-----------------------------------------------------------------------------------------------------------

### Pausing execution

You can pause the current thread by using `pause`. Press Continue in Ray to resume. Stop execution returns `Error::StopExecutionRequested`; propagate it to stop your operation. The library never kills your process.

This example requires a running Ray app and is compile-checked, not executed by rustdoc. Continue and Stop are exercised by the HTTP tests.

```
ray!().pause()?;
```

Network failure resumes execution unless `throws(true)` is set. The client polls once per second with the current hostname and project name. This is a blocking API; use it off your async executor's worker threads.

Flow helpers
--------------------------------------------------------------------------------------------

### Returning items

To send something to Ray and have that same value returned, use `pass`. This is handy when debugging return values.

```
let value = ray!().pass("return value")?;
```

Custom functions
--------------------------------------------------------------------------------------------------------

Use ordinary Rust functions or extension traits instead of dynamically registered methods. Send custom content with `send_custom`. `send_request` and `Payload::new` are available for protocol extensions.

```
ray!().send_custom("my custom content", "hey")?;
```

Managing the Ray app
--------------------------------------------------------------------------------------------------------------------

### Creating a new screen

You can use `new_screen` or `clear_screen` to programmatically create a new screen. Your previous messages are automatically saved and moved to the message archive. Pass an empty name for an unnamed screen.

```
ray!().new_screen("")?;
ray!().new_screen("My debug screen")?;
ray!().clear_screen()?;
ray!().clear_all()?;
```

`clear_all` also clears previous screens. Screen color helpers include `screen_green`, `screen_red`, `screen_blue`, `screen_orange`, `screen_purple` and `screen_gray`.

### Showing and hiding the app

You can show and hide the Ray app via code.

```
ray!().show_app()?;
ray!().hide_app()?;
```

Use `client.windows()` and `client.theme()` to query the app. Both return the server's JSON and report transport failures.

### Enabling &amp; disabling Ray

You can enable and disable sending stuff to Ray with `enable` and `disable`. This affects all handles sharing the same client.

```
let client = Client::new(Config::default());
client.ray().send("one")?;
client.ray().disable()?;
client.ray().send("two")?;
client.ray().enable()?;
client.ray().send("three")?;
```

Fun stuff
-----------------------------------------------------------------------------------

### Displaying a notification

You can use Ray to display a system notification.

```
ray!().notify("This is my notification")?;
```

### Shooting confetti

For those times when success is to be celebrated.

```
ray!().confetti()?;
```

 A good
match?
-------------

### What we do best

- All things Laravel
- Custom frontend components
- Building APIs
- AI-powered features
- Simplifying things
- Clean solutions
- Integrating services

### Not our cup of tea

- WordPress themes
- Cutting corners
- Free mockups to win a job
- "Just execute the briefing"

 In short: we'd like to be a **substantial part** of your project.

 [ Get in touch via email ](mailto:info@spatie.be?subject=A%20good%20match%21&body=Tell%20us%20as%20much%20as%20you%20can%20about%0A-%20your%20online%20project%0A-%20your%20planning%0A-%20your%20budget%0A-%20%E2%80%A6%0A%0AAnything%20that%20helps%20us%20to%20start%20straightforward%21)
