-
Notifications
You must be signed in to change notification settings - Fork 0
refactor: DRY improvements with builder helpers and namespace detection #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This commit implements the low-hanging fruit refactorings from the DRY analysis report, focusing on eliminating code duplication with minimal risk. Changes: 1. Unify bytes_to_string() function - Moved implementation to util/text.rs as single source of truth - Removed duplicate implementations from parser/common.rs and types/common.rs - Added comprehensive documentation with examples - Re-exported from parser/common for backward compatibility - Lines saved: ~8 2. Add Person::from_name() helper method - Added convenience constructor for creating Person from just a name - Updated all 4 occurrences in namespace/dublin_core.rs to use helper - Reduces boilerplate and ensures consistent Person construction - Lines saved: ~10 3. Use Tag::new() builder method consistently - Tag::new() already existed but wasn't used everywhere - Updated 4 occurrences across namespace modules to use builder - Replaced verbose struct literal construction with concise builder - Lines saved: ~12 4. Add HTTP header helper method - Added insert_header() helper to reduce boilerplate in FeedHttpClient - Centralizes error handling for header value conversion - Updated get() method to use helper for User-Agent, ETag, and Last-Modified - Consistent error messages across all header insertions - Lines saved: ~15 Total impact: - Lines reduced: ~45 - Files modified: 6 - Test coverage: All 302 tests pass - Clippy: Clean with -D warnings - Risk level: Low (backward compatible changes only) These changes improve code maintainability and reduce the risk of inconsistent behavior when similar operations are performed in different parts of the codebase.
Add 3 tests for the new insert_header() helper method: - test_insert_header_valid: verifies basic functionality - test_insert_header_invalid_value: verifies error handling for invalid headers - test_insert_header_multiple_headers: verifies multiple header insertions
Phase 2: Feed/Entry builder helper methods - Added 6 FeedMeta helpers: set_title, set_subtitle, set_rights, set_generator, set_author, set_publisher - Added 4 Entry helpers: set_title, set_summary, set_author, set_publisher - Updated atom.rs and json.rs to use helpers - Net reduction: 11 lines in parsers Phase 3: Infrastructure for namespace consolidation - Created parser/namespace_detection.rs (216 lines) - Implemented NamespacePrefix struct with const construction - Added 5 namespace constants: DC, CONTENT, MEDIA, ITUNES, PODCAST - Added 6 unit tests for namespace detection Phase 4: Buffer factory functions - Added new_event_buffer() factory in common.rs - Added new_text_buffer() factory in common.rs - Semantic naming for consistent buffer creation Impact: - 315/315 tests passing - Zero clippy warnings - 300+ lines of infrastructure added - Foundation for future namespace consolidation - All changes backward compatible
- Use std::mem::take() instead of clone() for zero-cost string moves - Enhance unsafe block documentation with detailed safety invariants - Add ignore attribute to doc tests for private modules - Add explicit fn main() wrapper to set_generator doc test All 315 tests pass, zero clippy warnings.
Codecov Report❌ Patch coverage is ❌ Your patch status has failed because the patch coverage (72.87%) is below the target coverage (80.00%). You can increase the patch coverage or adjust the target coverage. @@ Coverage Diff @@
## main #9 +/- ##
==========================================
+ Coverage 81.07% 81.18% +0.10%
==========================================
Files 25 27 +2
Lines 4053 4145 +92
==========================================
+ Hits 3286 3365 +79
- Misses 767 780 +13
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements comprehensive DRY (Don't Repeat Yourself) refactoring across the feedparser-rs codebase, introducing reusable abstractions and builder-style helper methods.
Changes
DRY Refactoring (Phases 1-4)
FeedMetaandEntrytypes:set_title(),set_subtitle(),set_rights(),set_generator()set_author(),set_publisher(),set_summary()NamespacePrefixabstraction for efficient XML namespace tag detectionnew_event_buffer,new_text_buffer) for consistent capacity allocationPerformance Optimizations
std::mem::take()instead of.clone()in helper methods for zero-cost string moves#[inline]for compiler optimizationSecurity Improvements
unsafeblock inprefix()methodTesting Improvements
ignoreattribute to doc tests for private modulesinsert_headerhelperCI/CD Improvements
Test plan
cargo nextest run- 315 tests passcargo clippy --all-targets -- -D warnings- No warningscargo test --doc- 50 doc tests pass, 10 ignored (private modules)Files changed
crates/feedparser-rs-core/src/types/feed.rs- FeedMeta helper methodscrates/feedparser-rs-core/src/types/entry.rs- Entry helper methodscrates/feedparser-rs-core/src/parser/namespace_detection.rs- NamespacePrefix abstractioncrates/feedparser-rs-core/src/parser/common.rs- Buffer factory functions