Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
bevyengine
GitHub Repository: bevyengine/bevy
Path: blob/main/crates/bevy_reflect/examples/reflect_docs.rs
6598 views
1
//! This example illustrates how you can reflect doc comments.
2
//!
3
//! There may be cases where you may want to collect a reflected item's documentation.
4
//! For example, you may want to generate schemas or other external documentation for scripting.
5
//! Or perhaps you want your custom editor to display tooltips for certain properties that match the documentation.
6
//!
7
//! These scenarios can readily be achieved by using `bevy_reflect` with the `documentation` feature.
8
9
#![expect(clippy::print_stdout, reason = "Allowed in examples.")]
10
11
use bevy_reflect::{Reflect, TypeInfo, Typed};
12
13
fn main() {
14
//! This function will simply demonstrate how you can access a type's documentation.
15
//!
16
//! Please note that the code below uses a standard struct with named fields; however, this isn't
17
//! exclusive to them. It can work for all kinds of data types including tuple structs and enums too!
18
19
/// The struct that defines our player.
20
///
21
/// # Example
22
///
23
/// ```
24
/// let player = Player::new("Urist McPlayer");
25
/// ```
26
#[derive(Reflect)]
27
struct Player {
28
/// The player's name.
29
name: String,
30
/// The player's current health points.
31
hp: u8,
32
// Some regular comment (i.e. not a doc comment)
33
max_hp: u8,
34
}
35
36
// Using `TypeInfo` we can access all of the doc comments on the `Player` struct above:
37
let player_info = <Player as Typed>::type_info();
38
39
// From here, we already have access to the struct's docs:
40
let player_docs = player_info.docs().unwrap();
41
assert_eq!(" The struct that defines our player.\n\n # Example\n\n ```\n let player = Player::new(\"Urist McPlayer\");\n ```", player_docs);
42
println!("=====[ Player ]=====\n{player_docs}");
43
44
// We can then iterate through our struct's fields to get their documentation as well:
45
if let TypeInfo::Struct(struct_info) = player_info {
46
for field in struct_info.iter() {
47
let field_name = field.name();
48
let field_docs = field.docs().unwrap_or("<NO_DOCUMENTATION>");
49
println!("-----[ Player::{field_name} ]-----\n{field_docs}");
50
}
51
}
52
}
53
54