Skip to content

Commit

Permalink
Change resource parser and id parse helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tinrab committed Dec 2, 2023
1 parent 5bc71f2 commit 9d06197
Show file tree
Hide file tree
Showing 11 changed files with 143 additions and 22 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.13"
version = "0.1.14"
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.13" }
bomboni_common = { path = "bomboni_common", version = "0.1.14" }

bomboni_prost = { path = "bomboni_prost", version = "0.1.13", optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.13", optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.13", optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.13", optional = true }
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 }
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.13"
version = "0.1.14"
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.13"
version = "0.1.14"
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.13"
version = "0.1.14"
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.13" }
bomboni_prost = { path = "../bomboni_prost", version = "0.1.14" }
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.13"
version = "0.1.14"
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.13" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.13" }
bomboni_common = { path = "../bomboni_common", version = "0.1.14" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.14" }
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.13", optional = true }
bomboni_request_derive = { path = "../bomboni_request_derive", version = "0.1.14", optional = true }
55 changes: 55 additions & 0 deletions bomboni_request/src/parse/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
pub mod parse_id {
use bomboni_common::id::Id;

use crate::error::{CommonError, RequestResult};

pub fn parse(source: String) -> RequestResult<Id> {
Ok(source.parse().map_err(|_| CommonError::InvalidId)?)
}

pub fn write(id: Id) -> String {
id.to_string()
}
}

#[cfg(test)]
mod tests {
use crate::{error::RequestError, parse::RequestParse};
use bomboni_common::id::Id;
use bomboni_request_derive::Parse;

use super::*;

#[test]
fn parse_ids() {
#[derive(Debug, PartialEq, Default)]
struct Item {
id: String,
}
#[derive(Parse, Debug, PartialEq)]
#[parse(source=Item, write)]
struct ParsedItem {
#[parse(with=parse_id)]
id: Id,
}

assert_eq!(parse_id::parse("F".to_string()).unwrap(), Id::new(15));
assert!(parse_id::parse("-1".to_string()).is_err());
assert!(parse_id::parse("x".to_string()).is_err());
assert!(parse_id::parse(String::new()).is_err());

assert_eq!(
ParsedItem::parse(Item {
id: "F".to_string(),
})
.unwrap(),
ParsedItem { id: Id::new(15) }
);
assert_eq!(
Item::from(ParsedItem { id: Id::new(15) }),
Item {
id: "f".to_string()
}
);
}
}
30 changes: 28 additions & 2 deletions bomboni_request/src/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bomboni_common::id::Id;
use time::OffsetDateTime;
pub mod helpers;

pub trait RequestParse<T>: Sized {
type Error;
Expand Down Expand Up @@ -571,7 +572,7 @@ mod tests {
update_time: Option<Timestamp>,
delete_time: Option<Timestamp>,
deleted: bool,
// etag: Option<String>,
etag: Option<String>,
}

#[derive(Parse, Debug, PartialEq)]
Expand All @@ -583,6 +584,13 @@ mod tests {
resource: ParsedResource,
}

#[derive(Parse, Debug, PartialEq)]
#[parse(source = Item, write)]
struct ParsedItemDefaultResource {
#[parse(resource)]
resource: ParsedResource,
}

assert_eq!(
ParsedItem::parse(Item {
name: "items/42".into(),
Expand All @@ -607,7 +615,7 @@ mod tests {
create_time: Some(OffsetDateTime::UNIX_EPOCH),
deleted: true,
..Default::default()
}
},
}),
Item {
name: "items/42".into(),
Expand All @@ -616,6 +624,24 @@ mod tests {
..Default::default()
}
);

assert_eq!(
ParsedItemDefaultResource::parse(Item {
name: "items/42".into(),
create_time: Some(OffsetDateTime::UNIX_EPOCH.into()),
deleted: true,
..Default::default()
})
.unwrap(),
ParsedItemDefaultResource {
resource: ParsedResource {
name: "items/42".into(),
create_time: Some(OffsetDateTime::UNIX_EPOCH),
deleted: true,
..Default::default()
}
}
);
}

#[test]
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.13"
version = "0.1.14"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Provides derive implementations for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
42 changes: 41 additions & 1 deletion bomboni_request_derive/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub struct ParseVariant {
pub with: Option<Expr>,
}

#[derive(Debug, FromMeta)]
#[derive(Debug)]
pub struct ResourceOptions {
pub fields: ResourceFields,
}
Expand Down Expand Up @@ -119,6 +119,46 @@ pub fn expand(input: DeriveInput) -> syn::Result<TokenStream> {
}
}

impl FromMeta for ResourceOptions {
fn from_list(items: &[ast::NestedMeta]) -> darling::Result<Self> {
let mut fields = ResourceFields::default();
for item in items {
match item {
ast::NestedMeta::Meta(meta) => {
let ident = meta.path().get_ident().unwrap();
match ident.to_string().as_str() {
"fields" => {
fields = ResourceFields::from_meta(meta)?;
}
_ => {
return Err(
darling::Error::custom("unknown resource option").with_span(ident)
);
}
}
}
ast::NestedMeta::Lit(lit) => {
return Err(darling::Error::custom("unexpected literal").with_span(lit));
}
}
}
Ok(Self { fields })
}

fn from_word() -> darling::Result<Self> {
Ok(Self {
fields: ResourceFields {
name: true,
create_time: true,
update_time: true,
delete_time: true,
deleted: true,
etag: true,
},
})
}
}

impl FromMeta for ResourceFields {
fn from_meta(item: &Meta) -> darling::Result<Self> {
let mut fields = ResourceFields::default();
Expand Down
2 changes: 1 addition & 1 deletion bomboni_request_derive/src/parse_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ fn expand_write_resource(field: &ParseField) -> TokenStream {
}
if options.fields.etag {
result.extend(quote! {
etag: value.#ident.etag.unwrap_or_else(Default::default),
etag: value.#ident.etag,
});
}

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.13"
version = "0.1.14"
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.13" }
bomboni_proto = { version = "0.1.13", path = "../bomboni_proto", features = [
bomboni_common = { path = "../bomboni_common", version = "0.1.14" }
bomboni_proto = { version = "0.1.14", path = "../bomboni_proto", features = [
"json",
] }
thiserror = "1.0.50"
Expand Down

0 comments on commit 9d06197

Please sign in to comment.