refactor: clean up errors; docs: add doc-comments to error.rs

This commit is contained in:
DarkCat09 2024-08-08 16:56:09 +04:00
parent 03420c8e8d
commit 9de12169a6
Signed by: DarkCat09
GPG key ID: 0A26CD5B3345D6E3
2 changed files with 20 additions and 12 deletions

View file

@ -99,7 +99,7 @@ impl Client {
/// Check the inner error if you need to determine what exactly happened
/// - [`LibError::StatusOutOfRange`] means that a server returned an incorrect status code
/// (less than 10 or greater than 69).
/// - [`LibError::MessageNotUtf8`] is returned when metadata (the text after the status code)
/// - [`LibError::DataNotUtf8`] is returned when metadata (the text after a status code)
/// is not in UTF-8 and cannot be converted to string without errors.
pub async fn request_with_host(
&self,

View file

@ -1,10 +1,19 @@
//! Library error structures and enums
/// Main error structure, also a wrapper for everything else
#[derive(Debug)]
pub enum LibError {
/// General I/O error
// TODO: separate somehow
IoError(std::io::Error),
/// URL parse or check error
InvalidUrlError(InvalidUrl),
/// Response status code is out of [10; 69] range
StatusOutOfRange(u8),
MessageNotUtf8(std::string::FromUtf8Error),
BodyNotUtf8(std::str::Utf8Error),
/// Response metadata or content cannot be parsed
/// as a UTF-8 string without errors
DataNotUtf8(std::string::FromUtf8Error),
/// Provided string is not a valid MIME type
InvalidMime(mime::FromStrError),
}
@ -39,14 +48,7 @@ impl LibError {
impl From<std::string::FromUtf8Error> for LibError {
#[inline]
fn from(err: std::string::FromUtf8Error) -> Self {
Self::MessageNotUtf8(err)
}
}
impl From<std::str::Utf8Error> for LibError {
#[inline]
fn from(err: std::str::Utf8Error) -> Self {
Self::BodyNotUtf8(err)
Self::DataNotUtf8(err)
}
}
@ -57,11 +59,17 @@ impl From<mime::FromStrError> for LibError {
}
}
/// URL parse or check error
#[derive(Debug)]
pub enum InvalidUrl {
/// Provided string cannot be parsed as a valid URL with [`url::Url`]
ParseError(url::ParseError),
/// URL scheme is not `gemini://`
SchemeNotGemini,
/// URL contains userinfo -- `user:pswd@` --
/// which is forbidden by Gemini spec
UserinfoPresent,
NoHostFound,
/// Could not extract host from the URL or convert host and port into
/// [`std::net::SocketAddr`] or [`crate::certs::ServerName`]
ConvertError,
}