Skip to content

Commit

Permalink
Updated README.md and fixed clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
Nawy committed Mar 7, 2024
1 parent c924e58 commit 1d1c2b8
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 35 deletions.
10 changes: 6 additions & 4 deletions README.md
@@ -1,12 +1,14 @@
# Serde Flow - Migration Framework
===========================
Serde Flow - Migration Framework
==================================

[<img alt="build status" src="https://img.shields.io/github/actions/workflow/status/versolid/serde-flow/ci.yml?branch=main&style=for-the-badge" height="20">]
(https://github.com/versolid/serde-flow/actions?query=branch%3Amain)

`serde_flow` is a Rust library that simplifies managing changes in *serialized* data formats during software development, enabling seamless file migration and maintaining version compatibility.

# Features
1. **De/serialize migration** with `#[variants(UserV1, UserV2)]`
2. **De/serialize to file** with `#[flow(variant = 1, file)]`
1. **Serialize migration** with `#[variants(UserV1, UserV2)]`
2. **Serialize to file** with `#[flow(variant = 1, file)]`
3. **Async** with `#[flow(variant = 1, file(nonbloking))]`
4. **Zerocopy** with `#[flow(variant = 1, file, zerocopy)]`

Expand Down
4 changes: 2 additions & 2 deletions serde_flow/src/encoder/zerocopy.rs
Expand Up @@ -85,12 +85,12 @@ where
/// // serialize
/// let person = Person { name: "John Doe".to_string() };
/// let person_bytes: Vec<u8> = zerocopy::Encoder::serialize(&person).unwrap();
///
///
/// // zerocopy deserialize
/// let person_reader = zerocopy::Reader::<Person>::new(person_bytes);
/// let archive = person_reader.archive().unwrap();
/// assert_eq!(archive.name, "John Doe");
///
///
/// ```
pub fn archive(&'a self) -> Result<&'a T::Archived, SerdeFlowError> {
let borrow = self.archived.borrow();
Expand Down
6 changes: 3 additions & 3 deletions serde_flow/src/flow/mod.rs
Expand Up @@ -21,13 +21,13 @@ pub trait FileMigrate<T: Serialize + DeserializeOwned + File<T>> {
}

pub trait FileAsync<T> {
fn load_from_path_async<'a, E: FlowEncoder>(path: &'a Path) -> AsyncResult<T>;
fn load_from_path_async<E: FlowEncoder>(path: &Path) -> AsyncResult<T>;
fn save_to_path_async<'a, E: FlowEncoder>(&'a self, path: &'a Path) -> AsyncResult<()>;
}

pub trait FileMigrateAsync<T: FileAsync<T>> {
fn load_and_migrate_async<'a, E: FlowEncoder>(path: &'a Path) -> AsyncResult<T>;
fn migrate_async<'a, E: FlowEncoder>(path: &'a Path) -> AsyncResult<()>;
fn load_and_migrate_async<E: FlowEncoder>(path: &Path) -> AsyncResult<T>;
fn migrate_async<E: FlowEncoder>(path: &Path) -> AsyncResult<()>;
}

pub trait Bytes<T> {
Expand Down
2 changes: 1 addition & 1 deletion serde_flow/src/flow/zerocopy.rs
@@ -1,7 +1,7 @@
use std::path::Path;

use super::{AsyncResult, FlowResult};
use crate::encoder::zerocopy::{DefaultSerializer, Reader};
use crate::encoder::zerocopy::Reader;

pub trait File<T>
where
Expand Down
53 changes: 28 additions & 25 deletions serde_flow_derive/src/lib.rs
Expand Up @@ -2,11 +2,7 @@ extern crate proc_macro;

use proc_macro::TokenStream;
use quote::quote;
use syn::{
parse_macro_input,
spanned::Spanned,
DeriveInput, Ident
};
use syn::{parse_macro_input, spanned::Spanned, DeriveInput, Ident};

#[proc_macro_derive(Flow, attributes(flow, variants))]
pub fn flow_derive(input: TokenStream) -> TokenStream {
Expand All @@ -20,7 +16,7 @@ pub fn flow_derive(input: TokenStream) -> TokenStream {
return TokenStream::new();
}
};

let flow = flow_gen.generate();
// println!("Flow \n{}", flow);
flow.into()
Expand Down Expand Up @@ -153,7 +149,7 @@ impl FlowGenerator {
}
};
}

