pub trait HelpProvider {
// Required methods
fn url(&self) -> &'static str;
fn build_request(&self, cmd: &str, url: &str) -> Request;
// Provided methods
fn handle_error(&self, e: Error) -> Error { ... }
fn _fetch(&self, cmd: &str, custom_url: &Option<String>) -> Result<String> { ... }
fn fetch(&self, cmd: &str, custom_url: &Option<String>) -> Result<String> { ... }
}
Expand description
The HelpProvider
trait defines essential methods for fetching help content related to commands from a provider.
Each provider that implements this trait should provide a default URL used to retrieve the command help content. However, it also allows specifying a custom URL to override the default one.
This trait is generic and not tied to any specific command help system such as man pages or POSIX documentation, instead it relies on the implementation to define how to fetch and mark up the content.
§Methods
url
: Returns the default URL of the provider.build_req
: Uses the given command and URL to create an HTTP GET request.err_handle
: Handles possible request errors, such as a non-existent command page on the provider.fetch
: Attempts to retrieve the command page from the provider, optionally from a custom URL.
§Example
An implementation could be created for a provider that supplies help pages in Markdown format.
The url
method would return the base URL of this provider.
The build_request
method could construct a GET request for {base_url}/{command}.md
.
The handle_error
could interpret a 404 status code as ‘Command page not found’.
The fetch
would handle fetching the specified command page using the constructed request.
Required Methods§
Sourcefn build_request(&self, cmd: &str, url: &str) -> Request
fn build_request(&self, cmd: &str, url: &str) -> Request
Provided Methods§
Sourcefn handle_error(&self, e: Error) -> Error
fn handle_error(&self, e: Error) -> Error
Handle the request error. aka return a custom message if the error means that provider doesn’t have a page for the command.
Sourcefn _fetch(&self, cmd: &str, custom_url: &Option<String>) -> Result<String>
fn _fetch(&self, cmd: &str, custom_url: &Option<String>) -> Result<String>
The default fetch implementation.
This method attempts to retrieve the specified command page from the given provider.
If a custom_url
is provided, this URL is used instead of the default URL.
The method will return the content of the command page if the fetch operation is successful.
Sourcefn fetch(&self, cmd: &str, custom_url: &Option<String>) -> Result<String>
fn fetch(&self, cmd: &str, custom_url: &Option<String>) -> Result<String>
Fetches the command page from the provider.
§Parameters
cmd
: The name of the command for which the page should be fetched.custom_url
: Optional parameter that, if supplied, specifies a custom URL from which to fetch the command page.
§Returns
This method returns a Result type. On successful fetch, it contains a String
with the content of the fetched page.
In case of failure, it contains an error that provides further details about the issue encountered during the fetch operation.
§Errors
This method will return an error if the fetch operation fails.