Skip to content

Commit

Permalink
Implement std::error::Error and rename to Error
Browse files Browse the repository at this point in the history
  • Loading branch information
spk committed Mar 8, 2020
1 parent df30fe6 commit b2525b5
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
15 changes: 8 additions & 7 deletions src/http/reqwest/async_reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
use reqwest::{Client, Request};
use reqwest::{Method, Error};
use reqwest::Method;
use reqwest::Error as ReqwestError;
use reqwest::header::HeaderValue;
use url::{Origin, Url};
use reqwest::header::USER_AGENT;
use crate::http::{RobotsTxtClient, DEFAULT_USER_AGENT};
use crate::parser::{ParseResult, parse_fetched_robots_txt};
use crate::model::FetchedRobotsTxt;
use crate::model::{RobotparserError, ErrorKind};
use crate::model::{Error, ErrorKind};
use std::pin::Pin;
use futures::task::{Context, Poll};
use futures::Future;
use futures::future::TryFutureExt;
use futures::future::ok as future_ok;

type FetchFuture = Box<dyn Future<Output=Result<(ResponseInfo, String), Error>>>;
type FetchFuture = Box<dyn Future<Output=Result<(ResponseInfo, String), ReqwestError>>>;

impl RobotsTxtClient for Client {
type Result = Result<RobotsTxtResponse, RobotparserError>;
type Result = Result<RobotsTxtResponse, Error>;
fn fetch_robots_txt(&self, origin: Origin) -> Self::Result {
let url = format!("{}/robots.txt", origin.unicode_serialization());
let url = Url::parse(&url).map_err(|err| RobotparserError {kind: ErrorKind::Url(err)})?;
let url = Url::parse(&url).map_err(|err| Error {kind: ErrorKind::Url(err)})?;
let mut request = Request::new(Method::GET, url);
let _ = request.headers_mut().insert(USER_AGENT, HeaderValue::from_static(DEFAULT_USER_AGENT));
let response = self
Expand All @@ -30,7 +31,7 @@ impl RobotsTxtClient for Client {
return future_ok((response_info, response_text));
});
});
let response: Pin<Box<dyn Future<Output=Result<(ResponseInfo, String), Error>>>> = Box::pin(response);
let response: Pin<Box<dyn Future<Output=Result<(ResponseInfo, String), ReqwestError>>>> = Box::pin(response);
Ok(RobotsTxtResponse {
origin,
response,
Expand All @@ -56,7 +57,7 @@ impl RobotsTxtResponse {
}

impl Future for RobotsTxtResponse {
type Output = Result<ParseResult<FetchedRobotsTxt>, Error>;
type Output = Result<ParseResult<FetchedRobotsTxt>, ReqwestError>;

fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let self_mut = self.get_mut();
Expand Down
10 changes: 5 additions & 5 deletions src/http/reqwest/sync_reqwest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ use reqwest::header::USER_AGENT;
use crate::http::{RobotsTxtClient, DEFAULT_USER_AGENT};
use crate::parser::{ParseResult, parse_fetched_robots_txt};
use crate::model::FetchedRobotsTxt;
use crate::model::{RobotparserError, ErrorKind};
use crate::model::{Error, ErrorKind};

impl RobotsTxtClient for Client {
type Result = Result<ParseResult<FetchedRobotsTxt>, RobotparserError>;
type Result = Result<ParseResult<FetchedRobotsTxt>, Error>;
fn fetch_robots_txt(&self, origin: Origin) -> Self::Result {
let url = format!("{}/robots.txt", origin.unicode_serialization());
let url = Url::parse(&url).map_err(|err| RobotparserError {kind: ErrorKind::Url(err)})?;
let url = Url::parse(&url).map_err(|err| Error {kind: ErrorKind::Url(err)})?;
let mut request = Request::new(Method::GET, url);
let _ = request.headers_mut().insert(USER_AGENT, HeaderValue::from_static(DEFAULT_USER_AGENT));
let response = self.execute(request).map_err(|err| RobotparserError {kind: ErrorKind::Http(err)})?;
let response = self.execute(request).map_err(|err| Error {kind: ErrorKind::Http(err)})?;
let status_code = response.status().as_u16();
let text = response.text().map_err(|err| RobotparserError {kind: ErrorKind::Http(err)})?;
let text = response.text().map_err(|err| Error {kind: ErrorKind::Http(err)})?;
let robots_txt = parse_fetched_robots_txt(origin, status_code, &text);
return Ok(robots_txt);
}
Expand Down
2 changes: 1 addition & 1 deletion src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ pub use self::robots_txt::RobotsTxt;
mod path;
pub (crate) use self::path::Path;
mod errors;
pub use self::errors::{RobotparserError, ErrorKind};
pub use self::errors::{Error, ErrorKind};
6 changes: 4 additions & 2 deletions src/model/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::fmt;

#[derive(Debug)]
pub struct RobotparserError {
pub struct Error {
pub kind: ErrorKind,
}

Expand All @@ -11,11 +11,13 @@ pub enum ErrorKind {
Http(reqwest::Error),
}

impl fmt::Display for RobotparserError {
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.kind {
ErrorKind::Url(ref err) => err.fmt(f),
ErrorKind::Http(ref err) => err.fmt(f),
}
}
}

impl std::error::Error for Error {}

0 comments on commit b2525b5

Please sign in to comment.