4 releases (2 breaking)

0.4.0 Jun 11, 2020
0.3.0 Sep 6, 2019
0.2.1 Oct 27, 2018
0.2.0 Oct 27, 2018

#24 in #primary-key


Used in ledb

MIT license

33KB
606 lines

Derive macro for defining storable documents

License: MIT Travis-CI Build Status Appveyor Build status Crates.io Package Docs.rs API Documentation

This derive macro helps to define documents which can be managed using persistent storages like LEDB.

The LEDB is an attempt to implement simple but efficient, lightweight but powerful document storage.

The abbreviation LEDB may be treated as an Lightweight Embedded DB, also Low End DB, also Literium Engine DB, also LitE DB, and so on.

Usage example

use serde::{Serialize, Deserialize};
use ledb::{Document};

#[derive(Serialize, Deserialize, Document)]
struct MyDoc {
    // define optional primary key field
    #[document(primary)]
    id: Option<Primary>,
    // define unique key field
    #[document(unique)]
    title: String,
    // define index fields
    #[document(index)]
    tag: Vec<String>,
    #[document(unique)]
    timestamp: u32,
    // define nested document
    #[document(nested)]
    meta: MetaData,
}

#[derive(Serialize, Deserialize, Document)]
#[document(nested)]
struct MetaData {
    // define index field
    #[document(index)]
    keywords: Vec<String>,
    // define other fields
    description: String,
}

This automatically generate Document traits like so:

impl Document for MyDoc {
    // declare primary key field name
    fn primary_field() -> Identifier {
        "id".into()
    }

    // declare other key fields for index
    fn key_fields() -> KeyFields {
        KeyFields::new()
            // add key fields of document
            .with_field(("title", String::key_type(), IndexKind::Unique))
            .with_field(("tag", String::key_type(), IndexKind::Index))
            .with_field(("timestamp", u32::key_type(), IndexKind::Unique))
            // add key fields from nested document
            .with_fields(MetaData::key_fields().with_parent("meta"))
    }
}

impl Document for MetaData {
    // declare key fields for index
    fn key_fields() -> KeyFields {
        KeyFields::new()
            // add key fields of document
            .with_field(("keywords", KeyType::String, IndexKind::Index))
    }
}

Dependencies

~1.5MB
~35K SLoC