if self.is_nonbloking {
generated = quote! {
#generated
Expand Down Expand Up @@ -228,7 +224,7 @@ impl FlowGenerator {
}
};
}

if self.is_nonbloking {
generated = quote! {
#generated
Expand All @@ -241,7 +237,7 @@ impl FlowGenerator {
Ok(object)
})
}
fn migrate_async<'a, E: serde_flow::encoder::FlowEncoder>(path: &'a std::path::Path) -> serde_flow::flow::AsyncResult<()> {
fn migrate_async<E: serde_flow::encoder::FlowEncoder>(path: &std::path::Path) -> serde_flow::flow::AsyncResult<()> {
std::boxed::Box::pin(async {
use serde_flow::flow::FileAsync;
let object = #struct_name::load_from_path_async::<E>(path).await?;
Expand All @@ -254,11 +250,15 @@ impl FlowGenerator {
generated
}

fn component_load_from_path(&self, is_zerocopy: bool, is_bloking: bool) -> proc_macro2::TokenStream {
fn component_load_from_path(
&self,
is_zerocopy: bool,
is_bloking: bool,
) -> proc_macro2::TokenStream {
let struct_name = self.struct_name.clone();
let current_variant = self.variant;
let file_read = self.component_generate_fs_read(is_bloking);

// NON zerocopy
if !is_zerocopy {
// let current_flow_id = gen_variant_id_name(&struct_name);
Expand Down Expand Up @@ -297,18 +297,18 @@ impl FlowGenerator {
fn load_from_path<E: serde_flow::encoder::FlowEncoder>(path: &std::path::Path) -> serde_flow::flow::FlowResult<#struct_name> {
#func_body
}
}
};
}
return quote! {
fn load_from_path_async<'a, E: serde_flow::encoder::FlowEncoder>(path: &'a std::path::Path) -> serde_flow::flow::AsyncResult<#struct_name> {
fn load_from_path_async<E: serde_flow::encoder::FlowEncoder>(path: &std::path::Path) -> serde_flow::flow::AsyncResult<#struct_name> {
std::boxed::Box::pin(async move { #func_body })
}
}
}
};
}

// zerocopy
let variants = self.variants.clone().unwrap_or_default();
let variants: Vec<proc_macro2::TokenStream> = variants
let variants: Vec<proc_macro2::TokenStream> = variants
.into_iter()
.map(|i| {
let const_variant_id_name = gen_variant_id_name(&i);
Expand Down Expand Up @@ -358,8 +358,12 @@ impl FlowGenerator {
}
}
}

fn component_save_to_path(&self, is_zerocopy: bool, is_bloking: bool) -> proc_macro2::TokenStream {

fn component_save_to_path(
&self,
is_zerocopy: bool,
is_bloking: bool,
) -> proc_macro2::TokenStream {
let struct_name = self.struct_name.clone();
let current_variant = self.variant;
let file_write = self.component_generate_fs_write(is_bloking);
Expand All @@ -380,15 +384,14 @@ impl FlowGenerator {
}
};
}

return quote! {
fn save_to_path_async<'a, E: serde_flow::encoder::FlowEncoder>(&'a self, path: &'a std::path::Path) -> serde_flow::flow::AsyncResult<()> {
std::boxed::Box::pin(async move { #func_body })
}
};
}


// Zerocopy
let func_body = quote! {
let mut total_bytes = #current_variant.to_le_bytes().to_vec();
Expand Down Expand Up @@ -418,8 +421,8 @@ impl FlowGenerator {
return quote! {
let mut bytes = std::fs::read(path)?;
};
}
}

#[cfg(feature = "async-std")]
{
return quote! {
Expand All @@ -431,14 +434,14 @@ impl FlowGenerator {
let mut bytes = tokio::fs::read(path).await?;
}
}

fn component_generate_fs_write(&self, is_bloking: bool) -> proc_macro2::TokenStream {
if is_bloking {
return quote! {
std::fs::write(path, &total_bytes)?;
};
}
}

#[cfg(feature = "async-std")]
{
return quote! {
Expand Down
1 change: 1 addition & 0 deletions test_suite/src/json_migration.rs
@@ -0,0 +1 @@

0 comments on commit 1d1c2b8

Please sign in to comment.