Skip to content

Commit

Permalink
Snippet index: support for manual opt-outs (#8385)
Browse files Browse the repository at this point in the history
Adds the ability to manually opt-out of the index for specific
snippets/archetypes/components.

E.g.:
```toml
# These snippets will be excluded from the snippet index, unconditionally.
[snippets_ref.snippets.opt_out]
"archetypes/manual_indicator" = []

# These archetypes will ignore the associated snippets in the snippet index.
[snippets_ref.archetypes.opt_out]
"DataframeQuery" = [
  "reference/dataframe_save_blueprint"
]

# These components will ignore the associated snippets in the snippet index.
[snippets_ref.components.opt_out]
"ShowLabels" = [
  "tutorials/data_out",
]
```

Config will have to be filled over time!


[Rendered](https://github.com/rerun-io/rerun/blob/cmc/snippets_index_2_opt_outs/docs/snippets/INDEX.md)

* Part of #1123
* Part of #5662
* DNM: requires #8383
  • Loading branch information
teh-cmc authored Dec 10, 2024
1 parent 6b40cd5 commit 7d0036e
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 7 deletions.
50 changes: 50 additions & 0 deletions crates/build/re_types_builder/src/codegen/docs/snippets_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ use crate::{CodeGenerator, GeneratedFiles, Object, ObjectKind, Objects, Reporter

// ---

/// See `docs/snippets/snippets.toml` for more info
#[derive(Debug, serde::Deserialize)]
struct Config {
snippets_ref: SnippetsRef,
}

#[derive(Debug, serde::Deserialize)]
struct SnippetsRef {
snippets: OptOut,
archetypes: OptOut,
components: OptOut,
}

#[derive(Debug, serde::Deserialize)]
struct OptOut {
// <name, vec<snippet_name_qualified>>
opt_out: BTreeMap<String, Vec<String>>,
}

/// Everything we know about a snippet, including which objects (archetypes, components, etc) it references.
#[allow(dead_code)]
#[derive(Debug, Clone)]
Expand Down Expand Up @@ -117,6 +136,10 @@ impl SnippetsRefCodeGenerator {
let snippets_dir_root = snippets_dir.join("all");
let snippets_dir_archetypes = snippets_dir_root.clone();

let config_path = snippets_dir.join("snippets.toml");
let config = std::fs::read_to_string(config_path)?;
let config: Config = toml::from_str(&config)?;

let known_objects = KnownObjects::init(objects);

let snippets = collect_snippets_recursively(
Expand Down Expand Up @@ -180,6 +203,10 @@ impl SnippetsRefCodeGenerator {
Ok(row)
}

let opt_outs = &config.snippets_ref.snippets.opt_out;
let archetype_opt_outs = &config.snippets_ref.archetypes.opt_out;
let component_opt_outs = &config.snippets_ref.components.opt_out;

let snippets_table = |snippets: &BTreeMap<&Object, Vec<Snippet<'_>>>| {
let table = snippets
.iter()
Expand All @@ -198,6 +225,29 @@ impl SnippetsRefCodeGenerator {
});
snippets.into_iter().map(move |snippet| (obj, snippet))
})
.filter(|(obj, snippet)| {
if opt_outs.contains_key(&snippet.name_qualified) {
return false;
}

if obj.kind == ObjectKind::Archetype {
if let Some(opt_outs) = archetype_opt_outs.get(&obj.name) {
if opt_outs.contains(&snippet.name_qualified) {
return false;
}
}
}

if obj.kind == ObjectKind::Component {
if let Some(opt_outs) = component_opt_outs.get(&obj.name) {
if opt_outs.contains(&snippet.name_qualified) {
return false;
}
}
}

true
})
.map(|(obj, snippet)| snippet_row(obj, &snippet))
.collect::<Result<Vec<_>, _>>()?
.join("\n");
Expand Down
Loading

0 comments on commit 7d0036e

Please sign in to comment.