mirror of
https://github.com/Hopiu/lychee.git
synced 2026-03-17 05:00:26 +00:00
Apply suggestions
This commit is contained in:
parent
1fe6f2f1be
commit
c41cd5d6b9
4 changed files with 25 additions and 25 deletions
|
|
@ -4,8 +4,8 @@ use crate::Status;
|
|||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub(crate) enum ChainResult<T, R> {
|
||||
Chained(T),
|
||||
EarlyExit(R),
|
||||
Next(T),
|
||||
Done(R),
|
||||
}
|
||||
|
||||
pub(crate) type RequestChain = Chain<reqwest::Request, Status>;
|
||||
|
|
@ -31,15 +31,15 @@ impl<T, R> Chain<T, R> {
|
|||
pub(crate) fn traverse(&mut self, mut input: T) -> ChainResult<T, R> {
|
||||
use ChainResult::*;
|
||||
for e in self.0.iter_mut() {
|
||||
match e.handle(input) {
|
||||
Chained(r) => input = r,
|
||||
EarlyExit(r) => {
|
||||
return EarlyExit(r);
|
||||
match e.chain(input) {
|
||||
Next(r) => input = r,
|
||||
Done(r) => {
|
||||
return Done(r);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Chained(input)
|
||||
Next(input)
|
||||
}
|
||||
|
||||
pub(crate) fn push(&mut self, value: Box<dyn Chainable<T, R> + Send>) {
|
||||
|
|
@ -48,25 +48,25 @@ impl<T, R> Chain<T, R> {
|
|||
}
|
||||
|
||||
pub(crate) trait Chainable<T, R>: Debug {
|
||||
fn handle(&mut self, input: T) -> ChainResult<T, R>;
|
||||
fn chain(&mut self, input: T) -> ChainResult<T, R>;
|
||||
}
|
||||
|
||||
mod test {
|
||||
use super::{ChainResult, ChainResult::*, Chainable};
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Add(i64);
|
||||
struct Add(usize);
|
||||
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
struct Result(i64);
|
||||
struct Result(usize);
|
||||
|
||||
impl Chainable<Result, Result> for Add {
|
||||
fn handle(&mut self, req: Result) -> ChainResult<Result, Result> {
|
||||
fn chain(&mut self, req: Result) -> ChainResult<Result, Result> {
|
||||
let added = req.0 + self.0;
|
||||
if added > 100 {
|
||||
EarlyExit(Result(req.0))
|
||||
Done(Result(req.0))
|
||||
} else {
|
||||
Chained(Result(added))
|
||||
Next(Result(added))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -75,9 +75,9 @@ mod test {
|
|||
fn simple_chain() {
|
||||
use super::Chain;
|
||||
let mut chain: Chain<Result, Result> =
|
||||
Chain::new(vec![Box::new(Add(10)), Box::new(Add(-3))]);
|
||||
Chain::new(vec![Box::new(Add(7)), Box::new(Add(3))]);
|
||||
let result = chain.traverse(Result(0));
|
||||
assert_eq!(result, Chained(Result(7)));
|
||||
assert_eq!(result, Next(Result(10)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
@ -86,6 +86,6 @@ mod test {
|
|||
let mut chain: Chain<Result, Result> =
|
||||
Chain::new(vec![Box::new(Add(80)), Box::new(Add(30)), Box::new(Add(1))]);
|
||||
let result = chain.traverse(Result(0));
|
||||
assert_eq!(result, EarlyExit(Result(80)));
|
||||
assert_eq!(result, Done(Result(80)));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ use typed_builder::TypedBuilder;
|
|||
use crate::{
|
||||
chain::{
|
||||
Chain,
|
||||
ChainResult::{Chained, EarlyExit},
|
||||
ChainResult::{Next, Done},
|
||||
RequestChain,
|
||||
},
|
||||
filter::{Excludes, Filter, Includes},
|
||||
|
|
@ -676,8 +676,8 @@ impl Client {
|
|||
let result = request_chain.traverse(request);
|
||||
|
||||
match result {
|
||||
EarlyExit(status) => status,
|
||||
Chained(r) => match self.reqwest_client.execute(r).await {
|
||||
Done(status) => status,
|
||||
Next(r) => match self.reqwest_client.execute(r).await {
|
||||
Ok(ref response) => Status::new(response, self.accepted.clone()),
|
||||
Err(e) => e.into(),
|
||||
},
|
||||
|
|
@ -1121,8 +1121,8 @@ mod tests {
|
|||
struct ExampleHandler();
|
||||
|
||||
impl Chainable<Request, Status> for ExampleHandler {
|
||||
fn handle(&mut self, _: Request) -> ChainResult<Request, Status> {
|
||||
ChainResult::EarlyExit(Status::Excluded)
|
||||
fn chain(&mut self, _: Request) -> ChainResult<Request, Status> {
|
||||
ChainResult::Done(Status::Excluded)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -91,8 +91,8 @@ impl Quirks {
|
|||
}
|
||||
|
||||
impl Chainable<Request, Status> for Quirks {
|
||||
fn handle(&mut self, input: Request) -> ChainResult<Request, Status> {
|
||||
ChainResult::Chained(self.apply(input))
|
||||
fn chain(&mut self, input: Request) -> ChainResult<Request, Status> {
|
||||
ChainResult::Next(self.apply(input))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -74,10 +74,10 @@ impl BasicAuthCredentials {
|
|||
}
|
||||
|
||||
impl Chainable<Request, Status> for BasicAuthCredentials {
|
||||
fn handle(&mut self, mut request: Request) -> ChainResult<Request, Status> {
|
||||
fn chain(&mut self, mut request: Request) -> ChainResult<Request, Status> {
|
||||
request
|
||||
.headers_mut()
|
||||
.append(AUTHORIZATION, self.to_authorization().0.encode());
|
||||
ChainResult::Chained(request)
|
||||
ChainResult::Next(request)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue