Skip to content
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

Add support for BTv2 magnet links #72

Merged
merged 1 commit into from
Jan 8, 2024

Conversation

jabedude
Copy link
Contributor

This is a tiny step to supporting BEP52 (#70). Ideally the new InfoHash type would replace Id20 in Magnet, but that would be a significant refactor across the project and it's not required until we actually begin performing lookups with SHA-256 hashes

@ikatson
Copy link
Owner

ikatson commented Dec 27, 2023

Hey thanks for this, I'm traveling with almost no internet and only a phone, so will take a look in a few days when I come back.

Copy link
Owner

@ikatson ikatson left a comment

Choose a reason for hiding this comment

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

Hey, first pass mostly LGTM, see the comment.
Without that suggestion implemented there'll just be a dead struct in the code

crates/librqbit_core/src/magnet.rs Outdated Show resolved Hide resolved
@ikatson
Copy link
Owner

ikatson commented Jan 1, 2024

@jabedude left a few pretty significant design comments

@jabedude jabedude force-pushed the bep52-magnet-support branch from ed872f6 to 091aef5 Compare January 3, 2024 04:22
@ikatson
Copy link
Owner

ikatson commented Jan 3, 2024

Code LGTM so far, but:

  • I assume you want to change Magnet too, so I'll wait for it before merging
  • A bunch of tests got broken by this PR, PTAL

@jabedude jabedude force-pushed the bep52-magnet-support branch from 091aef5 to 0836b65 Compare January 8, 2024 02:57
@jabedude
Copy link
Contributor Author

jabedude commented Jan 8, 2024

Tests fixed, changed Magnet to look how we discussed


fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(formatter, "a 20 byte slice or a 40 byte string")
formatter.write_str("a byte array of length ")
Copy link
Owner

Choose a reason for hiding this comment

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

nit: could have just done formatter.write_fmt(format_args!("a byte array of length {}", N)))

match info_hash {
Some(info_hash) => Ok(Magnet {
info_hash,
match info_hash_found {
Copy link
Owner

Choose a reason for hiding this comment

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

nit: you don't need a separate variable to track this, could have just check if at least one of id20 and id32 is set

self.info_hash.as_string(),
self.trackers.join("&tr=")
)
if let (Some(id20), Some(id32)) = (self.id20, self.id32) {
Copy link
Owner

Choose a reason for hiding this comment

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

nit: it's a bit repetitive to handle all cases like this, you could have done this without a big branch, but small branches instead:

write!(f, "magnet:?")?;
if let Some(id20) = self.id20 {
    write!("&...");
}
if let Some(id32) = self.i32 {
    write!("&...");
}
write!("&tr={}", self.trackers.join("&tr="))?;

id32.as_string(),
self.trackers.join("&tr=")
)
} else {
Copy link
Owner

Choose a reason for hiding this comment

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

please don't panic in Display trait. If you restructure the code like I suggested above, let it just print a magnet without hashes

@ikatson
Copy link
Owner

ikatson commented Jan 8, 2024

All comments are minor, we can fix them later, so merging

@ikatson ikatson merged commit c8189de into ikatson:main Jan 8, 2024
4 checks passed
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