pub struct Structsy { /* private fields */ }
Expand description
Main API to persist structs with structsy.
Implementations§
source§impl Structsy
impl Structsy
sourcepub fn config<C: AsRef<Path>>(path: C) -> StructsyConfig
pub fn config<C: AsRef<Path>>(path: C) -> StructsyConfig
Config builder for open and/or create a structsy file.
Example
use structsy::Structsy;
let config = Structsy::config("path/to/file.stry");
let config = config.create(false);
let stry = Structsy::open(config)?;
sourcepub fn prepare_open<C: Into<StructsyConfig>>(config: C) -> SRes<PrepareOpen>
pub fn prepare_open<C: Into<StructsyConfig>>(config: C) -> SRes<PrepareOpen>
Prepare the open of a stuctsy file with the possibility to do migrations of data of previous structs.
sourcepub fn open<C: Into<StructsyConfig>>(config: C) -> SRes<Structsy>
pub fn open<C: Into<StructsyConfig>>(config: C) -> SRes<Structsy>
Open a Structsy file, following the configuration as parameter, if the parameter is just a path it will create the file if it does not exist.
Example
use structsy::Structsy;
let stry = Structsy::open("path/to/file.stry")?;
sourcepub fn memory() -> SRes<Structsy>
pub fn memory() -> SRes<Structsy>
Open a structsy instance with only in memory persistence. This instance will delete all the data when went out of scope.
Example
use structsy::Structsy;
let stry = Structsy::memory()?;
sourcepub fn define<T: Persistent>(&self) -> SRes<bool>
pub fn define<T: Persistent>(&self) -> SRes<bool>
Every struct before use must be ‘defined’ calling this method.
Example
use structsy::Structsy;
use structsy_derive::Persistent;
#[derive(Persistent)]
struct Simple {
name:String,
}
let stry = Structsy::open("path/to/file.stry")?;
stry.define::<Simple>()?;
sourcepub fn undefine<T: Persistent>(&self) -> SRes<()>
pub fn undefine<T: Persistent>(&self) -> SRes<()>
Remove a defined struct deleting all the contained data.
Example
use structsy::Structsy;
use structsy_derive::Persistent;
#[derive(Persistent)]
struct Simple {
name:String,
}
let stry = Structsy::open("path/to/file.stry")?;
stry.define::<Simple>()?;
stry.undefine::<Simple>()?;
sourcepub fn begin(&self) -> SRes<OwnedSytx>
pub fn begin(&self) -> SRes<OwnedSytx>
Begin a new transaction needed to manipulate data.
Returns an instance of OwnedSytx
to be used with the StructsyTx
trait.
Example
use structsy::{Structsy,StructsyTx};
let stry = Structsy::open("path/to/file.stry")?;
//....
let mut tx = stry.begin()?;
// ... operate on tx.
tx.commit()?;
sourcepub fn read<T: Persistent>(&self, sref: &Ref<T>) -> SRes<Option<T>>
pub fn read<T: Persistent>(&self, sref: &Ref<T>) -> SRes<Option<T>>
Read a persistent instance.
Example
use structsy::{Structsy,StructsyTx};
use structsy_derive::Persistent;
#[derive(Persistent)]
struct Example {
value:u8,
}
//.. open structsy etc.
let mut tx = structsy.begin()?;
let id = tx.insert(&Example{value:10})?;
tx.commit()?;
let read = structsy.read(&id)?;
assert_eq!(10,read.unwrap().value);
sourcepub fn scan<T: Persistent>(&self) -> SRes<RecordIter<T>>
pub fn scan<T: Persistent>(&self) -> SRes<RecordIter<T>>
Scan records of a specific struct.
Example
use structsy::Structsy;
use structsy_derive::Persistent;
#[derive(Persistent)]
struct Simple {
name:String,
}
let stry = Structsy::open("path/to/file.stry")?;
stry.define::<Simple>()?;
for (id, inst) in stry.scan::<Simple>()? {
// logic here
}
sourcepub fn commit(&self, tx: OwnedSytx) -> SRes<()>
👎Deprecated
pub fn commit(&self, tx: OwnedSytx) -> SRes<()>
Commit a transaction
Example
use structsy::{Structsy, StructsyTx};
let stry = Structsy::open("path/to/file.stry")?;
//....
let mut tx = stry.begin()?;
// ... operate on tx.
tx.commit()?;
sourcepub fn is_defined<T: Persistent>(&self) -> SRes<bool>
pub fn is_defined<T: Persistent>(&self) -> SRes<bool>
Check if a struct is defined
sourcepub fn query<T: Persistent>(&self) -> StructsyQuery<T>
pub fn query<T: Persistent>(&self) -> StructsyQuery<T>
Query for a persistent struct
Example
use structsy::{ Structsy, StructsyTx, StructsyError};
use structsy_derive::{queries, Persistent};
#[derive(Persistent)]
struct Basic {
name: String,
}
impl Basic {
fn new(name: &str) -> Basic {
Basic { name: name.to_string() }
}
}
#[queries(Basic)]
trait BasicQuery {
fn by_name(self, name: String) -> Self;
}
fn basic_query() -> Result<(), StructsyError> {
let structsy = Structsy::open("file.structsy")?;
structsy.define::<Basic>()?;
let mut tx = structsy.begin()?;
tx.insert(&Basic::new("aaa"))?;
tx.commit()?;
let count = structsy.query::<Basic>().by_name("aaa".to_string()).fetch().count();
assert_eq!(count, 1);
Ok(())
}
sourcepub fn fetch<R: Fetch<T>, T>(&self, filter: R) -> StructsyIter<'_, T> ⓘ
pub fn fetch<R: Fetch<T>, T>(&self, filter: R) -> StructsyIter<'_, T> ⓘ
Execute a filter query and return an iterator of results
Example
use structsy::{ Structsy, StructsyTx, StructsyError, Filter};
use structsy_derive::{queries, embedded_queries, Persistent, PersistentEmbedded};
#[derive(Persistent)]
struct WithEmbedded {
embedded: Embedded,
}
#[derive(PersistentEmbedded)]
struct Embedded {
name: String,
}
impl WithEmbedded {
fn new(name: &str) -> WithEmbedded {
WithEmbedded {
embedded: Embedded { name: name.to_string() },
}
}
}
#[queries(WithEmbedded)]
trait WithEmbeddedQuery {
fn embedded(self, embedded: Filter<Embedded>) -> Self;
}
#[embedded_queries(Embedded)]
trait EmbeddedQuery {
fn by_name(self, name: String) -> Self;
}
fn embedded_query() -> Result<(), StructsyError> {
let structsy = Structsy::open("file.structsy")?;
structsy.define::<WithEmbedded>()?;
let mut tx = structsy.begin()?;
tx.insert(&WithEmbedded::new("aaa"))?;
tx.commit()?;
let embedded_filter = Filter::<Embedded>::new().by_name("aaa".to_string());
let filter = Filter::<WithEmbedded>::new().embedded(embedded_filter);
let count = structsy.fetch(filter).count();
assert_eq!(count, 1);
Ok(())
}
pub fn into_iter<R: Fetch<T>, T>(&self, filter: R) -> StructsyIter<'_, T> ⓘ
pub fn list_defined(&self) -> SRes<impl Iterator<Item = Description>>
sourcepub fn snapshot(&self) -> SRes<Snapshot>
pub fn snapshot(&self) -> SRes<Snapshot>
Create a new snapshot at this specific moment.
Example
use structsy::{Structsy,StructsyTx};
use structsy_derive::Persistent;
#[derive(Persistent)]
struct Example {
value:u8,
}
//.. open structsy etc.
let mut tx = structsy.begin()?;
let id = tx.insert(&Example{value:10})?;
tx.commit()?;
let snapshot = structsy.snapshot()?;
let read = snapshot.read(&id)?;
assert_eq!(10,read.unwrap().value);