From cc2e82599cca607aeb767c9ea4ed094282df1515 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tau=20G=C3=A4rtli?= Date: Mon, 10 Jun 2024 12:26:41 +0200 Subject: [PATCH] Replace hand-written `Debug` impls with derive --- .../src/dialogue_runner/builder.rs | 24 ++++------------- crates/bevy_plugin/src/fmt_utils.rs | 26 +++++++++++++++++++ crates/bevy_plugin/src/lib.rs | 1 + .../file_extension_asset_provider_plugin.rs | 17 +++--------- .../strings_file_text_provider.rs | 20 +++----------- crates/bevy_plugin/src/project.rs | 18 +++---------- crates/bevy_plugin/src/project/compilation.rs | 3 ++- 7 files changed, 43 insertions(+), 66 deletions(-) create mode 100644 crates/bevy_plugin/src/fmt_utils.rs diff --git a/crates/bevy_plugin/src/dialogue_runner/builder.rs b/crates/bevy_plugin/src/dialogue_runner/builder.rs index cb69b6e3..32b73ea7 100644 --- a/crates/bevy_plugin/src/dialogue_runner/builder.rs +++ b/crates/bevy_plugin/src/dialogue_runner/builder.rs @@ -1,16 +1,17 @@ use crate::default_impl::{MemoryVariableStorage, StringsFileTextProvider}; +use crate::fmt_utils::SkipDebug; use crate::line_provider::SharedTextProvider; use crate::prelude::*; use bevy::prelude::*; use bevy::utils::HashMap; use rand::{rngs::SmallRng, Rng, SeedableRng}; use std::any::{Any, TypeId}; -use std::fmt; -use std::fmt::{Debug, Formatter}; +use std::fmt::Debug; pub(crate) fn dialogue_runner_builder_plugin(_app: &mut App) {} /// A builder for [`DialogueRunner`]. This is instantiated for you by calling [`YarnProject::build_dialogue_runner`]. +#[derive(Debug)] pub struct DialogueRunnerBuilder { variable_storage: Box, text_provider: SharedTextProvider, @@ -19,22 +20,7 @@ pub struct DialogueRunnerBuilder { commands: YarnCommands, compilation: Compilation, localizations: Option, - asset_server: AssetServer, -} - -impl Debug for DialogueRunnerBuilder { - fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { - f.debug_struct("DialogueRunnerBuilder") - .field("variable_storage", &self.variable_storage) - .field("text_provider", &self.text_provider) - .field("asset_providers", &self.asset_providers) - .field("library", &self.library) - .field("commands", &self.commands) - .field("compilation", &self.compilation) - .field("localizations", &self.localizations) - .field("asset_server", &()) - .finish() - } + asset_server: SkipDebug, } impl DialogueRunnerBuilder { @@ -99,7 +85,7 @@ impl DialogueRunnerBuilder { asset_provider.set_localizations(localizations.clone()); } - asset_provider.set_asset_server(self.asset_server.clone()); + asset_provider.set_asset_server(self.asset_server.0.clone()); } let popped_line_hints = dialogue.pop_line_hints(); diff --git a/crates/bevy_plugin/src/fmt_utils.rs b/crates/bevy_plugin/src/fmt_utils.rs new file mode 100644 index 00000000..4e1e5190 --- /dev/null +++ b/crates/bevy_plugin/src/fmt_utils.rs @@ -0,0 +1,26 @@ +use std::fmt; +use std::ops::{Deref, DerefMut}; + +/// A wrapper for skipping individual fields when deriving [`fmt::Debug`]. +#[derive(Clone, Default)] +pub(crate) struct SkipDebug(pub(crate) T); + +impl fmt::Debug for SkipDebug { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str("...") + } +} + +impl Deref for SkipDebug { + type Target = T; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +impl DerefMut for SkipDebug { + fn deref_mut(&mut self) -> &mut Self::Target { + &mut self.0 + } +} diff --git a/crates/bevy_plugin/src/lib.rs b/crates/bevy_plugin/src/lib.rs index 11c5536e..49549834 100644 --- a/crates/bevy_plugin/src/lib.rs +++ b/crates/bevy_plugin/src/lib.rs @@ -100,6 +100,7 @@ mod commands; mod development_file_generation; mod dialogue_runner; +mod fmt_utils; mod line_provider; mod localization; mod plugin; diff --git a/crates/bevy_plugin/src/line_provider/asset_provider/file_extension_asset_provider_plugin.rs b/crates/bevy_plugin/src/line_provider/asset_provider/file_extension_asset_provider_plugin.rs index 1db81b3a..d28070fe 100644 --- a/crates/bevy_plugin/src/line_provider/asset_provider/file_extension_asset_provider_plugin.rs +++ b/crates/bevy_plugin/src/line_provider/asset_provider/file_extension_asset_provider_plugin.rs @@ -1,3 +1,4 @@ +use crate::fmt_utils::SkipDebug; use crate::prelude::*; use crate::UnderlyingYarnLine; use bevy::asset::{LoadState, LoadedUntypedAsset}; @@ -23,11 +24,11 @@ pub(crate) fn file_extension_asset_provider_plugin(_app: &mut App) {} /// /// If you want to load audio assets, the feature `audio_assets` will provide you with an [`AudioAssetProvider`] that is a wrapper around this type /// configured in such a way. -#[derive(Clone, Default)] +#[derive(Clone, Default, Debug)] pub struct FileExtensionAssetProvider { language: Option, localizations: Option, - asset_server: Option, + asset_server: SkipDebug>, loading_handles: HashMap>, loaded_handles: HashMap, line_ids: HashSet, @@ -232,15 +233,3 @@ impl FileExtensionAssetProvider { } } } - -impl Debug for FileExtensionAssetProvider { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("AudioAssetProvider") - .field("language", &self.language) - .field("localizations", &self.localizations) - .field("asset_server", &()) - .field("handles", &self.loading_handles) - .field("line_ids", &self.line_ids) - .finish() - } -} diff --git a/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs b/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs index 920c0ee0..8e8f5726 100644 --- a/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs +++ b/crates/bevy_plugin/src/line_provider/text_provider/strings_file_text_provider.rs @@ -1,3 +1,4 @@ +use crate::fmt_utils::SkipDebug; use crate::prelude::*; use crate::UnderlyingTextProvider; @@ -5,7 +6,6 @@ use bevy::ecs::event::ManualEventReader; use bevy::prelude::*; use std::any::Any; use std::collections::HashMap; -use std::fmt::Debug; use std::sync::{Arc, RwLock}; pub(crate) fn strings_file_text_provider_plugin(_app: &mut App) {} @@ -15,9 +15,9 @@ pub(crate) fn strings_file_text_provider_plugin(_app: &mut App) {} /// this will send the lines as they appear in the Yarn file. If [`DialogueRunner::set_language`] or [`DialogueRunner::set_text_language`] were used to /// set the language to a language supported by a translation in the [`Localizations`], this loads the strings file for that translation from the disk at the /// specified path. If this fails, the base language is used as a fallback. -#[derive(Clone)] +#[derive(Debug, Clone)] pub struct StringsFileTextProvider { - asset_server: AssetServer, + asset_server: SkipDebug, localizations: Option, language: Option, base_string_table: HashMap, @@ -26,20 +26,6 @@ pub struct StringsFileTextProvider { event_reader: Arc>>>, } -impl Debug for StringsFileTextProvider { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("StringsTableTextProvider") - .field("asset_server", &()) - .field("localizations", &self.localizations) - .field("language", &self.language) - .field("base_string_table", &self.base_string_table) - .field("strings_file_handle", &self.strings_file_handle) - .field("translation_string_table", &self.translation_string_table) - .field("event_reader", &self.event_reader) - .finish() - } -} - impl UnderlyingTextProvider for StringsFileTextProvider { fn clone_shallow(&self) -> Box { Box::new(self.clone()) diff --git a/crates/bevy_plugin/src/project.rs b/crates/bevy_plugin/src/project.rs index 67259f50..a9b3286f 100644 --- a/crates/bevy_plugin/src/project.rs +++ b/crates/bevy_plugin/src/project.rs @@ -1,3 +1,4 @@ +use crate::fmt_utils::SkipDebug; use crate::prelude::*; use bevy::prelude::*; use bevy::utils::{HashMap, HashSet}; @@ -29,30 +30,17 @@ pub(crate) struct CompilationSystemSet; /// commands.spawn(project.create_dialogue_runner()); /// } /// ``` -#[derive(Resource)] +#[derive(Resource, Debug)] pub struct YarnProject { pub(crate) yarn_files: HashSet>, pub(crate) compilation: Compilation, pub(crate) localizations: Option, - pub(crate) asset_server: AssetServer, + pub(crate) asset_server: SkipDebug, pub(crate) metadata: HashMap>, pub(crate) watching_for_changes: bool, pub(crate) development_file_generation: DevelopmentFileGeneration, } -impl Debug for YarnProject { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.debug_struct("YarnProject") - .field("yarn_files", &self.yarn_files) - .field("compilation", &self.compilation) - .field("localizations", &self.localizations) - .field("asset_server", &()) - .field("metadata", &self.metadata) - .field("watching_for_changes", &self.watching_for_changes) - .finish() - } -} - impl YarnProject { /// Iterates over the [`YarnFile`]s that were used to compile this project. These will be the files passed to the [`YarnSpinnerPlugin`] or the [`LoadYarnProjectEvent`]. pub fn yarn_files(&self) -> impl Iterator> { diff --git a/crates/bevy_plugin/src/project/compilation.rs b/crates/bevy_plugin/src/project/compilation.rs index c963e3db..615537f7 100644 --- a/crates/bevy_plugin/src/project/compilation.rs +++ b/crates/bevy_plugin/src/project/compilation.rs @@ -1,3 +1,4 @@ +use crate::fmt_utils::SkipDebug; use crate::localization::{LineIdUpdateSystemSet, UpdateAllStringsFilesForStringTableEvent}; use crate::plugin::AssetRoot; use crate::prelude::*; @@ -237,7 +238,7 @@ fn compile_loaded_yarn_files( yarn_files: std::mem::take(&mut yarn_files_being_loaded.0), compilation, localizations: yarn_project_config_to_load.localizations.clone().unwrap(), - asset_server: asset_server.clone(), + asset_server: SkipDebug(asset_server.clone()), watching_for_changes: yarn_project_config_to_load.watching_for_changes, development_file_generation, metadata,