diff options
Diffstat (limited to 'mingling_core/src')
| -rw-r--r-- | mingling_core/src/any.rs | 7 | ||||
| -rw-r--r-- | mingling_core/src/asset/global_resource.rs | 9 | ||||
| -rw-r--r-- | mingling_core/src/asset/help.rs | 2 | ||||
| -rw-r--r-- | mingling_core/src/asset/renderer.rs | 2 | ||||
| -rw-r--r-- | mingling_core/src/program/collection.rs | 4 | ||||
| -rw-r--r-- | mingling_core/src/program/collection/mock.rs | 4 | ||||
| -rw-r--r-- | mingling_core/src/program/exec.rs | 20 | ||||
| -rw-r--r-- | mingling_core/src/program/hook.rs | 4 | ||||
| -rw-r--r-- | mingling_core/src/renderer.rs | 2 | ||||
| -rw-r--r-- | mingling_core/src/renderer/render_result.rs | 17 | ||||
| -rw-r--r-- | mingling_core/src/renderer/structural.rs | 21 |
11 files changed, 49 insertions, 43 deletions
diff --git a/mingling_core/src/any.rs b/mingling_core/src/any.rs index b586f09..2680f43 100644 --- a/mingling_core/src/any.rs +++ b/mingling_core/src/any.rs @@ -1,5 +1,5 @@ -use crate::{Groupped, ProgramCollect}; use crate::error::ChainProcessError; +use crate::{Groupped, ProgramCollect}; #[doc(hidden)] pub mod group; @@ -136,7 +136,10 @@ impl<G> From<AnyOutput<G>> for ChainProcess<G> { } } -impl<G> From<()> for ChainProcess<G> where G: ProgramCollect<Enum = G> { +impl<G> From<()> for ChainProcess<G> +where + G: ProgramCollect<Enum = G>, +{ fn from(_v: ()) -> Self { G::build_empty_result().route_chain() } diff --git a/mingling_core/src/asset/global_resource.rs b/mingling_core/src/asset/global_resource.rs index 651655a..3d0af7b 100644 --- a/mingling_core/src/asset/global_resource.rs +++ b/mingling_core/src/asset/global_resource.rs @@ -80,9 +80,7 @@ where /// owned value. The caller must call [`__store_res`] to write back modifications. #[doc(hidden)] #[must_use] - pub fn __extract_res_mut<Res: 'static + Default + ResourceMarker + Send + Sync>( - &self, - ) -> Res { + pub fn __extract_res_mut<Res: 'static + Default + ResourceMarker + Send + Sync>(&self) -> Res { let Ok(mut guard) = self.resources.lock() else { return Res::res_default(); }; @@ -103,10 +101,7 @@ where /// /// Stores a modified resource value back into the global store. #[doc(hidden)] - pub fn __store_res<Res: 'static + Send + Sync + ResourceMarker>( - &self, - val: Res, - ) { + pub fn __store_res<Res: 'static + Send + Sync + ResourceMarker>(&self, val: Res) { if let Ok(mut guard) = self.resources.lock() { guard.insert(TypeId::of::<Res>(), Box::new(Arc::new(val))); } diff --git a/mingling_core/src/asset/help.rs b/mingling_core/src/asset/help.rs index ff2b3d4..b3742f2 100644 --- a/mingling_core/src/asset/help.rs +++ b/mingling_core/src/asset/help.rs @@ -6,5 +6,5 @@ pub trait HelpRequest { type Entry; /// Process the previous value and write the result into the provided [`RenderResult`](./struct.RenderResult.html) - fn render_help(p: Self::Entry, r: &mut RenderResult); + fn render_help(p: Self::Entry) -> RenderResult; } diff --git a/mingling_core/src/asset/renderer.rs b/mingling_core/src/asset/renderer.rs index 1d5a2c1..732f9b7 100644 --- a/mingling_core/src/asset/renderer.rs +++ b/mingling_core/src/asset/renderer.rs @@ -6,5 +6,5 @@ pub trait Renderer { type Previous; /// Process the previous value and write the result into the provided [`RenderResult`](./struct.RenderResult.html) - fn render(p: Self::Previous, r: &mut RenderResult); + fn render(p: Self::Previous) -> RenderResult; } diff --git a/mingling_core/src/program/collection.rs b/mingling_core/src/program/collection.rs index 044379c..d5aab4b 100644 --- a/mingling_core/src/program/collection.rs +++ b/mingling_core/src/program/collection.rs @@ -53,10 +53,10 @@ pub trait ProgramCollect { fn build_empty_result() -> AnyOutput<Self::Enum>; /// Render the input [`AnyOutput`](./struct.AnyOutput.html) - fn render(any: AnyOutput<Self::Enum>, r: &mut RenderResult); + fn render(any: AnyOutput<Self::Enum>) -> RenderResult; /// Render help for Entry - fn render_help(any: AnyOutput<Self::Enum>, r: &mut RenderResult); + fn render_help(any: AnyOutput<Self::Enum>) -> RenderResult; /// Find a matching chain to continue execution based on the input [AnyOutput](./struct.AnyOutput.html), returning a new [AnyOutput](./struct.AnyOutput.html) #[cfg(feature = "async")] diff --git a/mingling_core/src/program/collection/mock.rs b/mingling_core/src/program/collection/mock.rs index 568000a..9b2e7af 100644 --- a/mingling_core/src/program/collection/mock.rs +++ b/mingling_core/src/program/collection/mock.rs @@ -59,11 +59,11 @@ impl ProgramCollect for MockProgramCollect { unreachable!() } - fn render(_any: AnyOutput<Self::Enum>, _r: &mut RenderResult) { + fn render(_any: AnyOutput<Self::Enum>) -> RenderResult { unreachable!() } - fn render_help(_any: AnyOutput<Self::Enum>, _r: &mut RenderResult) { + fn render_help(_any: AnyOutput<Self::Enum>) -> RenderResult { unreachable!() } diff --git a/mingling_core/src/program/exec.rs b/mingling_core/src/program/exec.rs index d1983ed..f5bdc2b 100644 --- a/mingling_core/src/program/exec.rs +++ b/mingling_core/src/program/exec.rs @@ -461,19 +461,13 @@ pub(crate) fn handle_program_control<C: ProgramCollect<Enum = C>>( fn render<C: ProgramCollect<Enum = C>>(program: &Program<C>, any: AnyOutput<C>) -> RenderResult { #[cfg(not(feature = "structural_renderer"))] { - let mut render_result = RenderResult::default(); - C::render(any, &mut render_result); - render_result + C::render(any) } #[cfg(feature = "structural_renderer")] { #[allow(unreachable_patterns)] match program.structural_renderer_name { - super::StructuralRendererSetting::Disable => { - let mut render_result = RenderResult::default(); - C::render(any, &mut render_result); - render_result - } + super::StructuralRendererSetting::Disable => C::render(any), _ => C::structural_render(any, &program.structural_renderer_name).unwrap(), } } @@ -487,19 +481,13 @@ fn render_help<C: ProgramCollect<Enum = C>>( ) -> RenderResult { #[cfg(not(feature = "structural_renderer"))] { - let mut render_result = RenderResult::default(); - C::render_help(entry, &mut render_result); - render_result + C::render_help(entry) } #[cfg(feature = "structural_renderer")] { #[allow(unreachable_patterns)] match program.structural_renderer_name { - super::StructuralRendererSetting::Disable => { - let mut render_result = RenderResult::default(); - C::render_help(entry, &mut render_result); - render_result - } + super::StructuralRendererSetting::Disable => C::render_help(entry), _ => RenderResult::default(), } } diff --git a/mingling_core/src/program/hook.rs b/mingling_core/src/program/hook.rs index 630baae..56d8e0e 100644 --- a/mingling_core/src/program/hook.rs +++ b/mingling_core/src/program/hook.rs @@ -718,11 +718,11 @@ mod tests { unreachable!() } - fn render(_any: crate::AnyOutput<MockHookEnum>, _r: &mut crate::RenderResult) { + fn render(_any: crate::AnyOutput<MockHookEnum>) -> crate::RenderResult { unreachable!() } - fn render_help(_any: crate::AnyOutput<MockHookEnum>, _r: &mut crate::RenderResult) { + fn render_help(_any: crate::AnyOutput<MockHookEnum>) -> crate::RenderResult { unreachable!() } diff --git a/mingling_core/src/renderer.rs b/mingling_core/src/renderer.rs index 435518d..6e67431 100644 --- a/mingling_core/src/renderer.rs +++ b/mingling_core/src/renderer.rs @@ -1,3 +1,3 @@ +pub mod render_result; #[cfg(feature = "structural_renderer")] pub mod structural; -pub mod render_result; diff --git a/mingling_core/src/renderer/render_result.rs b/mingling_core/src/renderer/render_result.rs index 5ef3120..82a745c 100644 --- a/mingling_core/src/renderer/render_result.rs +++ b/mingling_core/src/renderer/render_result.rs @@ -52,6 +52,23 @@ impl From<&RenderResult> for String { } impl RenderResult { + /// Creates a new `RenderResult` with default values (empty text and exit code 0). + /// + /// Equivalent to `RenderResult::default()`. + /// + /// # Examples + /// + /// ``` + /// use mingling_core::RenderResult; + /// + /// let result = RenderResult::new(); + /// assert_eq!(result.exit_code, 0); + /// assert!(result.is_empty()); + /// ``` + pub fn new() -> Self { + Self::default() + } + /// Appends the given text to the rendered content. /// /// # Examples diff --git a/mingling_core/src/renderer/structural.rs b/mingling_core/src/renderer/structural.rs index 16ce471..30255aa 100644 --- a/mingling_core/src/renderer/structural.rs +++ b/mingling_core/src/renderer/structural.rs @@ -1,5 +1,6 @@ use crate::{ - StructuralRendererSetting, RenderResult, renderer::structural::error::StructuralRendererSerializeError, + RenderResult, StructuralRendererSetting, + renderer::structural::error::StructuralRendererSerializeError, }; use serde::Serialize; @@ -103,9 +104,7 @@ impl StructuralRenderer { data: &T, r: &mut RenderResult, ) -> Result<(), StructuralRendererSerializeError> { - let pretty_config = ron::ser::PrettyConfig::new() - .new_line("\n") - .indentor(" "); + let pretty_config = ron::ser::PrettyConfig::new().new_line("\n").indentor(" "); let ron_string = ron::ser::to_string_pretty(data, pretty_config) .map_err(|e| StructuralRendererSerializeError::new(e.to_string()))?; @@ -123,8 +122,8 @@ impl StructuralRenderer { data: &T, r: &mut RenderResult, ) -> Result<(), StructuralRendererSerializeError> { - let toml_string = - toml::to_string(data).map_err(|e| StructuralRendererSerializeError::new(e.to_string()))?; + let toml_string = toml::to_string(data) + .map_err(|e| StructuralRendererSerializeError::new(e.to_string()))?; r.print(&toml_string); Ok(()) } @@ -196,8 +195,11 @@ mod tests { #[test] fn test_render_to_json_pretty() { let mut r = RenderResult::default(); - let result = - StructuralRenderer::render(&test_data(), &StructuralRendererSetting::JsonPretty, &mut r); + let result = StructuralRenderer::render( + &test_data(), + &StructuralRendererSetting::JsonPretty, + &mut r, + ); assert!(result.is_ok()); let output: String = r.into(); // Pretty JSON has newlines @@ -261,7 +263,8 @@ mod tests { #[test] fn test_render_dispatches_json() { let mut r = RenderResult::default(); - let result = StructuralRenderer::render(&test_data(), &StructuralRendererSetting::Json, &mut r); + let result = + StructuralRenderer::render(&test_data(), &StructuralRendererSetting::Json, &mut r); assert!(result.is_ok()); assert!(!r.is_empty()); } |
