Skip to content

Builder pattern for Checker #12

Closed
Closed

Description

@mre

At the moment we construct the Checker object (which takes care of actually checking the links) with a basic constructor:
https://github.com/hello-rust/lychee/blob/812c5972a3aa6004273a9fe512b95c7f9a3da91a/src/checker.rs#L293-L310

The readability and maintainability could be greatly improved e.g. by using a builder pattern.
This way, the individuals fields would be more understandable and we could omit the ones where the defaults work.

I had good experiences with derive_builder in the past
Here is an example from their docs:

#[macro_use]
extern crate derive_builder;

#[derive(Default, Builder, Debug)]
#[builder(setter(into))]
struct Channel {
    token: i32,
    special_info: i32,
    // .. a whole bunch of other fields ..
}

fn main() {
    // builder pattern, go, go, go!...
    let ch = ChannelBuilder::default()
        .special_info(42u8)
        .token(19124)
        .build()
        .unwrap();
    println!("{:?}", ch);
}

We could use something similar for our Checker:

 let checker = Checker::default() 
         .token("DUMMY_GITHUB_TOKEN".to_string()) 
         .max_redirects(5)
         .user_agent("curl/7.71.1".to_string())
         .build()?;

This would clean up a lot of lines in our test code and throughout the application.
If anyone wants to tackle this, please add a comment here.

Activity

  1. added
    enhancement
    good first issue
    help wanted
    on Oct 16, 2020
  2. kashifsoofi commented on Oct 16, 2020

    @kashifsoofi

    Can I help on this.

  3. mre commented on Oct 16, 2020

    @mre
    MemberAuthor

    Sure. It's all yours. Thanks for looking into it. 😊

  4. kashifsoofi commented on Oct 16, 2020

    @kashifsoofi

    Sorry spoke too soon, i have begun reading rust lang book to learn rust and i though i can handle this but apparently its a bit advanced for my current rust level.

  5. pawroman commented on Oct 18, 2020

    @pawroman
    Member

    I have done some exploration and looks like rust-typed-builder could be an alternative to derive_builder worth considering.

    One caveat I see in our case is that not all fields are trivially instantiated (e.g. reqwest::Client requires using a builder). Having a way to initialize it in the generated build method would be ideal, but I'm not sure if any of these libraries have that capability out of the box.

  6. mre commented on Oct 18, 2020

    @mre
    MemberAuthor

    Not sure if it helps, but according to the docs derive_builder has a flag to skip the build() method:

    Build method suppression: You can use #[builder(build_fn(skip))] to disable auto-implementation of the build method and provide your own.
    Maybe that's all we need?

    @kashifsoofi no worries and thanks for the feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

enhancementgood first issuehelp wanted

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions