|
| 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