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