Code Monkey home page Code Monkey logo

constrained_type's Introduction

Constrained Type

On the fly value objects in Rust

crates.io Documentation Version MIT or Apache 2.0 licensed Dependency Status
build status Coverage Status downloads

This is a simple project to help create on the fly value objects aka constrained types.

It provides some helper functions to construct these from Rust primitives and turn them into domain primitives, new types, value objects, you name it.

Motivation

Constrained types guarantee valid state and behaviour from dynamic runtime inputs after construction.

This can be useful when creating simple wrapper types, so called newtypes, value objects or domain primitives.

If an input does not meet the validation criteria, it returns an error result instead.

The goal is to remove defensive code statements, ease implementing business invariants and guarantee correct state at runtime.

Heavily inspired by "Domain Modelling Made Functional".

For more complex types, please take a look at the various builder crates.

constrained_type's People

Contributors

ronlobo avatar

Stargazers

David M. Golembiowski avatar Zack Rosen avatar  avatar Will Christman avatar Alejandro Carstens Cattori avatar

constrained_type's Issues

Ideas for 0.3

Spoiler: This project is for learning Rust.

After getting some super helpful feedback from the Rust community, writing down some thoughts for improvements.

Although it works as expected and is well tested, the usage is non-idiomatic.

What's the original idea?

A struct like this, for example:

pub struct EmailAddress (pub String);

This can be instantiated without a check for the correctness of the String actually being a valid email address.

Downstream, this can lead to hard to track bugs, so in general, it's a good practice to do the actual validation right before instantiation. This will lead to usable structures that hold up their invariants.

A more idiomatic Rust way for the above email address structure is to encapsulate the field by not making it public beyond the crate level (useful for testing) and use a constructor function to instantiate the struct:

pub struct EmailAddress(pub(crate) String);

impl FromStr for EmailAddress {
    type Err = InvalidPattern;

    fn from_str(s: &str) Result<Self, Self::Err> {
        // validation goes here
       ...
    }
}

It's still some boilerplate to write, so taking some ideas from other crates, it might be a good idea to turn this into a proc macro:

#[derive(constrained_type)]
#[constrained_type("pattern" = r".+@.+")]
pub struct EmailAddress(pub(crate) String);

This would then generate the FromStr implementation for the structure.
I'll think about this more in detail and, in the meantime, play around with some ideas for the proc macro.

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.