Skip to content

refactor: Reduce sub-field render boilerplate#108

Open
zfsguy wants to merge 1 commit intostephenleo:mainfrom
zfsguy:refactor/reduce-subfield-boilerplate
Open

refactor: Reduce sub-field render boilerplate#108
zfsguy wants to merge 1 commit intostephenleo:mainfrom
zfsguy:refactor/reduce-subfield-boilerplate

Conversation

@zfsguy
Copy link

@zfsguy zfsguy commented Mar 15, 2026

Summary

  • Replace CostSubfieldConfig and ContextWindowSubfieldConfig with a single SubfieldConfig type
  • Add HasThresholdStyle trait for uniform access to style/threshold fields across parent configs
  • Add render_styled_value() helper in format.rs that centralizes the style/threshold/format rendering pipeline
  • Refactor 14 sub-field render functions in context_window.rs and cost.rs to use the new helper, reducing each from ~40-60 lines to ~15 lines
  • No behavioral changes, all existing tests pass

Files changed

File Change
src/config.rs Unified SubfieldConfig, HasThresholdStyle trait with macro
src/format.rs Added render_styled_value() with 6 unit tests
src/modules/context_window.rs 9 sub-field functions refactored to use helper
src/modules/cost.rs 5 sub-field functions refactored to use helper

Test plan

  • cargo test passes (291 unit + 65 integration)
  • cargo clippy clean
  • cargo build --release clean

@stephenleo
Copy link
Owner

stephenleo commented Mar 25, 2026

@zfsguy can you please resolve the formatting CI failure from here: https://github.com/stephenleo/cship/actions/runs/23119102465/job/67521191716?pr=108

you'll also need to merge in latest main to solve the conflict

stephenleo added a commit that referenced this pull request Mar 27, 2026
…sed HasThresholdStyle impls

- Merged CostSubfieldConfig and ContextWindowSubfieldConfig into a unified
  SubfieldConfig struct with #[cfg(test)] type aliases for backward compat.
- Added HasThresholdStyle trait and impl_has_threshold_style! macro to config.rs;
  only ContextWindowConfig gets an impl (the three unused CostConfig,
  ContextBarConfig, UsageLimitsConfig impls are intentionally absent per Story 3.1).
- Added render_styled_value() to format.rs with six unit tests covering all paths
  (format, threshold, invert_threshold, parent fallback, no-sub/no-parent).
- Replaced all inline threshold/style boilerplate in cost.rs (5 functions) and
  context_window.rs (8 functions) with render_styled_value() calls.
- cargo build, cargo test (65/65), and cargo clippy -- -D warnings all exit 0.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
stephenleo added a commit that referenced this pull request Mar 27, 2026
…sed HasThresholdStyle impls

- Merged CostSubfieldConfig and ContextWindowSubfieldConfig into a unified
  SubfieldConfig struct with #[cfg(test)] type aliases for backward compat.
- Added HasThresholdStyle trait and impl_has_threshold_style! macro to config.rs;
  only ContextWindowConfig gets an impl (the three unused CostConfig,
  ContextBarConfig, UsageLimitsConfig impls are intentionally absent per Story 3.1).
- Added render_styled_value() to format.rs with six unit tests covering all paths
  (format, threshold, invert_threshold, parent fallback, no-sub/no-parent).
- Replaced all inline threshold/style boilerplate in cost.rs (5 functions) and
  context_window.rs (8 functions) with render_styled_value() calls.
- cargo build, cargo test (65/65), and cargo clippy -- -D warnings all exit 0.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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.

2 participants