Code Monkey home page Code Monkey logo

named-tup's Introduction

Named Tups

github crates.io docs.rs

Provides a new type called Named Tups that can be called using the tup! macro. Named Tups are structs that can contain a set of named arguments, effectively they work like normal tuples that can be accessed and created using an actual name.

The idea of named tuples is to provide a way to quickly iterate on ideas without having to create a builder struct or losing the ability to type check at compile time. Named tuples also allow the creation of default values that can replace nonexistent arguments.

[dependencies]
named-tup = "0.3.1"

[build-dependencies]
inwelling = "0.4.0"

[package.metadata.inwelling.named-tup-derive]

And put the following in your build.rs file.

fn main() {
    inwelling::register();
}

If you would prefer for this crate to not scan your project files to determine what named arguments are being used add a list of the named tup arguments you used in your Cargo.toml like so.

[package.metadata.inwelling.named-tup-derive]
arguments = ["count", "ingredients", "eggs", "price"]

Examples

use named_tup::tup;
let count = 5;

// This will have the type of Tup!(count: i32, ingredients: [&str; 3], eggs: bool)
let cakes = tup!(count, ingredients: ["milk", "flower", "sugar"], eggs: true);

// We can just add a price afterwards
let mut cakes = cakes + tup!(price: 3);
// And now it has the type of Tup!(eggs: bool, ingredients: [&str; 3], count: i32, price: i32)

// Once the price is in the tup we can just update it!
cakes.price = 4;

// Will print tup { count: 5, eggs: true, ingredients: ["milk", "flower", "sugar"], price: 4 }
println!("{cakes:?}");

To use defaults just annotate the item where you set a field with #[tup_default]. Additionally since the defaulted tup! is a type you need to convert into it by calling .into_tup() which can be accessed through the TupInto trait.

use named_tup::{tup, Tup, tup_default, TupInto};

let options = tup!(read: false, write: true);

// Converts to Tup!(read: false, write: true, create: false, timeout: 5)
open_file("main.rs", options.into_tup());

#[tup_default]
fn open_file(
    path: &str,
    options: Tup!(
        read: bool = true,
        write: bool = false,
        create: bool = false,
        timeout: i32 = 5
    ))
{
    // Open the file
}

To test the crate enable the feature dev-test.

Roadmap

  • Write some more tests
  • Serialise and Deserialise using Serde
  • Provide nice looking types for cargo doc

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

named-tup's People

Contributors

dependabot[bot] avatar miam-miam avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 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.