Skip to content

Commit 0e7a885

Browse files
committed
add rust example
1 parent 7d2ad9f commit 0e7a885

File tree

5 files changed

+143
-0
lines changed

5 files changed

+143
-0
lines changed

Cargo.lock

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/assets/example.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
println!("That will only work with the right plugin in your $PATH!");
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "rerun-loader-rust-file"
3+
version = "0.12.0-alpha.1+dev"
4+
edition = "2021"
5+
rust-version = "1.72"
6+
license = "MIT OR Apache-2.0"
7+
publish = false
8+
9+
[dependencies]
10+
rerun = { path = "../../../crates/rerun" }
11+
12+
pico-args = { version = "0.5.0", features = ["eq-separator"] }
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---
2+
title: Standard Input/Output example
3+
python: https://github.com/rerun-io/rerun/tree/latest/examples/python/external_data_loader/main.py?speculative-link
4+
rust: https://github.com/rerun-io/rerun/tree/latest/examples/rust/external_data_loader/src/main.rs?speculative-link
5+
cpp: https://github.com/rerun-io/rerun/tree/latest/examples/cpp/external_data_loader/main.cpp?speculative-link
6+
thumbnail: https://static.rerun.io/external_data_loader_rust/45b1312910063086708d2db52dc3eb619e741d8c/480w.png
7+
---
8+
9+
<picture>
10+
<img src="https://static.rerun.io/external_data_loader_rust/45b1312910063086708d2db52dc3eb619e741d8c/full.png" alt="">
11+
<source media="(max-width: 480px)" srcset="https://static.rerun.io/external_data_loader_rust/45b1312910063086708d2db52dc3eb619e741d8c/480w.png">
12+
<source media="(max-width: 768px)" srcset="https://static.rerun.io/external_data_loader_rust/45b1312910063086708d2db52dc3eb619e741d8c/768w.png">
13+
<source media="(max-width: 1024px)" srcset="https://static.rerun.io/external_data_loader_rust/45b1312910063086708d2db52dc3eb619e741d8c/1024w.png">
14+
<source media="(max-width: 1200px)" srcset="https://static.rerun.io/external_data_loader_rust/45b1312910063086708d2db52dc3eb619e741d8c/1200w.png">
15+
</picture>
16+
17+
This is an example executable data-loader plugin for the Rerun Viewer.
18+
19+
It will log Rust source code files as markdown documents.
20+
To try it out, install it in your path (`cargo install --path . -f`), then open a Rust source file with Rerun (`rerun file.rs`).
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
//! Example of an external data-loader executable plugin for the Rerun Viewer.
2+
3+
use rerun::MediaType;
4+
5+
const USAGE: &str = "
6+
This is an example executable data-loader plugin for the Rerun Viewer.
7+
8+
It will log Rust source code files as markdown documents.
9+
To try it out, install it in your path (`cargo install --path . -f`), then open a Rust source file with Rerun (`rerun file.rs`).
10+
11+
USAGE:
12+
rerun-loader-rs [OPTIONS] FILEPATH
13+
14+
FLAGS:
15+
-h, --help Prints help information
16+
17+
OPTIONS:
18+
--recording-id RECORDING_ID ID of the shared recording
19+
20+
ARGS:
21+
<FILEPATH>
22+
";
23+
24+
#[allow(clippy::exit)]
25+
fn usage() -> ! {
26+
eprintln!("{USAGE}");
27+
std::process::exit(1);
28+
}
29+
30+
// The Rerun Viewer will always pass these two pieces of information:
31+
// 1. The path to be loaded, as a positional arg.
32+
// 2. A shared recording ID, via the `--recording-id` flag.
33+
//
34+
// It is up to you whether you make use of that shared recording ID or not.
35+
// If you use it, the data will end up in the same recording as all other plugins interested in
36+
// that file, otherwise you can just create a dedicated recording for it. Or both.
37+
struct Args {
38+
filepath: std::path::PathBuf,
39+
recording_id: Option<String>,
40+
}
41+
42+
impl Args {
43+
fn from_env() -> Result<Self, pico_args::Error> {
44+
let mut pargs = pico_args::Arguments::from_env();
45+
Ok(Self {
46+
filepath: pargs.free_from_str()?,
47+
recording_id: pargs.opt_value_from_str("--recording-id")?,
48+
})
49+
}
50+
}
51+
52+
fn main() -> Result<(), Box<dyn std::error::Error>> {
53+
let Ok(args) = Args::from_env() else {
54+
usage();
55+
};
56+
57+
let is_file = args.filepath.is_file();
58+
let is_rust_file = args
59+
.filepath
60+
.extension()
61+
.unwrap_or_default()
62+
.to_ascii_lowercase()
63+
== "rs";
64+
65+
// We're not interested: just exit silently.
66+
// Don't return an error, as that would show up to the end user in the Rerun Viewer!
67+
if !(is_file && is_rust_file) {
68+
return Ok(());
69+
}
70+
71+
let rec = {
72+
let mut rec =
73+
rerun::RecordingStreamBuilder::new(args.filepath.to_string_lossy().to_string());
74+
if let Some(recording_id) = args.recording_id {
75+
rec = rec.recording_id(recording_id);
76+
};
77+
78+
// The most important part of this: log to standard output so the Rerun Viewer can ingest it!
79+
rec.stdout()?
80+
};
81+
82+
let body = std::fs::read_to_string(&args.filepath)?;
83+
84+
let text = format!(
85+
"
86+
## Some Rust code
87+
88+
```rust
89+
{body}
90+
```
91+
"
92+
);
93+
94+
rec.log(
95+
rerun::EntityPath::from_file_path(&args.filepath),
96+
&rerun::TextDocument::new(text).with_media_type(MediaType::MARKDOWN),
97+
)?;
98+
99+
Ok(())
100+
}

0 commit comments

Comments
 (0)