Giter VIP home page Giter VIP logo

struct-tools-derive's Introduction

struct-tools-derive

Table of Contents

Contains

Tools for working with structs.

currently contains:

  • StructIterTools
    • fields ( ) -> Vec<String>: A Function that returns the names of its fields
    • values<E> (&self) -> Vec<E>: A Method that returns the values of its Instance
    • fields_and_values<E> (&self) -> Vec<(String, E)>: A Method that returns a Vector of Tuples of the field and the values of its Instance
  • StructEnum
  • StructFieldEnum
    • get_fields_enums
  • StructBuilder

How to use

StructIterTools

Lets you iterate over structs

If you have a struct

pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

you can just add the derive to it

use struct_tools_derive::StructIterTools;

#[derive(StructIterTools)]
pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

This now let's you use it like this:

fields

use struct_tools_derive::StructIterTools;

#[derive(StructIterTools)]
#[StructFields]
pub struct Foo{
    field1: i32,
    field2: String,
}

let fields = Foo::fields();
assert_eq!(fields,vec![String::from("field1"), String::from("field2"),...])

values

use struct_tools_derive::StructIterTools;

// Default just for demonstration
#[derive(StructIterTools, Default)]
#[StructValues]
pub struct Foo{
    field1: i32,
    field2: String,
}

enum FooEnum {
  I32(i32),
  String(String),
  {...}
}
impl From<i32> for FooEnum {
    fn from(value: i32) -> Self {
        FooEnum::I32(value)
    }
}
impl From<String> for FooEnum {
    fn from(value: String) -> Self {
        FooEnum::String(value)
    }
}
{...}

let instance = Foo::default();

let values = instance.values::<FooEnum>();

assert_eq!(values,vec![FooEnum::I32(0), FooEnum::String(String::new()),...])

fields and values

use struct_tools_derive::StructIterTools;

// Default just for demonstration
#[derive(StructIterTools, Default)]
#[StructFields]
#[StructValues]
pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

enum FooEnum {
  I32(i32),
  String(String),
  {...}
}
impl From<i32> for FooEnum {
    fn from(value: i32) -> Self {
        FooEnum::I32(value)
    }
}
impl From<String> for FooEnum {
    fn from(value: String) -> Self {
        FooEnum::String(value)
    }
}
{...}

let instance = Foo::default();

let f_v = instance.fields_and_values::<FooEnum>();

assert_eq!(f_v,vec![(String::from("field1"), FooEnum::I32(0)), (String::from("field2"), FooEnum::String(String::new())),...])

StructEnum

Will create an Enum which is capable of containing all possible contents of the struct

If you have a struct

pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

you can just add the derive to it

use struct_tools_derive::StructEnum;

#[derive(StructEnum)]
pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

This Grants you access to an automatically generated Enum with the name {structname}Enum.

its Variants are named by Capitalizing the first letter of the respective Type

pub enum FooEnum{
    I32(i32),
    String(String),
    {...}
}
impl From<i32> for FooEnum {
    fn from(value: i32) -> Self {
        FooEnum::I32(value)
    }
}
impl From<String> for FooEnum {
    fn from(value: String) -> Self {
        FooEnum::String(value)
    }
}
{...}

This means, that it can be used with both values and fields and values

you can also have it derive traits by adding them to the EnumDerives attribute like this:

#[derive(StructEnum)]
#[EnumDerive(Debug)]
pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

StructFieldEnum

Will create an Enum which is capable of containing all possible contents of the struct

If you have a struct

pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

you can just add the derive to it

use struct_tools_derive::StructFieldEnum;

#[derive(StructFieldEnum)]
pub struct Foo{
    field1: i32,
    field2: String,
    {...}
}

This Grants you access to an automatically generated Enum with the name {structname}FieldEnum.

its Variants are named by CapitalCamelCase-ing the fields of the struct

pub enum FooFieldEnum{
    Field1(i32),
    Field2(String),
    {...}
}

you can also have it derive traits by adding them to the EnumDerives attribute like this:

#[derive(StructFieldEnum)]
#[EnumDerive(Debug)]
pub struct Foo{
    field1: i32,
    field2: String,
}

get_fields_enums

TODO!

get_variants

TODO!


StructBuilder

Will create a BuilderPattern Struct.

If you have a struct

pub struct Foo{
    field1: i32,
    field2: String,
    //{...}
}

you can just add the derive to it

use struct_tools_derive::StructBuilder;

#[derive(StructBuilder)]
#[StructFields]
pub struct Foo{
    field1: i32,
    field2: String,
    //{...}
}

This Grants you access to an automatically generated struct with the name {structname}Builder.

#[allow(non_camel_case_types)]
#[derive(Debug)]
pub enum FooBuilderError {
    field1,
    field2,
    //{...}
}
impl std::fmt::Display for FooBuilderError {
    //{...}
}
impl std::error::Error for FooBuilderError {}

pub struct FooBuilder{
    field1: Option<i32>,
    field2: Option<String>,
    //{...}
}
impl Default for FooBuilder{
    fn default() -> Self {
        /*...*/
    }
}
impl FooBuilder {
    pub fn build(self) -> Result<Foo, Vec<FooBuilderError>>{
        //{...}
    }
    pub fn set_field1(mut self, val:i32) -> Self{
        self.field1 = Some(val);
        self
    }
    pub fn set_field2(mut self, val:String) -> Self{
        self.field2 = Some(val);
        self
    }
    //{...}
}

If you want specific fields to have specific Default-values you can add the default-Attribute to it like this:

use struct_tools_derive::{StructFieldEnum, StructBuilder};

#[derive(StructFieldEnum, StructBuilder)]
#[StructFields]
pub struct Foo{
    #[builder_default(1)]
    field1: i32,
    #[builder_default("Hello".to_owned())]
    field2: String,
    //{...}
}

TODO!

struct-tools-derive's People

Contributors

lptimey avatar

Stargazers

 avatar

Watchers

 avatar

struct-tools-derive's Issues

Derives of Enum aren't customisable

It would be cool if Enum could be set to mirror (some of or all) the Derives of its corresponding struct.
Or have the be added independently somehow.

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.