Dade is data definition for Rust structures.

Overview

dade

Test Crates.io

dade is data definition for Rust structures.

For the easy handle of data, the following will support it.

  • Data validation.
  • Data schema conforms JsonSchema.

Example

Basic

To define a model, You need the below module.

use dade::{Model, model};

For example, define user-model.

, #[field(default = false)] verified: bool, }">
#[model]
struct User {
    #[field(ge = 1)]
    id: u64,
    #[field(min_length = 1, max_length = 100)]
    name: String,
    #[field(default = "en")]
    lang: String,
    #[field(min_length = 1, max_length = 255, default = null)]
    url: Option<String>,
    #[field(default = false)]
    verified: bool,
}

Then you create an instance of the model by the below.

let input = "{\"id\": 1, \"name\": \"James Smith\"}";
let user = User::parse(input).unwrap();

And you get a Json string for the instance by the below.

let json_string = user.json(false);
// json_string = "{\"id\":1,\"name\":\"James Smith\",\"lang\":\"en\",\"url\":null,\"verified\":false}"

If you want to validate a value, you will get a schema that conforms JsonSchema, for the given model, by the below.

let schema = User::schema();

The schema is

{
  "$ref": "#/definitions/User",
  "definitions": {
    "User": {
      "title": "User",
      "type": "object",
      "properties": {
        "id": {
          "type": "integer",
          "title": "Id",
          "minimum": 1
        },
        "name": {
          "type": "string",
          "title": "Name",
          "minLength": 1,
          "maxLength": 100
        },
        "lang": {
          "type": "string",
          "title": "Lang",
          "default": "en"
        },
        "url": {
          "type": "string",
          "title": "Url",
          "default": null,
          "minLength": 1,
          "maxLength": 255
        },
        "verified": {
          "type": "boolean",
          "title": "Verified",
          "default": false
        }
      },
      "required": ["id", "name"]
    }
  }
}

Advance

  • If you want to bind other name
#[model]
struct User {
    id: u64,
    #[field(alias = "FirstName")]
    first_name: String,
    #[field(alias = "LastName")]
    last_name: String,
}
  • If you need a nested model
#[model]
struct Name {
    first_name: String,
    last_name: String,
}

#[model]
struct User {
    id: u64,
    name: Name,
}
  • If you need a self-reference model
#[model]
struct Item {
    id: u64,
    name: String,
    value: u128,
    related_items: Option<Vec<Box<Item>>>,
}
You might also like...
Coding-challenge - Algorithms and Data-structures, problems and solutions in Rust language using cargo-workspaces

Coding Challenge LeetCode/Hackerrank e.t.c Using this as an opportunity to improve my knowledge of rust lang If you found this repo useful to you, add

Common data structures and algorithms in Rust

Contest Algorithms in Rust A collection of classic data structures and algorithms, emphasizing usability, beauty and clarity over full generality. As

Rust data structures and client for the PubChem REST API

pubchem.rs Rust data structures and client for the PubChem REST API. πŸ”Œ Usage πŸ’Š Compound Create a Compound to query the PubChem API for a single comp

NixEl is a Rust library that turns Nix code into a variety of correct, typed, memory-safe data-structures

πŸ‰ NixEL Lexer, Parser, Abstract Syntax Tree and Concrete Syntax Tree for the Nix Expressions Language. NixEl is a Rust library that turns Nix code in

rust_aads - Rust Algorithms And Data Structures

rust_aads - Rust Algorithms And Data Structures rust_aads is an open repository with algorithms and data structures, used in computer science and comp

This repository aims to organize codes related to data structures in Rust. πŸ¦€
This repository aims to organize codes related to data structures in Rust. πŸ¦€

Rust Data Structure A project with the objective of introducing the main concepts about data structure using Rust! Explore the docs and learn Rust Β» R

Obake is a procedural macro for declaring and maintaining versioned data-structures.

Obake is a procedural macro for declaring and maintaining versioned data-structures. The name 'obake' is taken from the Japanese 'γŠεŒ–γ‘ (γŠγ°γ‘)', a class of supernatural beings in Japanese folklore that shapeshift.

Garbage Collector(Hyaline- Safe Memory Reclaimation) for lock free data structures

Hyaline-SMR This crate provides garbage collection using hyaline algorithm for building concurrent data structures. When a thread removes an object fr

Library containing implementations of various sequential data-structures.

Library containing implementations of various sequential data-structures.

Comments
  • v0.2

    v0.2

    This PR will be implementing the following features.

    • [x] Support a struct with unnamed field.
    • [x] Support a struct without field.
    • [x] Support an enum with unnamed field or named field or unit.
    • [x] Support a struct with an enum field.
    • [x] Add test cases for model validation.
    enhancement 
    opened by odd12258053 1
Releases(0.2.0)
  • 0.2.0(Mar 12, 2022)

Owner
odd
Creator of agraffe and dade. Python, Rust...
odd
Rust Persistent Data Structures

Rust Persistent Data Structures Rust Persistent Data Structures provides fully persistent data structures with structural sharing. Setup To use rpds a

Diogo Sousa 883 Dec 31, 2022
A proof of concept implementation of cyclic data structures in stable, safe, Rust.

A proof of concept implementation of cyclic data structures in stable, safe, Rust. This demonstrates the combined power of the static-rc crate and the

null 157 Dec 28, 2022
Rust library for string parsing of basic data structures.

afmt Simple rust library for parsing basic data structures from strings. Usage You can specify string formats to any strucute, via the use of the fmt

Eduard 4 May 8, 2021
Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

Serde is a framework for serializing and deserializing Rust data structures efficiently and generically.

null 6.5k Dec 31, 2022
Algorithms and Data Structures of all kinds written in Rust.

Classic Algorithms in Rust This repo contains the implementation of various classic algorithms for educational purposes in Rust. Right now, it is in i

Alexander GonzΓ‘lez 49 Dec 14, 2022
Library containing various Data Structures implemented using Rust.

rust-data-structures Library containing various Data Structures implemented using Rust. Running You can test the library by running cargo test, an exa

c1m50c 1 Jan 6, 2022
A library for comparing data structures in Rust, oriented toward testing

Delta: Structural differencing in Rust The delta crate defines the trait Delta, along with a derive macro for auto-generating instances of this trait

John Wiegley 19 Oct 7, 2022
A library for comparing data structures in Rust, oriented toward testing

The comparable crate defines the trait [Comparable], along with a derive macro for auto-generating instances of this trait for most data types. Primar

John Wiegley 19 Oct 7, 2022
Collection of Data Structures in Rust

cds - Collection of Data Structures !!! UNDER CONSTRUCTION !!! The version v0.0.1 is a crates.io placeholder. License Licensed under either of Apache

Rafael Buchbinder 2 May 23, 2022
Succinct data structures in Rust

sucds: Succinct data structures in Rust sucds contains some succinct data structures written in Rust. Data structures So far, the following data struc

Shunsuke Kanda 43 Dec 3, 2022