mirror of
https://github.com/Hopiu/lychee.git
synced 2026-05-12 15:53:11 +00:00
Rename Chainable to Handler
This commit is contained in:
parent
e3a236b257
commit
ddcca65e72
6 changed files with 21 additions and 21 deletions
|
|
@ -3,11 +3,11 @@
|
|||
//! lychee is based on a chain of responsibility, where each handler can modify
|
||||
//! a request and decide if it should be passed to the next element or not.
|
||||
//!
|
||||
//! The chain is implemented as a vector of [`Chainable`] handlers. It is
|
||||
//! The chain is implemented as a vector of [`Handler`] handlers. It is
|
||||
//! traversed by calling [`Chain::traverse`], which will call
|
||||
//! [`Chainable::chain`] on each handler in the chain consecutively.
|
||||
//! [`Handler::chain`] on each handler in the chain consecutively.
|
||||
//!
|
||||
//! To add external handlers, you can implement the [`Chainable`] trait and add
|
||||
//! To add external handlers, you can implement the [`Handler`] trait and add
|
||||
//! the handler to the chain.
|
||||
//!
|
||||
//! [pattern]: https://github.com/lpxxn/rust-design-pattern/blob/master/behavioral/chain_of_responsibility.rs
|
||||
|
|
@ -44,10 +44,10 @@ pub(crate) type RequestChain = Chain<reqwest::Request, Status>;
|
|||
/// This holds all handlers, which were chained together.
|
||||
/// Handlers are traversed in order.
|
||||
///
|
||||
/// Each handler needs to implement the `Chainable` trait and be `Send`, because
|
||||
/// Each handler needs to implement the `Handler` trait and be `Send`, because
|
||||
/// the chain is traversed concurrently and the handlers can be sent between
|
||||
/// threads.
|
||||
pub(crate) type InnerChain<T, R> = Vec<Box<dyn Chainable<T, R> + Send>>;
|
||||
pub(crate) type InnerChain<T, R> = Vec<Box<dyn Handler<T, R> + Send>>;
|
||||
|
||||
/// The outer chain type.
|
||||
///
|
||||
|
|
@ -99,7 +99,7 @@ impl<T, R> Chain<T, R> {
|
|||
}
|
||||
}
|
||||
|
||||
/// Chainable trait for implementing request handlers
|
||||
/// Handler trait for implementing request handlers
|
||||
///
|
||||
/// This trait needs to be implemented by all chainable handlers.
|
||||
/// It is the only requirement to handle requests in lychee.
|
||||
|
|
@ -112,7 +112,7 @@ impl<T, R> Chain<T, R> {
|
|||
/// handler. This allows for modifying the request, such as adding headers or
|
||||
/// changing the URL (e.g. for remapping or filtering).
|
||||
#[async_trait]
|
||||
pub trait Chainable<T, R>: Debug {
|
||||
pub trait Handler<T, R>: Debug {
|
||||
/// Given an input request, return a [`ChainResult`] to continue or stop the
|
||||
/// chain.
|
||||
///
|
||||
|
|
@ -122,7 +122,7 @@ pub trait Chainable<T, R>: Debug {
|
|||
/// # Example
|
||||
///
|
||||
/// ```
|
||||
/// use lychee_lib::{Chainable, ChainResult, Status};
|
||||
/// use lychee_lib::{Handler, ChainResult, Status};
|
||||
/// use reqwest::Request;
|
||||
/// use async_trait::async_trait;
|
||||
///
|
||||
|
|
@ -130,7 +130,7 @@ pub trait Chainable<T, R>: Debug {
|
|||
/// struct AddHeader;
|
||||
///
|
||||
/// #[async_trait]
|
||||
/// impl Chainable<Request, Status> for AddHeader {
|
||||
/// impl Handler<Request, Status> for AddHeader {
|
||||
/// async fn chain(&mut self, mut request: Request) -> ChainResult<Request, Status> {
|
||||
/// // You can modify the request however you like here
|
||||
/// request.headers_mut().append("X-Header", "value".parse().unwrap());
|
||||
|
|
@ -183,7 +183,7 @@ mod test {
|
|||
use super::{
|
||||
ChainResult,
|
||||
ChainResult::{Done, Next},
|
||||
Chainable,
|
||||
Handler,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
|
||||
|
|
@ -194,7 +194,7 @@ mod test {
|
|||
struct Result(usize);
|
||||
|
||||
#[async_trait]
|
||||
impl Chainable<Result, Result> for Add {
|
||||
impl Handler<Result, Result> for Add {
|
||||
async fn chain(&mut self, req: Result) -> ChainResult<Result, Result> {
|
||||
let added = req.0 + self.0;
|
||||
if added > 100 {
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
chain::{ChainResult, Chainable},
|
||||
chain::{ChainResult, Handler},
|
||||
retry::RetryExt,
|
||||
Status,
|
||||
};
|
||||
|
|
@ -73,7 +73,7 @@ fn clone_unwrap(request: &Request) -> Request {
|
|||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Chainable<Request, Status> for Checker {
|
||||
impl Handler<Request, Status> for Checker {
|
||||
async fn chain(&mut self, input: Request) -> ChainResult<Request, Status> {
|
||||
ChainResult::Done(self.retry_request(input).await)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -750,7 +750,7 @@ mod tests {
|
|||
|
||||
use super::ClientBuilder;
|
||||
use crate::{
|
||||
chain::{ChainResult, Chainable, RequestChain},
|
||||
chain::{ChainResult, Handler, RequestChain},
|
||||
mock_server,
|
||||
test_utils::get_mock_client_response,
|
||||
Request, Status, Uri,
|
||||
|
|
@ -1086,7 +1086,7 @@ mod tests {
|
|||
struct ExampleHandler();
|
||||
|
||||
#[async_trait]
|
||||
impl Chainable<Request, Status> for ExampleHandler {
|
||||
impl Handler<Request, Status> for ExampleHandler {
|
||||
async fn chain(&mut self, _: Request) -> ChainResult<Request, Status> {
|
||||
ChainResult::Done(Status::Excluded)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -85,8 +85,8 @@ use openssl_sys as _; // required for vendored-openssl feature
|
|||
#[doc(inline)]
|
||||
pub use crate::{
|
||||
basic_auth::BasicAuthExtractor,
|
||||
// Expose the `Chainable` trait to allow defining external handlers (plugins)
|
||||
chain::{ChainResult, Chainable},
|
||||
// Expose the `Handler` trait to allow defining external handlers (plugins)
|
||||
chain::{ChainResult, Handler},
|
||||
// Constants get exposed so that the CLI can use the same defaults as the library
|
||||
client::{
|
||||
check, Client, ClientBuilder, DEFAULT_MAX_REDIRECTS, DEFAULT_MAX_RETRIES,
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
chain::{ChainResult, Chainable},
|
||||
chain::{ChainResult, Handler},
|
||||
Status,
|
||||
};
|
||||
use async_trait::async_trait;
|
||||
|
|
@ -92,7 +92,7 @@ impl Quirks {
|
|||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Chainable<Request, Status> for Quirks {
|
||||
impl Handler<Request, Status> for Quirks {
|
||||
async fn chain(&mut self, input: Request) -> ChainResult<Request, Status> {
|
||||
ChainResult::Next(self.apply(input))
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use reqwest::Request;
|
|||
use serde::Deserialize;
|
||||
use thiserror::Error;
|
||||
|
||||
use crate::chain::{ChainResult, Chainable};
|
||||
use crate::chain::{ChainResult, Handler};
|
||||
use crate::Status;
|
||||
|
||||
#[derive(Copy, Clone, Debug, Error, PartialEq)]
|
||||
|
|
@ -75,7 +75,7 @@ impl BasicAuthCredentials {
|
|||
}
|
||||
|
||||
#[async_trait]
|
||||
impl Chainable<Request, Status> for Option<BasicAuthCredentials> {
|
||||
impl Handler<Request, Status> for Option<BasicAuthCredentials> {
|
||||
async fn chain(&mut self, mut request: Request) -> ChainResult<Request, Status> {
|
||||
if let Some(credentials) = self {
|
||||
request
|
||||
|
|
|
|||
Loading…
Reference in a new issue