tokio-gemini/src/error.rs

76 lines
1.9 KiB
Rust
Raw Normal View History

//! 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 {
/// General I/O error
// TODO: separate somehow
2024-07-31 21:14:29 +03:00
IoError(std::io::Error),
/// URL parse or check error
2024-07-31 21:14:29 +03:00
InvalidUrlError(InvalidUrl),
/// Response status code is out of [10; 69] range
2024-07-31 21:14:29 +03:00
StatusOutOfRange(u8),
/// 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),
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 {
Self::DataNotUtf8(err)
}
}
impl From<mime::FromStrError> for LibError {
#[inline]
fn from(err: mime::FromStrError) -> Self {
Self::InvalidMime(err)
}
}
/// 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 {
/// Provided string cannot be parsed as a valid URL with [`url::Url`]
2024-07-31 21:14:29 +03:00
ParseError(url::ParseError),
/// URL scheme is not `gemini://`
2024-07-31 21:14:29 +03:00
SchemeNotGemini,
/// URL contains userinfo -- `user:pswd@` --
/// which is forbidden by Gemini spec
2024-07-31 21:14:29 +03:00
UserinfoPresent,
/// 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,
}