aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author魏曹先生 <1992414357@qq.com>2026-07-18 00:44:24 +0800
committer魏曹先生 <1992414357@qq.com>2026-07-18 00:44:24 +0800
commitb55c1815f175dec07d94c8f7f2dbbd01d4832caa (patch)
tree4bb8b456cc26f4b02cdb799c864f680e4aca7bde
parente938129acc6231b2224db793c7aa9d6f95f7d0bf (diff)
feat(core): add multiple From implementations for RenderResult
Implement `From<()>`, integer types, String, &String, and &str to allow renderers to return simple values directly
-rw-r--r--CHANGELOG.md8
-rw-r--r--mingling_core/src/renderer/render_result.rs50
2 files changed, 58 insertions, 0 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index ac00643..a3493b0 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -108,6 +108,14 @@ None
_No migration is required for existing `parser` users — the old API continues to work unchanged._
+4. **[`core`]** Added multiple `From` implementations for `RenderResult`:
+
+ - **From `()`** — Allows constructing an empty `RenderResult` from a unit value, enabling ergonomic returns like `fn my_renderer() -> RenderResult { }` (via `}` → `}` with implicit `()` return).
+ - **From integer types** (`i32`, `i16`, `i8`, `u32`, `u16`, `u8`, `usize`) — Allows constructing a `RenderResult` with a specific exit code and empty text, enabling `fn my_renderer() -> RenderResult { 0 }` or `42.into()`.
+ - **From `String`**, **`&String`**, and **`&str`** — Allows constructing a `RenderResult` with the given text and exit code `0`, enabling `fn my_renderer() -> RenderResult { "Hello".into() }` or passing a `String` directly.
+
+ These implementations make `RenderResult` more flexible as a return type, allowing renderer functions to return simple values without manually constructing a `RenderResult` via `new()` and `write!`/`writeln!`.
+
#### **BREAKING CHANGES** (API CHANGES):
1. **[`macros:renderer`]** **[`macros:help`]** Removed `r_println!` and `r_print!` macros. The `#[renderer]` and `#[help]` macros no longer implicitly inject an internal `RenderResult` variable or provide `r_println!` / `r_print!` macros.
diff --git a/mingling_core/src/renderer/render_result.rs b/mingling_core/src/renderer/render_result.rs
index 82a745c..e57a5b9 100644
--- a/mingling_core/src/renderer/render_result.rs
+++ b/mingling_core/src/renderer/render_result.rs
@@ -39,6 +39,56 @@ impl Deref for RenderResult {
}
}
+impl From<()> for RenderResult {
+ fn from(_value: ()) -> Self {
+ RenderResult::new()
+ }
+}
+
+macro_rules! impl_from_int {
+ ($($ty:ty),+ $(,)?) => {
+ $(
+ impl From<$ty> for RenderResult {
+ fn from(exit_code: $ty) -> Self {
+ RenderResult {
+ exit_code: exit_code as i32,
+ ..Default::default()
+ }
+ }
+ }
+ )+
+ };
+}
+
+impl_from_int!(i32, i16, i8, u32, u16, u8, usize);
+
+impl From<&String> for RenderResult {
+ fn from(value: &String) -> Self {
+ RenderResult {
+ render_text: value.clone(),
+ exit_code: 0,
+ }
+ }
+}
+
+impl From<String> for RenderResult {
+ fn from(value: String) -> Self {
+ RenderResult {
+ render_text: value,
+ exit_code: 0,
+ }
+ }
+}
+
+impl From<&str> for RenderResult {
+ fn from(value: &str) -> Self {
+ RenderResult {
+ render_text: value.to_string(),
+ exit_code: 0,
+ }
+ }
+}
+
impl From<RenderResult> for String {
fn from(result: RenderResult) -> Self {
result.render_text