Skip to content

Commit

Permalink
Merge pull request #198 from YarnSpinnerTool/derive-debug
Browse files Browse the repository at this point in the history
Replace hand-written `Debug` impls with derive
  • Loading branch information
bash authored Jun 10, 2024
2 parents d295184 + cc2e825 commit c6ab0c9
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 66 deletions.
24 changes: 5 additions & 19 deletions crates/bevy_plugin/src/dialogue_runner/builder.rs
Original file line number Diff line number Diff line change
@@ -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<dyn VariableStorage>,
text_provider: SharedTextProvider,
Expand All @@ -19,22 +20,7 @@ pub struct DialogueRunnerBuilder {
commands: YarnCommands,
compilation: Compilation,
localizations: Option<Localizations>,
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<AssetServer>,
}

impl DialogueRunnerBuilder {
Expand Down Expand Up @@ -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();
Expand Down
26 changes: 26 additions & 0 deletions crates/bevy_plugin/src/fmt_utils.rs
Original file line number Diff line number Diff line change
@@ -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<T>(pub(crate) T);

impl<T> fmt::Debug for SkipDebug<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("...")
}
}

impl<T> Deref for SkipDebug<T> {
type Target = T;

fn deref(&self) -> &Self::Target {
&self.0
}
}

impl<T> DerefMut for SkipDebug<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
1 change: 1 addition & 0 deletions crates/bevy_plugin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
mod commands;
mod development_file_generation;
mod dialogue_runner;
mod fmt_utils;
mod line_provider;
mod localization;
mod plugin;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::fmt_utils::SkipDebug;
use crate::prelude::*;
use crate::UnderlyingYarnLine;
use bevy::asset::{LoadState, LoadedUntypedAsset};
Expand All @@ -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<Language>,
localizations: Option<Localizations>,
asset_server: Option<AssetServer>,
asset_server: SkipDebug<Option<AssetServer>>,
loading_handles: HashMap<PathBuf, Handle<LoadedUntypedAsset>>,
loaded_handles: HashMap<PathBuf, UntypedHandle>,
line_ids: HashSet<LineId>,
Expand Down Expand Up @@ -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()
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::fmt_utils::SkipDebug;
use crate::prelude::*;
use crate::UnderlyingTextProvider;

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) {}
Expand All @@ -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<AssetServer>,
localizations: Option<Localizations>,
language: Option<Language>,
base_string_table: HashMap<LineId, StringInfo>,
Expand All @@ -26,20 +26,6 @@ pub struct StringsFileTextProvider {
event_reader: Arc<RwLock<ManualEventReader<AssetEvent<StringsFile>>>>,
}

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<dyn UnderlyingTextProvider> {
Box::new(self.clone())
Expand Down
18 changes: 3 additions & 15 deletions crates/bevy_plugin/src/project.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::fmt_utils::SkipDebug;
use crate::prelude::*;
use bevy::prelude::*;
use bevy::utils::{HashMap, HashSet};
Expand Down Expand Up @@ -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<Handle<YarnFile>>,
pub(crate) compilation: Compilation,
pub(crate) localizations: Option<Localizations>,
pub(crate) asset_server: AssetServer,
pub(crate) asset_server: SkipDebug<AssetServer>,
pub(crate) metadata: HashMap<LineId, Vec<String>>,
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<Item = &Handle<YarnFile>> {
Expand Down
3 changes: 2 additions & 1 deletion crates/bevy_plugin/src/project/compilation.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::fmt_utils::SkipDebug;
use crate::localization::{LineIdUpdateSystemSet, UpdateAllStringsFilesForStringTableEvent};
use crate::plugin::AssetRoot;
use crate::prelude::*;
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit c6ab0c9

Please sign in to comment.