2024-08-08 15:56:09 +03:00
|
|
|
//! Library error structures and enums
|
|
|
|
|
|
|
|
/// Main error structure, also a wrapper for everything else
|
2024-08-01 09:32:55 +03:00
|
|
|
#[derive(Debug)]
|
2024-07-31 21:14:29 +03:00
|
|
|
pub enum LibError {
|
2024-08-08 15:56:09 +03:00
|
|
|
/// General I/O error
|
|
|
|
// TODO: separate somehow
|
2024-07-31 21:14:29 +03:00
|
|
|
IoError(std::io::Error),
|
2024-08-08 15:56:09 +03:00
|
|
|
/// URL parse or check error
|
2024-07-31 21:14:29 +03:00
|
|
|
InvalidUrlError(InvalidUrl),
|
2024-08-08 15:56:09 +03:00
|
|
|
/// Response status code is out of [10; 69] range
|
2024-07-31 21:14:29 +03:00
|
|
|
StatusOutOfRange(u8),
|
2024-08-08 15:56:09 +03:00
|
|
|
/// 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
|
2024-08-01 09:29:34 +03:00
|
|
|
InvalidMime(mime::FromStrError),
|
2024-07-31 21:14:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::io::Error> for LibError {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: std::io::Error) -> Self {
|
|
|
|
Self::IoError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<url::ParseError> for LibError {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: url::ParseError) -> Self {
|
|
|
|
Self::InvalidUrlError(InvalidUrl::ParseError(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<InvalidUrl> for LibError {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: InvalidUrl) -> Self {
|
|
|
|
Self::InvalidUrlError(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LibError {
|
|
|
|
#[inline]
|
|
|
|
pub fn status_out_of_range(num: u8) -> Self {
|
|
|
|
Self::StatusOutOfRange(num)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<std::string::FromUtf8Error> for LibError {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: std::string::FromUtf8Error) -> Self {
|
2024-08-08 15:56:09 +03:00
|
|
|
Self::DataNotUtf8(err)
|
2024-08-01 10:59:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 09:29:34 +03:00
|
|
|
impl From<mime::FromStrError> for LibError {
|
|
|
|
#[inline]
|
|
|
|
fn from(err: mime::FromStrError) -> Self {
|
|
|
|
Self::InvalidMime(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-08 15:56:09 +03:00
|
|
|
/// URL parse or check error
|
2024-08-01 09:32:55 +03:00
|
|
|
#[derive(Debug)]
|
2024-07-31 21:14:29 +03:00
|
|
|
pub enum InvalidUrl {
|
2024-08-08 15:56:09 +03:00
|
|
|
/// Provided string cannot be parsed as a valid URL with [`url::Url`]
|
2024-07-31 21:14:29 +03:00
|
|
|
ParseError(url::ParseError),
|
2024-08-08 15:56:09 +03:00
|
|
|
/// URL scheme is not `gemini://`
|
2024-07-31 21:14:29 +03:00
|
|
|
SchemeNotGemini,
|
2024-08-08 15:56:09 +03:00
|
|
|
/// URL contains userinfo -- `user:pswd@` --
|
|
|
|
/// which is forbidden by Gemini spec
|
2024-07-31 21:14:29 +03:00
|
|
|
UserinfoPresent,
|
2024-08-08 15:56:09 +03:00
|
|
|
/// Could not extract host from the URL or convert host and port into
|
|
|
|
/// [`std::net::SocketAddr`] or [`crate::certs::ServerName`]
|
2024-07-31 21:14:29 +03:00
|
|
|
ConvertError,
|
|
|
|
}
|