refactor: Improve MagnetUri field types for spec compliance#171
refactor: Improve MagnetUri field types for spec compliance#171artrixdotdev merged 2 commits intomainfrom
MagnetUri field types for spec compliance#171Conversation
WalkthroughMagnetUri fields were changed to support multi-valued parameters and a larger length type: Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20–30 minutes
Possibly related issues
Possibly related PRs
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/libtortillas/src/metainfo/magnet.rs(2 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
crates/libtortillas/src/metainfo/magnet.rs (2)
crates/libtortillas/src/metainfo/mod.rs (2)
MetaInfo(13-16)MetaInfo(18-57)crates/libtortillas/src/metainfo/file.rs (1)
TorrentFile(17-32)
🔇 Additional comments (2)
crates/libtortillas/src/metainfo/magnet.rs (2)
33-34: No issues detected with theexact_sourcefield type change.The field is only populated through automatic serde deserialization with
ParseMode::Duplicate, which correctly handles multiple values. This change aligns with the Magnet URI specification and matches the pattern used by other multi-valued fields in the struct (web_seed,source,peer). Since there are no direct code usages of this field elsewhere in the codebase, all references are implicitly updated through the schema change.
30-31: No direct usages of thesourcefield were found in the codebase.The type change from
Option<String>toVec<String>is appropriate and safe:
- The
serdeconfiguration withrename = "as"anddefaultcorrectly handles the Magnet URI spec's multipleasparametersParseMode::Duplicateon line 70 properly collects multiple values into the vector- The public struct is only used through methods like
parse()andinfo_hash(), not through direct field access- Serialization and deserialization are handled automatically by serde
There was a problem hiding this comment.
Actionable comments posted: 0
♻️ Duplicate comments (1)
crates/libtortillas/src/metainfo/magnet.rs (1)
22-22: Portability concern:usizeis platform-dependent.The previous review suggested using
u64instead ofusizefor consistent large-file support across all platforms. This concern remains unaddressed—on 32-bit systems,usizelimits file sizes to ~4GB, the same constraint as the originalu32.
🧹 Nitpick comments (1)
crates/libtortillas/src/metainfo/magnet.rs (1)
131-140: Enhance test to validate actual values and edge cases.The test correctly validates that multiple parameters are collected into vectors. However, it only checks lengths without verifying the actual values were parsed correctly.
Consider enhancing the test to validate actual values and edge cases:
#[tokio::test] #[traced_test] async fn test_parse_magnet_uri_multi_valued_params() { let uri = "magnet:?xt=urn:btih:xyz&as=seed1&as=seed2&xs=exact1&xs=exact2&x.pe=peer1&x.pe=peer2&dn=name"; let magnet = MagnetUri::try_from(uri.to_string()).unwrap(); assert_eq!(magnet.source.len(), 2); + assert_eq!(magnet.source[0], "seed1"); + assert_eq!(magnet.source[1], "seed2"); assert_eq!(magnet.exact_source.len(), 2); + assert_eq!(magnet.exact_source[0], "exact1"); + assert_eq!(magnet.exact_source[1], "exact2"); assert_eq!(magnet.peer.len(), 2); + assert_eq!(magnet.peer[0], "peer1"); + assert_eq!(magnet.peer[1], "peer2"); } + +#[tokio::test] +#[traced_test] +async fn test_parse_magnet_uri_single_and_empty_params() { + // Test with single value and absent parameters + let uri = "magnet:?xt=urn:btih:xyz&as=single&dn=name"; + let magnet = MagnetUri::try_from(uri.to_string()).unwrap(); + + assert_eq!(magnet.source.len(), 1); + assert_eq!(magnet.source[0], "single"); + assert_eq!(magnet.exact_source.len(), 0); // default empty vec + assert_eq!(magnet.peer.len(), 0); // default empty vec +}
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
crates/libtortillas/src/metainfo/magnet.rs(3 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
crates/libtortillas/src/metainfo/magnet.rs (1)
crates/libtortillas/src/hashes.rs (1)
len(344-346)
🔇 Additional comments (3)
crates/libtortillas/src/metainfo/magnet.rs (3)
33-34: Breaking change: Verify downstream usage ofexact_sourcefield.The change from
Option<String>toVec<String>is confirmed and aligns with the multi-valued parameter support acrosssource,exact_source, andpeerfields. Codebase analysis found only test usage (line 138) already expectingVecbehavior via.len()calls. No production code breakage detected within this repository.However, since libtortillas is a published library (v0.0.0), external consumers accessing
magnet.exact_sourcewill need to adapt their code. While this pre-release version permits breaking changes, library maintainers should confirm downstream impact.
45-46: Test coverage added; internal usage is safe.The change correctly implements multi-valued parameter support for the
x.pe(peer) parameter. Test coverage validating theVec<String>type has been added at lines 131-140.No production code in the internal codebase accesses the
peerfield, so this change introduces no breaking changes within the repository scope.
30-31: Breaking change correctly implemented; no migration needed in codebase.Verification confirms there are no usage patterns within the codebase that require migration. The only
.sourcefield access found (line 137 test) already uses the Vec-compatible.len()method. No Option-based patterns (if let Some, unwrap, expect) exist on this field.This is a breaking change for external API consumers (requires SEMVER major bump), but the codebase itself is fully compatible with the new
Vec<String>type and#[serde(default)]implementation.
#168
Summary by CodeRabbit