[][src]Crate dodo

Dodo

Dodo (pronounced doe doe) is a very basic persistence library designed to be a quick and easy way to create a persistent storage. It uses Serde under the hood to perform serialization of the persisted data.

Getting Started

First, create a struct implementing the Entity, Serialize and Deserialize traits, which can be done using derives. For the Entity derive to work, it must contain a field named id of type Option<Uuid>.

#[derive(Entity, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Person {
    id: Option<Uuid>,
    name: String,
    age: u64,
}

Then, create a storage for your data. You can implement your own, or use the default Directory storage.

let directory = Directory::new(path)?;

Now, you must choose a serializer. You can also implement your own, or use the default JsonSerializer. When this is done, it is highly recommanded to create a type definition for your repository using the chosen storage and serializer.

type PersonRepository = Repository<Person, Directory, JsonSerializer>;

Finally, create a repository and start using it.

let mut repository = PersonRepository::new(directory);
let mut person = Person { id: None, name: "John Smith".into(), age: 42 };
repository.insert(&mut person)?;

Example

This short example demonstrate how to use Dodo to read and write entities to the disk.

use std::error::Error;

use serde::{Deserialize, Serialize};
use uuid::Uuid;

use dodo::prelude::*;

#[derive(Debug, Entity, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
struct Person {
    id: Option<Uuid>,
    name: String,
    age: u64,
}

type PersonRepository = Repository<Person, Directory, JsonSerializer>;

fn main() -> Result<(), Box<dyn Error>> {
    let path = tempfile::tempdir()?;

    let mut person1 = Person { id: None, name: "John Smith".into(), age: 18 };
    let mut person2 = Person { id: None, name: "Mary Smith".into(), age: 42 };

    let mut repository = PersonRepository::new(Directory::new(&path)?);
    repository.insert(&mut person1)?;
    repository.insert(&mut person2)?;

    println!("{:?}", repository.find_all()?.filter(|person| person.age > 20).collect()?);

    Ok(())
}

Modules

entity

Entity.

prelude

Prelude. Re-exports most used structs and traits.

repository

Repository of entities.

serializer

Serializers.

storage

Storage backends.

Structs

Directory

Storage backed by a folder.

JsonSerializer

JSON serializer (using Serde).

Memory

Memory storage.

Repository

Repository of entities, backed by provided storage.

Enums

Error

Repository error.

Traits

Cursor

Cursor over the contents of a repository.

Entity

Entity in a repository.

Serializer

Serialize and deserialize data into writer and readers respectively.

Storage

Bytes storage backend.

Type Definitions

Result

Repository result.

Derive Macros

Entity

Implements the Entity trait for your struct. It must contain a field named id of type Option<Uuid>.