ivandardi / RustbotPython

A Discord bot for the Community Rust Language server, written in Python.
MIT License
16 stars 9 forks source link

Add ?doc command #27

Closed jcdyer closed 2 years ago

jcdyer commented 3 years ago

I'd love to be able to use ferris to link to API documentation. I propose a new command ?doc path::to::item, which will provide a link to the named item. If path::to::item matches ^(std|core|alloc)(::|$), then it should return a link to the appropriate documentation page on https://doc.rust-lang.org, otherwise it should link to the appropriate documentation page on docs.rs.

I do not envision linking to search pages, fuzzy matching, or any other sort of guessing. If the user doesn't provide an exact match, ferris should respond with a friendly error message.

Examples:

User: ?doc core::option::Option
Ferris: https://doc.rust-lang.org/core/option/enum.Option.html
User: ?doc serde::Deserialize
Ferris: https://docs.rs/serde/latest/serde/trait.Deserialize.html
User: ?doc serde::Deser
Ferris: Could not find a page matching serde::Deser

Note: docs.rs links generally have a version number in them, but /latest/ redirects to the current version, so we can use that.

jcdyer commented 3 years ago

I'm happy to implement this, but wanted to kick the tires before I get started, see if anyone has opinions about how it should work.

kangalio commented 3 years ago

Would ?doc serde just like that work too? If yes, #4's crate commands can be dismissed in favor of this proposal

jyn514 commented 3 years ago

I'm curious why you want to avoid search. If you're ok with search this would be easy to do by linking to https://docs.rs/serde/latest/?search=serde::Deserialize. Otherwise it will be very hard because you have to know what type Deserialize is.

jcdyer commented 3 years ago

@kangalioo It seems ?doc serde should be easy to implement.

@jyn514 Linking the search page seems a little less useful, and a lot less friendly to the recipient. It's like saying "let me google that for you" instead of providing a link to a page with real information. Personally, I wouldn't use a ?doc command that returned a search page, because it feels rude.

jcdyer commented 3 years ago

And it turns out the module page has very nicely tagged links for each item in the module, so it will require two queries instead of one, but wouldn't require any guessing as to what kind of object you're looking for.

<tr class="module-item">
  <td>
    <a class="trait" href="trait.Error.html" title="serde::de::Error trait">Error</a>
  </td>
  <td class="docblock-short">
    <p>The <code>Error</code> trait allows <code>Deserialize</code> implementations to create descriptive error messages belonging to the <code>Deserializer</code> against which they are
currently running.</p>
  </td>
</tr>
jcdyer commented 3 years ago

Hmm. This doesn't account for elements below item level.

jyn514 commented 3 years ago

And more than that, you don't know if Visitor is an item or a module. So you don't know whether to look at struct.Visitor.html or Visitor/index.html. You'd have to look up each segment in the namespace one page at a time.

kangalio commented 3 years ago

Is there any reason against querying the search result page and presenting the first search result link to the user? This will require just one network request and have all the advantages of the search functionality

jcdyer commented 3 years ago

Is there any reason against querying the search result page and presenting the first search result link to the user? This will require just one network request and have all the advantages of the search functionality

That would probably work.

kangalio commented 3 years ago

Actually, that won't work like that because rustdoc seems to rely heavily on dynamic html creation using javascript, hence the search result data is not actually available from the html.

What the official Rust bot does here is, it just simply links to the search results page. It's a bit unfortunate because then the Discord preview doesn't show the actual item docs, and to access the docs you have to click twice. But it's better than nothing.

What do you think about linking to the search results page?

jyn514 commented 3 years ago

Actually, that won't work like that because rustdoc seems to rely heavily on dynamic html creation using javascript, hence the search result data is not actually available from the html.

For context, this because search has to work when viewing file:/// URLs, without a webserver present. So it's not possible to generate the HTML ahead of time, it has to be done with javascript.