Struct structsy::Structsy

source ·
pub struct Structsy { /* private fields */ }
Expand description

Main API to persist structs with structsy.

Implementations§

source§

impl Structsy

source

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)?;
source

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.

source

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")?;
source

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()?;
source

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>()?;
source

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>()?;
source

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()?;
source

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);
source

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
}
source

pub fn commit(&self, tx: OwnedSytx) -> SRes<()>

👎Deprecated

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()?;
source

pub fn is_defined<T: Persistent>(&self) -> SRes<bool>

Check if a struct is defined

source

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(())
}
source

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(())
}
source

pub fn into_iter<R: Fetch<T>, T>(&self, filter: R) -> StructsyIter<'_, T>

👎Deprecated
source

pub fn list_defined(&self) -> SRes<impl Iterator<Item = Description>>

source

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);

Trait Implementations§

source§

impl Clone for Structsy

source§

fn clone(&self) -> Structsy

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl RawAccess for Structsy

source§

fn raw_begin(&self) -> SRes<RawTransaction>

Begin a new raw transaction
source§

fn raw_define(&self, desc: Description) -> SRes<bool>

Declare a new struct or enum from the generic description
source§

fn raw_scan(&self, ty_name: &str) -> SRes<RawIter>

Scan the records of a struct or enum in a raw format
source§

fn raw_read(&self, id: &str) -> SRes<Option<Record>>

read a single record in a raw formant from a string id
source§

impl RawRead for Structsy

source§

fn raw_scan(&self, strct_name: &str) -> SRes<RawIter>

Scan the records of a struct or enum in a raw format
source§

fn raw_read(&self, id: &str) -> SRes<Option<Record>>

read a single record in a raw formant from a string id

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V