Skip to content

Commit

Permalink
Add derived fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tinrab committed Dec 2, 2023
1 parent 9d06197 commit 3f8f8db
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 29 deletions.
12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni"
version = "0.1.14"
version = "0.1.15"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utility Library for Rust"
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -38,9 +38,9 @@ tokio = ["bomboni_common/tokio"]
tonic = ["bomboni_proto/tonic", "bomboni_request/tonic"]

[dependencies]
bomboni_common = { path = "bomboni_common", version = "0.1.14" }
bomboni_common = { path = "bomboni_common", version = "0.1.15" }

bomboni_prost = { path = "bomboni_prost", version = "0.1.14", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.14", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.14", optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.14", optional = true }
bomboni_prost = { path = "bomboni_prost", version = "0.1.15", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.15", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.15", optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.15", optional = true }
2 changes: 1 addition & 1 deletion bomboni_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_common"
version = "0.1.14"
version = "0.1.15"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Common things for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
2 changes: 1 addition & 1 deletion bomboni_prost/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_prost"
version = "0.1.14"
version = "0.1.15"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with prost. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
4 changes: 2 additions & 2 deletions bomboni_proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_proto"
version = "0.1.14"
version = "0.1.15"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with Protobuf/gRPC. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -36,5 +36,5 @@ serde_json = { version = "1.0.108", optional = true }
serde_json = "1.0.108"

[build-dependencies]
bomboni_prost = { path = "../bomboni_prost", version = "0.1.14" }
bomboni_prost = { path = "../bomboni_prost", version = "0.1.15" }
prost-build = "0.12.3"
8 changes: 4 additions & 4 deletions bomboni_request/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_request"
version = "0.1.14"
version = "0.1.15"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with API requests. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -19,8 +19,8 @@ testing = []
tonic = ["bomboni_proto/tonic", "dep:tonic"]

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.14" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.14" }
bomboni_common = { path = "../bomboni_common", version = "0.1.15" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.15" }
thiserror = "1.0.50"
itertools = "0.12.0"
time = { version = "0.3.30", features = ["formatting", "parsing"] }
Expand All @@ -35,4 +35,4 @@ rand = "0.8.5"
regex = "1.10.2"

tonic = { version = "0.10.2", optional = true }
bomboni_request_derive = { path = "../bomboni_request_derive", version = "0.1.14", optional = true }
bomboni_request_derive = { path = "../bomboni_request_derive", version = "0.1.15", optional = true }
24 changes: 24 additions & 0 deletions bomboni_request/src/parse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,30 @@ mod tests {

use super::*;

#[test]
fn derived_fields() {
#[derive(Debug, PartialEq, Default)]
struct Item {
x: i32,
y: i32,
}
#[derive(Parse, Debug, PartialEq)]
#[parse(source = Item, write)]
struct ParsedItem {
#[parse(derive = derive_value)]
z: i32,
}

#[allow(clippy::unnecessary_wraps)]
fn derive_value(item: &Item) -> RequestResult<i32> {
Ok(item.x + item.y)
}

assert_eq!(ParsedItem::parse(Item { x: 3, y: 5 }).unwrap().z, 8);

assert_eq!(Item::from(ParsedItem { z: 8 }), Item::default());
}

#[test]
fn it_works() {
#[derive(Debug, PartialEq)]
Expand Down
2 changes: 1 addition & 1 deletion bomboni_request_derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_request_derive"
version = "0.1.14"
version = "0.1.15"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Provides derive implementations for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
4 changes: 4 additions & 0 deletions bomboni_request_derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ pub struct ParseField {
/// The names of the functions must be `parse` and `write` respectively.
#[darling(with = parse_expr::parse_str_literal, map = Some)]
pub with: Option<Expr>,
/// Make this field derived.
/// The function must have the signature `fn(source: &Source) -> RequestResult<T>`.
#[darling(with = parse_expr::parse_str_literal, map = Some)]
pub derive: Option<Expr>,
}

#[derive(Debug, FromVariant)]
Expand Down
31 changes: 20 additions & 11 deletions bomboni_request_derive/src/parse_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,11 @@ fn expand_parse(options: &ParseOptions, fields: &[ParseField]) -> syn::Result<To
continue;
}

let parse_field = if field.resource.is_some() {
expand_parse_resource_field(field)?
if field.resource.is_some() {
parse_fields.extend(expand_parse_resource_field(field)?);
} else {
expand_parse_field(field)?
parse_fields.extend(expand_parse_field(field)?);
};
parse_fields.extend(quote! {
#field_ident: { #parse_field },
});
}

Ok(quote! {
Expand Down Expand Up @@ -77,6 +74,13 @@ fn expand_parse_field(field: &ParseField) -> syn::Result<TokenStream> {
} else {
field.ident.clone().unwrap()
};
let target_ident = field.ident.as_ref().unwrap();

if let Some(derive) = field.derive.as_ref() {
return Ok(quote! {
#target_ident: { #derive(&source)? },
});
}

let (is_option, is_nested, is_string) = check_proto_type(&field.ty);

Expand Down Expand Up @@ -307,8 +311,10 @@ fn expand_parse_field(field: &ParseField) -> syn::Result<TokenStream> {
}

Ok(quote! {
#result
target
#target_ident: {
#result
target
},
})
}

Expand All @@ -320,6 +326,7 @@ fn expand_parse_resource_field(field: &ParseField) -> syn::Result<TokenStream> {
));
}

let target_ident = field.ident.as_ref().unwrap();
let options = field.resource.as_ref().unwrap();

let mut result = quote! {
Expand Down Expand Up @@ -388,8 +395,10 @@ fn expand_parse_resource_field(field: &ParseField) -> syn::Result<TokenStream> {
}

Ok(quote! {
#result
result
#target_ident: {
#result
result
},
})
}

Expand All @@ -400,7 +409,7 @@ fn expand_write(options: &ParseOptions, fields: &[ParseField]) -> TokenStream {
let mut write_fields = quote!();

for field in fields {
if field.skip {
if field.skip || field.derive.is_some() {
continue;
}

Expand Down
6 changes: 3 additions & 3 deletions bomboni_template/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_template"
version = "0.1.14"
version = "0.1.15"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working Handlebars templates. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -17,8 +17,8 @@ path = "src/lib.rs"
testing = []

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.14" }
bomboni_proto = { version = "0.1.14", path = "../bomboni_proto", features = [
bomboni_common = { path = "../bomboni_common", version = "0.1.15" }
bomboni_proto = { version = "0.1.15", path = "../bomboni_proto", features = [
"json",
] }
thiserror = "1.0.50"
Expand Down

0 comments on commit 3f8f8db

Please sign in to comment.