-
Notifications
You must be signed in to change notification settings - Fork 373
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for registering custom loaders, the same way we offer support for registering custom space views and store subscribers. Checks: - [x] cargo r -p custom_data_loader -- Cargo.toml --- Part of a series of PRs to make it possible to load _any_ file from the local filesystem, by any means, on web and native: - #4516 - #4517 - #4518 - #4519 - #4520 - #4521 - #4565 - #4566 - #4567
- Loading branch information
Showing
8 changed files
with
157 additions
and
19 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "custom_data_loader" | ||
version = "0.12.0-alpha.1+dev" | ||
edition = "2021" | ||
rust-version = "1.72" | ||
license = "MIT OR Apache-2.0" | ||
publish = false | ||
|
||
[dependencies] | ||
rerun = { path = "../../../crates/rerun", features = ["native_viewer"] } | ||
|
||
[build-dependencies] | ||
re_build_tools = { path = "../../../crates/re_build_tools" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
--- | ||
title: Custom data-loader | ||
tags: [data-loader, extension] | ||
rust: https://github.com/rerun-io/rerun/tree/latest/examples/rust/custom_data_loader/src/main.rs?speculative-link | ||
--- | ||
|
||
<picture> | ||
<img src="https://static.rerun.io/custom_data_loader/e44aadfa02fade5a3cf5d7cbdd6e0bf65d9f6446/full.png" alt=""> | ||
<source media="(max-width: 480px)" srcset="https://static.rerun.io/custom_data_loader/e44aadfa02fade5a3cf5d7cbdd6e0bf65d9f6446/480w.png"> | ||
<source media="(max-width: 768px)" srcset="https://static.rerun.io/custom_data_loader/e44aadfa02fade5a3cf5d7cbdd6e0bf65d9f6446/768w.png"> | ||
<source media="(max-width: 1024px)" srcset="https://static.rerun.io/custom_data_loader/e44aadfa02fade5a3cf5d7cbdd6e0bf65d9f6446/1024w.png"> | ||
<source media="(max-width: 1200px)" srcset="https://static.rerun.io/custom_data_loader/e44aadfa02fade5a3cf5d7cbdd6e0bf65d9f6446/1200w.png"> | ||
</picture> | ||
|
||
This example demonstrates how to implement and register a `DataLoader` into the Rerun Viewer in order to add support for loading arbitrary files. | ||
|
||
Usage: | ||
```sh | ||
$ cargo r -p custom_data_loader -- path/to/some/file | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
re_build_tools::export_build_info_vars_for_crate("custom_data_loader"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
//! This example demonstrates how to implement and register a [`re_data_source::DataLoader`] into | ||
//! the Rerun Viewer in order to add support for loading arbitrary files. | ||
//! | ||
//! Usage: | ||
//! ```sh | ||
//! $ cargo r -p custom_data_loader -- path/to/some/file | ||
//! ``` | ||
use rerun::{ | ||
external::{anyhow, re_build_info, re_data_source, re_log, tokio}, | ||
log::{DataRow, RowId}, | ||
EntityPath, TimePoint, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() -> anyhow::Result<std::process::ExitCode> { | ||
re_log::setup_native_logging(); | ||
|
||
re_data_source::register_custom_data_loader(HashLoader); | ||
|
||
let build_info = re_build_info::build_info!(); | ||
rerun::run(build_info, rerun::CallSource::Cli, std::env::args()) | ||
.await | ||
.map(std::process::ExitCode::from) | ||
} | ||
|
||
// --- | ||
|
||
/// A custom [`re_data_source::DataLoader`] that logs the hash of file as a [`rerun::TextDocument`]. | ||
struct HashLoader; | ||
|
||
impl re_data_source::DataLoader for HashLoader { | ||
fn name(&self) -> String { | ||
"rerun.data_loaders.HashLoader".into() | ||
} | ||
|
||
fn load_from_path( | ||
&self, | ||
_store_id: rerun::external::re_log_types::StoreId, | ||
path: std::path::PathBuf, | ||
tx: std::sync::mpsc::Sender<re_data_source::LoadedData>, | ||
) -> Result<(), re_data_source::DataLoaderError> { | ||
let contents = std::fs::read(&path)?; | ||
if path.is_dir() { | ||
return Err(re_data_source::DataLoaderError::Incompatible(path)); // simply not interested | ||
} | ||
hash_and_log(&tx, &path, &contents) | ||
} | ||
|
||
fn load_from_file_contents( | ||
&self, | ||
_store_id: rerun::external::re_log_types::StoreId, | ||
filepath: std::path::PathBuf, | ||
contents: std::borrow::Cow<'_, [u8]>, | ||
tx: std::sync::mpsc::Sender<re_data_source::LoadedData>, | ||
) -> Result<(), re_data_source::DataLoaderError> { | ||
hash_and_log(&tx, &filepath, &contents) | ||
} | ||
} | ||
|
||
fn hash_and_log( | ||
tx: &std::sync::mpsc::Sender<re_data_source::LoadedData>, | ||
filepath: &std::path::Path, | ||
contents: &[u8], | ||
) -> Result<(), re_data_source::DataLoaderError> { | ||
use std::collections::hash_map::DefaultHasher; | ||
use std::hash::{Hash, Hasher}; | ||
|
||
let mut h = DefaultHasher::new(); | ||
contents.hash(&mut h); | ||
|
||
let doc = rerun::TextDocument::new(format!("{:08X}", h.finish())) | ||
.with_media_type(rerun::MediaType::TEXT); | ||
|
||
let entity_path = EntityPath::from_file_path(filepath); | ||
let entity_path = format!("{entity_path}/hashed").into(); | ||
let row = DataRow::from_archetype(RowId::new(), TimePoint::timeless(), entity_path, &doc)?; | ||
|
||
tx.send(row.into()).ok(); | ||
|
||
Ok(()) | ||
} |