Skip to content

feat(payload): add Payload tagged union for widget output (#18) - #21

Merged
unhappychoice merged 2 commits into
mainfrom
feat/18-payload
Apr 21, 2026
Merged

unhappychoice merged 2 commits into
mainfrom
feat/18-payload

Conversation

@unhappychoice

@unhappychoice unhappychoice commented Apr 21, 2026 •

Copy link
Copy Markdown
Owner

Summary

  • Introduces Payload — the foundation type for the 3-axis (fetcher / render / layout) architecture. Every fetcher produces one, every renderer consumes one, and the same shape is the plugin subprocess JSON wire format.
  • Eight render variants: text / list / gauge / sparkline / line_chart / bar_chart / bignum / image.
    • line_chart and bar_chart are kept separate because their underlying Ratatui widgets have fundamentally different data shapes (XY series vs labeled magnitudes).
  • Common meta fields (title, icon, status, format) flattened alongside the render-tagged body.
  • 12 tests: round-trip for each variant + spec-example-JSON parse + optional-fields-skipped + snake_case tag assertion.

Closes #18.

Wire format example

{ "render": "text", "data": { "lines": ["main"] }, "title": "Git" }
{ "render": "bar_chart", "data": { "bars": [{ "label": "a", "value": 3 }] } }

Test plan

  • cargo build
  • cargo test (12 passed)
  • cargo clippy --all-targets -- -D warnings
  • cargo fmt --check

🤖 Generated with Claude Code

Foundation type for the 3-axis (fetcher / render / layout) architecture.
Serves as both internal Rust type and plugin subprocess JSON wire format.

- Seven render variants: text / list / gauge / sparkline / chart / bignum / image
- Common meta fields: title, icon, status, format template
- Round-trip + spec-example tests for all variants

Refs #18

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Apr 21, 2026 •

Copy link
Copy Markdown
📝 Walkthrough

Walkthrough

The changes introduce foundational serialization infrastructure by adding serde and serde_json dependencies, declaring a payload module, and implementing a comprehensive Payload data structure with support for multiple render types (text, list, gauge, sparkline, chart, bignum, image), each with their own data shape, plus common metadata fields including title, icon, and status.

Changes

Cohort / File(s) Summary
Build Dependencies
Cargo.toml
Added serde with derive feature and serde_json crate at major version 1 for JSON serialization support.
Module Declaration
src/main.rs
Added module declaration for payload to enable payload definitions within the application.
Core Payload Definitions
src/payload.rs
Implemented Payload struct with optional metadata fields (title, icon, status, format) and a tagged Body enum supporting text, list, gauge, sparkline, chart, bignum, and image variants. Each variant includes its own data structure (TextData, ListData, GaugeData, SparklineData, ChartData, BignumData, ImageData). Added supporting types (Status, ChartKind, ListItem, ChartSeries) and comprehensive unit tests for JSON round-tripping and shape validation.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related issues

Poem

🐰 A payload hops into place,
With serde's graceful embrace,
Text and gauges, charts that glow—
Data structures steal the show!
JSON serialized with care,
Widget magic fills the air. ✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 76.92% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: introducing a Payload tagged union structure for widget output.
Linked Issues check ✅ Passed The pull request implements all core requirements from issue #18: Payload enum with seven render variants, proper serde tagging, common meta fields (title, icon, status, format), and comprehensive test coverage.
Out of Scope Changes check ✅ Passed All changes are directly related to implementing the Payload contract: dependency additions (serde/serde_json), module declaration, and the payload module with structures and tests.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/18-payload

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick comments (2)
src/payload.rs (2)

59-87: Numeric type inconsistency between Gauge and Chart.

GaugeData::value is f32 while ChartSeries::points uses (f64, f64). For a JSON wire format consumed across fetcher/renderer boundaries, picking a single floating-point width (typically f64, since that matches serde_json::Number precision) avoids surprises with rounding on round-trip and keeps the public data model uniform. Not a blocker, but worth aligning before this type is depended on widely.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/payload.rs` around lines 59 - 87, The GaugeData numeric type should be
aligned with the rest of the public model: change GaugeData::value from f32 to
f64 so it matches ChartSeries::points' (f64, f64) and serde_json/serde precision
expectations; update any constructors/uses of GaugeData::value (e.g., builders,
tests, deserialization helpers) to use f64 and run tests to ensure no signature
mismatches remain.

207-213: Brittle substring assertion.

json.contains("title") / "icon" will produce false positives the moment a body variant happens to contain those substrings in user-provided data (e.g., a TextData line or a ListItem::key). Safer to parse back into a serde_json::Value and assert the object does not contain those keys.

♻️ Proposed fix
     #[test]
     fn optional_fields_absent_in_serialization() {
         let p = bare(Body::Text(TextData { lines: vec![] }));
-        let json = serde_json::to_string(&p).unwrap();
-        assert!(!json.contains("title"));
-        assert!(!json.contains("icon"));
+        let v: serde_json::Value = serde_json::to_value(&p).unwrap();
+        let obj = v.as_object().unwrap();
+        assert!(!obj.contains_key("title"));
+        assert!(!obj.contains_key("icon"));
+        assert!(!obj.contains_key("status"));
+        assert!(!obj.contains_key("format"));
     }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/payload.rs` around lines 207 - 213, The test
optional_fields_absent_in_serialization is using brittle substring checks on the
serialized JSON; instead, deserialize the JSON string back into a
serde_json::Value (via serde_json::from_str) and assert the top-level object
does not contain the "title" and "icon" keys (e.g., v.get("title").is_none() and
v.get("icon").is_none()). Keep the test using the existing bare(Body::Text(...))
setup and replace the json.contains(...) assertions with these Value-based
key-absence checks to avoid false positives from user data containing those
substrings.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/payload.rs`:
- Around line 59-87: The GaugeData numeric type should be aligned with the rest
of the public model: change GaugeData::value from f32 to f64 so it matches
ChartSeries::points' (f64, f64) and serde_json/serde precision expectations;
update any constructors/uses of GaugeData::value (e.g., builders, tests,
deserialization helpers) to use f64 and run tests to ensure no signature
mismatches remain.
- Around line 207-213: The test optional_fields_absent_in_serialization is using
brittle substring checks on the serialized JSON; instead, deserialize the JSON
string back into a serde_json::Value (via serde_json::from_str) and assert the
top-level object does not contain the "title" and "icon" keys (e.g.,
v.get("title").is_none() and v.get("icon").is_none()). Keep the test using the
existing bare(Body::Text(...)) setup and replace the json.contains(...)
assertions with these Value-based key-absence checks to avoid false positives
from user data containing those substrings.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: f6e45f5c-5917-4253-880e-99df225f4fbf

📥 Commits

Reviewing files that changed from the base of the PR and between dc7e051 and 5439c44.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • Cargo.toml
  • src/main.rs
  • src/payload.rs

Align data shapes with concrete Ratatui widgets:

- LineChart: series of (x, y) points → Ratatui `Chart`
- BarChart: labeled magnitudes (label, u64) → Ratatui `BarChart`

Previously the `Chart` variant forced BarChart's labeled bars through a
LineChart-shaped series+points schema. Splitting keeps each render
type's data contract tight against its renderer.

Also widen GaugeData.value to f64 to match Ratatui's `Gauge::ratio`.

Switch the Body enum tag renaming to snake_case so LineChart / BarChart
serialize as "line_chart" / "bar_chart".

Refs #18

Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
@unhappychoice
unhappychoice merged commit 9c4a6dc into main Apr 21, 2026
@unhappychoice
unhappychoice deleted the feat/18-payload branch April 21, 2026 00:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

core: Widget data contract (Payload tagged union)

1 participant