-
Notifications
You must be signed in to change notification settings - Fork 33
Add FromStr for MagnetLink #482
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
Conversation
|
I think that |
casey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, it would be good to add tests that check that the expected errors are returned when malformed magnet links are parsed.
src/magnet_link.rs
Outdated
| }); | ||
| } | ||
| let xt = v | ||
| .trim_start_matches("urn:bti") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If these aren't present it should return an error.
|
I think Is |
|
Ahh, you're right. In that case, I think FromStr is fine. |
|
Also, just thought of this, I think it's allowed for a magnet link to have multiple exact topics. There are different xt types for different networks, and you're allowed to have multiple xts: http://wiki.depthstrike.com/wiki/P2P:Protocol:Specifications:Magnet#xt_-_eXact_Topic I think the right behavior is to just grab the first |
de37e74 to
0bbb097
Compare
|
have sent a band-aid patch (#485) for the linting failures |
0bbb097 to
984e8fe
Compare
|
I just merged #485, although with some changes. Let me know when this is ready to review! |
984e8fe to
0b2ae9e
Compare
0b2ae9e to
80e611d
Compare
casey
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added a bunch of comments, mostly about error messages and formatting.
src/error.rs
Outdated
| #[snafu(display("Invalid glob: {}", source))] | ||
| GlobParse { source: globset::Error }, | ||
| #[snafu(display("Failed to parse hex string `{}`: {}", text, source))] | ||
| HexParse { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think that HexParse and InfohashParse should be moved into MagnetLinkParseError. HexParse on its own is not informative enough on the top level for a user to figure out what to do. If eventually multiple parts of the code parse infohashes, InfohashParse can be be moved back into Error. (But actually, if multiple parts of the code are parsing infohashes, then a generic Error::InfohashParse will not be specific enough for the user to know what to do, and will need separate error types.)
I'm thinking how these errors will be reported to the user, and I think in all cases the text that was being parsed should be printed. That way, the user sees the text of the magnet link that failed to parse, in addition to the error.
There are a few ways to do this. One is to have the error be a struct with a kind member that shows the actual error:
struct MagnetLinkParseError {
text: String, // magnet link that we attempted to parse
kind: MagnetLinkParseKind, // the actual error that occurred
}
enum MagnetLinkParseKind {
Url { source: url::Error},
// …
}Another is to move the text into the Error variant:
enum Error {
MagnetLinkParse {
text: String, // magnet link we attempted to parse
source: MagnetLinkParseError,
},
// …
}I think the latter approach probably the right way to go for now. It's less annoying, and if it's eventually insufficient, it can be refactored into the former.
The variant can look like this:
#[snafu(display("Failed to parse magnet link `{}`: {}", text, source))]
MagnetLinkParse { text: String, source: MagnetLinkParseError},There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see why you want MagnetLinkParse to have access to the link URL. But I think it's cleaner to put it inside every variant of MagnetLinkParseError---otherwise, we'd lose the From conversions.
I see what to do 💯
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also cool with putting it inside of the MagnetLinkParserError, I was thinking it be annoying.
src/magnet_link_parse_error.rs
Outdated
| text: String, | ||
| source: HostPortParseError, | ||
| }, | ||
| #[snafu(display("Magnet links must begin with `magnet:`"))] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalid scheme `{}`, magnet links must use the `magnet:` scheme
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something about the comma looks strange. Sure you want this exact message?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not wedded to the exact message, as long as it contains the invalid scheme. (Since I'm not sure if all users will know what a URL scheme is.)
|
|
||
| #[derive(Debug, Snafu)] | ||
| #[snafu(visibility(pub(crate)))] | ||
| pub(crate) enum MagnetLinkParseError { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some of these messages will be redundant when printed in context, i.e. with Failed to parse magnet link: from the outer Error message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests should be added that use malformed magnet links to trigger all of these error variants, and check that their contents are correct.
80e611d to
bd31ff7
Compare
|
Looks good! Can you flip the switch that lets me push to this branch? I need to push a version with my PGP signature attached in order to merge it. |
|
I just realized a couple things that could be improved:
Feel free to do these, or I can do them before I merge. |
316b102 to
f354199
Compare
excellent! 💯 💯 Also, you now have permission to clobber this branch. |
type: added
d33fc51 to
97ab785
Compare
|
Sweet, merged! Thank you! |
Another step toward #255.