2 releases

new 0.1.1 Nov 24, 2024
0.1.0 Nov 24, 2024

#2564 in Data structures


Used in tuco

MIT license

15KB
134 lines

Tuco

Tuco can automatically generate tuple representations of simple types. This is helpful if you want to create an API using plain types. Or if you want to provide an easy way to convert between types.

Using #[derive(Tuco)] Will automatically derive Tuco and generate a tuple representation of your struct and assign it to the associated type Tuco::Tuple

Tuco can be used in places where data serialization is used with some differences:

  • Conversion operations are validated at compile time.
  • Since tuples are unnamed, any matching tuple shape may be used to construct your type. No further integrity checks are applied at this time.
  • Tuco does not automatically serialize your types to string data.

Todo

  • Support structs with named fields
  • Support structs with unnamed fields
  • Support more complex structs
  • Documentation
  • Compiler errors / Tests

Example


    #[derive(Tuco, Debug)]
    struct MyStruct {
        a: i32,
        b: String,
        c: bool,
    }

    #[derive(Tuco, Debug)]
    struct MyOtherStruct {
        x: i32,
        y: String,
        z: bool,
    }

    #[derive(Tuco, Debug)]
    struct MyNestedStruct {
        x: i32,
        #[tuco]
        y: MyStruct,
        z: bool,
    }

    #[derive(Tuco, Debug)]
    struct MyOtherNestedStruct {
        x: i32,
        #[tuco]
        y: MyOtherStruct,
        z: bool,
    }

    fn nested_struct() {
        let my_nested_struct = MyNestedStruct {
            x: 10,
            y: MyStruct {
                a: 1,
                b: "qwe".to_string(),
                c: true,
            },
            z: false,
        };

        println!("{:?}", my_nested_struct);

        let my_other_struct = MyOtherNestedStruct::from_tuco(my_nested_struct);

        println!("{:?}", my_other_struct);
    } 

Output:

    MyNestedStruct { x: 10, y: MyStruct { a: 1, b: "qwe", c: true }, z: false }
    MyOtherNestedStruct { x: 10, y: MyOtherStruct { x: 1, y: "qwe", z: true }, z: false }

License

Copyright (c) 2024 Johannes Gotlén

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Dependencies

~210–640KB
~15K SLoC