Git Product home page Git Product logo

jbe's Introduction

JBE (WIP)

JBE allows you to generate a builder for your struct by using a derive macro and comes with two different macros, Builder and ConsumingBuilder. (The ConsumingBuilder macro is not yet implemented.) The difference is how they construct the object in the end. While Builder copies the values to the newly created struct, a ConsumingBuilder moves ownership to the new struct. By default, the builder has the same name as the struct followed by Builder.

#[derive(PartialEq, Builder)]
pub struct MyStruct {
    a: u8
}

fn main() {
    let my_struct = MyStructBuilder::new().a(25).build();
    assert_eq!(my_struct, MyStruct { a: 25 })
}

Macros

Builder

By default, the builder is named <struct name>Builder. For each property of the struct, a Builder has a function with the same name. For building, each builder has two different functions build and try_build. build panics if not all required values are set. In contrast, try_build can return an error. By default, the error is named <builder name>Error.

You can change the default identifiers like this:

#[derive(Builder, PartialEq, Debug)]
#[builder({
    builder_ident: MyUserBuilder,
    error_ident: MyCustomUserBuilderErrorIdent
})]
pub struct User {
    id: usize,
    name: String,
    email: Option<String>
}

jbe's People

Contributors

julian-alberts avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.