Crate dodo[−][src]
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 collection using the chosen storage and serializer.
type PersonCollection = Collection<Person, Directory, JsonSerializer>;
Finally, create a collection and start using it.
let mut collection = PersonCollection::new(directory); let mut person = Person { id: None, name: "John Smith".into(), age: 42 }; collection.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 PersonCollection = Collection<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 collection = PersonCollection::new(Directory::new(&path)?); collection.insert(&mut person1)?; collection.insert(&mut person2)?; println!("{:?}", collection.find_all()?.filter(|person| person.age > 20).collect()?); Ok(()) }
Modules
collection | Collection of entities. |
index | Index of values. |
prelude | Prelude. Re-exports most used structs and traits. |
serializer | Serializers. |
storage | Storage backends. |
Structs
Collection | Collection of entities, backed by provided storage. |
CollectionError | Collection error. |
Directory | Storage backed by a folder. |
Index | Index of values. |
IndexError | Index error. |
JsonSerializer | JSON serializer (using Serde). |
Memory | Memory storage. |
Enums
CollectionErrorKind | Collection error kind. |
IndexErrorKind | Index error kind. |
Traits
Cursor | Cursor over the contents of a collection. |
Entity | Entity. |
Serializer | Serialize and deserialize data into writer and readers respectively. |
Storage | Bytes storage backend. |
Derive Macros
Entity | Implements the |